Reactjs 使用React.useffect()触摸以前的道具

Reactjs 使用React.useffect()触摸以前的道具,reactjs,Reactjs,现在我正在将类组件重构为函数 这是我的类组件,它应该是函数 类应用程序扩展了React.Component{ //... componentDidUpdate({info:prevInfo}){ const{info}=this.props; 如果(info.id!==prevInfo.id){ //做点什么。。。 } } //... } 功能应用程序(道具){ //... React.useffect(()=>{ //我不知道我怎么能碰上以前的道具。 如果(props.info!==prop

现在我正在将
组件重构为
函数

这是我的
组件,它应该是
函数

类应用程序扩展了React.Component{
//...
componentDidUpdate({info:prevInfo}){
const{info}=this.props;
如果(info.id!==prevInfo.id){
//做点什么。。。
}
}
//...
}
功能应用程序(道具){
//...
React.useffect(()=>{
//我不知道我怎么能碰上以前的道具。
如果(props.info!==props.prevInfo){
//...
}
},[props.info]);
//...
}

您可以使用类似于
usePrevious
hook()的方法来获取previous值

尽管如此,如果您只想在
props.info.id
更改后执行某些操作,那么更好的方法是将其用作
useffect
的依赖项,如下所示:

function App(props) {
  //...
  React.useEffect(() => {
     // Now you know for sure that id changed from previous one
     // because it is used inside dependency array for useEffect
     // and useEffect will only be invoked on first mount or after any of dependency was changed
  }, [props.info.id]);
  //...
}