简体   繁体   English

状态更新,但React组件没有

[英]State updates but react component does not

I have the next react code inside a render function: 我在渲染函数中有下一个反应代码:

         {this.state.info}
         <MyComponent index={0}
              selectedInfo={this.state.info} 
         />

if I pass a string to MyComponent like: 如果我将字符串传递给MyComponent:

         <MyComponent index={0}
              selectedInfo={"Hello"} 
         />

then I will see Hello. 然后我会看到你好。 However, if I use: 但是,如果我使用:

         {this.state.info}
         <MyComponent index={0}
              selectedInfo={this.state.info} 
         />

I will see nothing even if {this.state.info} prints Hello (ie this.state.info == 'Hello'). 即使{this.state.info}打印Hello(即this.state.info =='Hello'),我也什么也看不见。 I suspect this is a problem related with /react's rendering system but i have no idea how to trace this. 我怀疑这是与/ react的渲染系统有关的问题,但我不知道如何跟踪此问题。

In the beginning this.state.info is ''. 开头,this.state.info是“”。 After an axios call that takes like 2-3 seconds this state is updated but MyComponent does not reflect that change even when {this.state.info} shows hello (ie this.state.info got updated by the axios call using setState) 在进行2-3秒钟的axios调用后,此状态会更新,但即使{this.state.info}显示问候(例如,this.state.info由axios调用使用setState更新)时,MyComponent也不会反映该更改。

Any suggestions? 有什么建议么? Thx 谢谢

This probably happens because a component doesn't render when props change, but only when its state changes. 发生这种情况的原因可能是,组件在props更改时不呈现,而仅在其状态更改时才呈现。

A solution may be to check for a change in props in componentWillReceiveProps, and to call setState if render should be done. 一个解决方案可能是检查componentWillReceiveProps中道具的更改,并在应执行渲染时调用setState。

Is not easy to say what exact problem is cause you didnt post enough code...but here is a working example, i believe doing what you trying to do...ultimately like other guys have said, just need to update state via "this.setState()" and listening components will pick up the update... 很难说出什么确切问题是因为您没有发布足够的代码...但是这里是一个有效的示例,我相信您正在尝试做的事情...最后就像其他人说的那样,只需要通过“ this.setState()”和侦听组件将获取更新...

 const MyComponent = ({selectedInfo, index}) => { return ( <div style={{border: '1px solid black', padding: '10px'}}> <h4>{selectedInfo}</h4> <p>{index}</p> </div> ) } class App extends React.Component{ constructor(props){ super(props); this.state = {selectedInfo: 'intial', count: 0}; this.btnPressed = this.btnPressed.bind(this); } btnPressed(){ this.setState({selectedInfo: 'button pressed => '+this.state.count, count: this.state.count+1}); } render() { return (<div> <MyComponent index={0} selectedInfo={this.state.selectedInfo}/> <button type="btn" onClick={this.btnPressed}>State Change working</button> </div>) } } console.log(document.getElementById('app')); ReactDOM.render(<App />, document.getElementById('app')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM