As in this fiddle http://jsfiddle.net/69z2wepo/3535/ this.setState
is called in componentWillMount
with callback this.loadData
. But the function this.loadData
is never called as callback.
I don't know how the internals work here, but looking at the docs for setState()
...
...you can supply an optional callback function that is executed once
setState
is completed and the component is re-rendered.
...and the docs for componentWillMount()
...
If you call
setState
within this method,render()
will see the updated state and will be executed only once despite the state change.
...I assume it's related to the fact that calling setState()
in render()
doesn't queue up a re-render.
Given that, the most appropriate place to put an initial call to loadData
would be componentDidMount()
, which fires immediately after the initial render:
componentWillMount() {
// ...
this.setState({loaded, data});
},
componentDidMount() {
this.loadData();
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.