thundervexi
thundervexi

Reputation: 1

How to convert unsafe_componentwillreceiveprops(nextprops) to useEffect

I am new to ReactJS. I want to convert the class components into functional components. Please help.

I need help in converting unsafe_componentwillreceiveprops(nextprops) to useEffect

UNSAFE_componentWillReceiveProps(nextProps) {
    this.props.getDirects(this.state.myDirects);
  }

 const getDirects = directs => {
   setMyDirects(directs || []);
  };

Upvotes: 0

Views: 349

Answers (1)

kyun
kyun

Reputation: 10284

/*
UNSAFE_componentWillReceiveProps(nextProps) {
  this.props.getDirects(this.state.myDirects);
}
*/

function yourComp({ getDirects }){
  const [myDirects, setMyDirects] = React.useState();
  React.useEffect(() => {
    getDirects(myDirects);
  }, [getDirects, myDirects]);

  return(...);
}

Upvotes: 0

Related Questions