在render方法中处理重新渲染组件的最佳方法

时间:2023-01-15 19:08:16

I have a component,assume(Login).When i sent the request to server,i am fetching response(eventually i am getting the data in terms of props to my Login Component).Form props i need to validate ,to which component ,i allow the user.The below thing what i am doing so far,please update me the best approach

我有一个组件,假设(登录)。当我将请求发送到服务器时,我正在获取响应(最终我得到的道具数据到我的登录组件).Form道具我需要验证,哪个组件,我允许用户。到目前为止,我正在做的事情,请更新我最好的方法

See, this is what i am doing so far

看,这就是我到目前为止所做的

    render()
{

       const {status}=this.props;
       if(status===1){
          return (<X/>)
       } else {
          return (<X/>
       }
}

1 个解决方案

#1


2  

If you want to re-render components that are inside your render method, place the data you are using for those components in a state object:

如果要重新渲染渲染方法中的组件,请将用于这些组件的数据放在状态对象中:

state = {
 myStatus: status
}

Then call your data in your render method from State:

然后从State调用您的数据中的数据:

render() {
   if(this.state.myStatus === 1){
      return (<X/>)
   } else {
      return (<X/>)
   }
}

Then to re-render this data, just call setState:

然后重新渲染这些数据,只需调用setState:

this.setState({
 myStatus: 1
});

This will automatically re-render. Call setState whereever you are calling your data. Hope this helps!

这将自动重新渲染。在调用数据时调用setState。希望这可以帮助!

#1


2  

If you want to re-render components that are inside your render method, place the data you are using for those components in a state object:

如果要重新渲染渲染方法中的组件,请将用于这些组件的数据放在状态对象中:

state = {
 myStatus: status
}

Then call your data in your render method from State:

然后从State调用您的数据中的数据:

render() {
   if(this.state.myStatus === 1){
      return (<X/>)
   } else {
      return (<X/>)
   }
}

Then to re-render this data, just call setState:

然后重新渲染这些数据,只需调用setState:

this.setState({
 myStatus: 1
});

This will automatically re-render. Call setState whereever you are calling your data. Hope this helps!

这将自动重新渲染。在调用数据时调用setState。希望这可以帮助!