我目前正在使用 React 16.3(React Native),写在这里,它表明我应该在 componentDidMount 而不是 componentWillMount 内发出任何异步请求,因为它很快就会被弃用。
不幸的是,当我试图在 componentDidMount 中获取数据时,我收到了一个无操作警告,将从我的 axios 请求返回的数据设置为我的状态。
这是一个片段-
export default class MyComponent extends Component {
state = {
myData: []
}
componentDidMount() {
axios.get('api-endpoint')
.then(res => this.setState({ myData: res.data })
}
render() { return <View>...</View> }
}
和警告——
Warning: Can only update a mounted or mounting component.
This usually means you called setState, replaceState, or
forceUpdate on an unmounted component. This is a no-op.
Please check the code for the MyComponent component.