Skip to content

Commit

Permalink
chore(Sticky): remove usage of deprecated lifecycle methods (#3974)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Jun 29, 2020
1 parent 9adb4bf commit 20d851d
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/modules/Sticky/Sticky.js
Expand Up @@ -88,6 +88,7 @@ export default class Sticky extends Component {
}

state = {
active: true,
sticky: false,
}

Expand All @@ -96,43 +97,47 @@ export default class Sticky extends Component {

componentDidMount() {
if (!isBrowser()) return
const { active } = this.props
const { active } = this.state

if (active) {
this.handleUpdate()
this.addListeners(this.props)
this.addListeners(this.props.scrollContext)
}
}

// eslint-disable-next-line camelcase
UNSAFE_componentWillReceiveProps(nextProps) {
const { active: current, scrollContext: currentScrollContext } = this.props
const { active: next, scrollContext: nextScrollContext } = nextProps
static getDerivedStateFromProps(props, state) {
if (state.active !== props.active && !props.active) {
return { active: props.active, sticky: false }
}

if (current === next) {
if (currentScrollContext !== nextScrollContext) {
this.removeListeners()
this.addListeners(nextProps)
return { active: props.active }
}

componentDidUpdate(prevProps, prevState) {
if (prevState.active === this.state.active) {
if (prevProps.scrollContext !== this.props.scrollContext) {
this.removeListeners(prevProps.scrollContext)
this.addListeners(this.props.scrollContext)
}

return
}

if (next) {
if (this.state.active) {
this.handleUpdate()
this.addListeners(nextProps)
this.addListeners(this.props.scrollContext)
return
}

this.removeListeners()
this.setState({ sticky: false })
this.removeListeners(prevProps.scrollContext)
}

componentWillUnmount() {
if (!isBrowser()) return
const { active } = this.props
const { active } = this.state

if (active) {
this.removeListeners()
this.removeListeners(this.props.scrollContext)
cancelAnimationFrame(this.frameId)
}
}
Expand All @@ -141,8 +146,7 @@ export default class Sticky extends Component {
// Events
// ----------------------------------------

addListeners = (props) => {
const { scrollContext } = props
addListeners = (scrollContext) => {
const scrollContextNode = isRefObject(scrollContext) ? scrollContext.current : scrollContext

if (scrollContextNode) {
Expand All @@ -151,8 +155,7 @@ export default class Sticky extends Component {
}
}

removeListeners = () => {
const { scrollContext } = this.props
removeListeners = (scrollContext) => {
const scrollContextNode = isRefObject(scrollContext) ? scrollContext.current : scrollContext

if (scrollContextNode) {
Expand Down

0 comments on commit 20d851d

Please sign in to comment.