getDerivedStateFromProps is not called
Asked Answered
C

2

13

I use React 16.3.1 and next.js.
And I put getDerivedStateFromProps inside the class extending PureComponent.

Here is the code:

Header.js

import { PureComponent } from 'react'
...

export default class Header extends PureComponent {
  constructor (props) {
    super(props)

    this.colorAnimationProps = {
      animationDuration: '0.4s',
      animationFillMode: 'forwards'
    }

    this.colorAnimationStyle = {
      toColor: {
        animationName: 'toColor',
        ...this.colorAnimationProps
      },
      toTransparent: {
        animationName: 'toTransparent',
        ...this.colorAnimationProps
      }
    }

    this.state = {
      colorAnimation: {},
      headerModal: null
    }
  }

  componentDidMount () {
    if (this.props.isColor) {
      this.setState({colorAnimation: this.colorAnimationStyle.toColor})
    }
  }

  static getDerivedStateFromProps (nextProps, prevState) {
    console.log('should go here')
    if (nextProps.isColor) {
      return {colorAnimation: this.colorAnimationStyle.toColor}
    }
    return {colorAnimation: this.colorAnimationStyle.toTransparent}
  }

  render () {
    ...
  }
}

And here is the parent that modifies the prop:

index.js

import { PureComponent } from 'react'

...
import Header from '../components/Header'
import Layout from '../components/Layout'
import { withReduxSaga } from '../redux/store'

class Index extends PureComponent {
  constructor (props) {
    super(props)

    this.state = {
      isHeaderColor: false
    }
  }

  componentDidMount () {
    if (window.pageYOffset > 50) {
      this.setState({isHeaderColor: true})
    }

    window.addEventListener('scroll', (e) => {
      if (window.pageYOffset > 50) {
        this.setState({isHeaderColor: true})
      } else {
        this.setState({isHeaderColor: false})
      }
    })
  }

  render () {
    return (
      <Layout url={this.props.url}>
        <Header isColor={this.state.isHeaderColor} />
        ...
      </Layout>
    )
  }
}

export default withReduxSaga(Index)

My problem is: getDerivedStateFromProps is not called when the prop changes. At least, it should do console.log, but it doesn't.

Can anybody here help me?

Cilia answered 5/4, 2018 at 13:4 Comment(5)
Where is the code?Royden
@TheReason I'm sorry, I forgot to put the code. I just updated my question. Thank you.Cilia
Where your props come from?Royden
@TheReason The props come from the parent, index.js. I just updated my question again. And again, thank you.Cilia
which version of nextjs you use?Bunn
B
7

I see that support for this hook was patched in version - 6.0.0-canary.2 of next.JS. So I'm guessing you use an older version.

Bunn answered 5/4, 2018 at 17:7 Comment(0)
J
38

make sure you have the right versions of both react and react-dom in your package.json:

"react": "^16.3.1",
"react-dom": "^16.3.1"
Jacy answered 24/4, 2018 at 20:46 Comment(1)
This worked for me-- I had only upgraded react to 16.3.2. react-dom was still at 16.2.0Anesthesiology
B
7

I see that support for this hook was patched in version - 6.0.0-canary.2 of next.JS. So I'm guessing you use an older version.

Bunn answered 5/4, 2018 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.