score:4

there is a little miss use of life-cycle hooks there.

you should be giving initial value to state on class constructor.

e.g.

class example extends react.component {
  constructor(props){
    super(props)
    this.state = {
      name: this.props.cookies.get('name') || 'ben',
    }
  }
...

or if you use newer es7 syntax just

class example extends react.component {
    state = {
      name: this.props.cookies.get('name') || 'ben',
    }
...

constructor -function is not required on lower example

componentwillmount life-cycle will also be deprecated in upcoming react releases so i would recommend to avoid using it.

replacement for it is accordingly

static getderivedstatefromprops(nextprops, prevstate){
   return {username: nextprops.username}
}

see difference here, we return normal object which then get's passed to components state. so in here username would be get passed to state.


Related Query

More Query from same tag