小编Riz*_*ryo的帖子

不调用getDerivedStateFromProps

我用的阵营16.3.1next.js.
我将getDerivedStateFromProps放在扩展PureComponent的类中.

这是代码:

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 …
Run Code Online (Sandbox Code Playgroud)

reactjs next.js react-16

13
推荐指数
2
解决办法
8391
查看次数

标签 统计

next.js ×1

react-16 ×1

reactjs ×1