[英]React setState odd behaviour
New to React, in the following code I am passing data between two components via the parent. React的新手,在以下代码中,我通过父级在两个组件之间传递数据。 Flow is from Search
to the parent App
then to another child Sidebar
. 流程是从Search
到父App
再到另一个子Sidebar
。 I am able to send to both from Search to App and App to Sidebar individually but for some reason setState
is not behaving as expected making the link to trigger a refresh of <Search updateMenu={this.handleSearchResult} />
as you can see in the console.log
code comments below: 我能够分别从Search to App和App到Sidebar发送到同一位置,但是由于某种原因, setState
的行为不符合预期,因此该链接触发了刷新<Search updateMenu={this.handleSearchResult} />
如您所见下面的console.log
代码注释:
import React, { Component } from 'react';
import './App.css';
import Search from './Search';
import Sidebar from './Sidebar';
class App extends Component {
constructor() {
super()
this.state = {
menu: []
}
}
handleSearchResult = (array) => {
// always the correct value
console.log('in ', array);
this.setState( {menu: menuList})
// 1st call : empty
// 2nd call : previous value not showing on 1st call + new value as expected
// does not trigger <Sidebar list={this.state.menu}/>
console.log('out', this.state.menu);
}
render() {
return (
<div className="App">
// not refreshing
<Search updateMenu={this.handleSearchResult} />
<Sidebar list={this.state.menu}/>
</div>
);
}
}
export default App;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.