Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs componentWillMount中的状态未更新_Reactjs_React Native - Fatal编程技术网

Reactjs componentWillMount中的状态未更新

Reactjs componentWillMount中的状态未更新,reactjs,react-native,Reactjs,React Native,当我试图从componentwillmount函数中的api调用更新react组件的状态时,它没有按预期工作。未设置该值 export default class ListOfProducts extends Component { componentWillMount() { console.log('component currently mounting'); fetchOrder().then(data => {

当我试图从componentwillmount函数中的api调用更新react组件的状态时,它没有按预期工作。未设置该值

export default class ListOfProducts extends Component {
        componentWillMount() {
            console.log('component currently mounting');
            fetchOrder().then(data => {
                console.log('order api call has been finished.', data);
                this.setState(data, function() {
       //this should print new data but i am still getting old data 
                    console.log('current this.state.', this.state.orders)
                })
            })
        }
        constructor(props) {
            super(props);

            this.state = {
                "orders": {
                    "order_items": []
                }
            };
        }
        render() {
            let productList = [];
            let productUnit = (
                <View>
          {this.state.orders.order_items.map(function(order,i){
            return <ProductListItem
              delivered={true}
              productSKU={3}/>
          })}
          </View>
            );
            return productUnit;
        }
    }
导出产品扩展组件的默认类列表{
组件willmount(){
log(“当前正在安装的组件”);
fetchOrder()。然后(数据=>{
log('订单api调用已完成',数据);
this.setState(数据,函数(){
//这应该打印新数据,但我仍然得到旧数据
console.log('current this.state',this.state.orders)
})
})
}
建造师(道具){
超级(道具);
此.state={
“命令”:{
“订单项目”:[]
}
};
}
render(){
让productList=[];
设productUnit=(
{this.state.orders.order\u items.map(函数(order,i){
返回
})}
);
退货单位;
}
}

如果您想执行任何异步请求,我建议您在
componentDidMount
生命周期方法中执行它们。这是基于的建议路径

仅在客户端(而不是服务器)上调用一次,然后立即调用 在初始渲染发生后。在生命周期的这一点上, 您可以访问您孩子的任何引用(例如,访问 底层DOM表示)。的componentDidMount()方法 子组件在父组件之前被调用

如果您想与其他JavaScript框架集成,请设置计时器 使用setTimeout或setInterval,或发送AJAX请求,执行这些操作 此方法中的操作


也许你需要改变: “扩展组件{” 到
“扩展React.Component{”

我不确定,但我认为您可以直接在componentWillMount中修改状态,因为组件尚未呈现,因此调用setState或调用setState是ComponentDidMount是没有意义的,即使这样。state=data不是working@RakeshYadav您不应该手动将任何值设置为
this.state
,因为它们将被覆盖dden当调用
setState()
@Barry127时,可以直接在componentWillMount中更新状态。请参见:componentWillMount()这是我们处理配置、更新状态以及通常准备第一次渲染的机会。此时,定义了props和初始状态。我们可以安全地查询this.props和this.state,确信它们是当前值。发件人:您好,欢迎使用StackOverflow。当您发布答案时,请相信它会解决问题问题。如果你不确定,仍然想发表你的想法,试着描述一下为什么你认为它可以解决问题。祝你好运!