React native 如何从Expo调用render方法中的异步方法?

React native 如何从Expo调用render方法中的异步方法?,react-native,expo,React Native,Expo,我将数据保存到异步存储。现在我想在单独的屏幕上显示来自异步存储的所有数据。 方法getData是异步方法。它从异步存储中读取数据。 我使用这样的代码 从“React”导入React; 类列表扩展了React.Component{ 状态={list:null}; 异步组件didmount(){ 常量列表=等待获取数据(“列表”); log('LIST:'+JSON.stringify(LIST)); this.setState({list}); } render(){ const{list}=t

我将数据保存到异步存储。现在我想在单独的屏幕上显示来自异步存储的所有数据。 方法getData是异步方法。它从异步存储中读取数据。 我使用这样的代码

从“React”导入React;
类列表扩展了React.Component{
状态={list:null};
异步组件didmount(){
常量列表=等待获取数据(“列表”);
log('LIST:'+JSON.stringify(LIST));
this.setState({list});
}
render(){
const{list}=this.state;
log('state:'+JSON.stringify(list));
if(list | | list.length(
{item}
))}
);
}

}
componentDidMount
正如标题所说,是为了在渲染后运行。要实现您想要做的事情,您可以使用
组件willmount

从“React”导入React;
类列表扩展了React.Component{
状态={list:null};
组件willmount(){
const loadData=async()=>{
常量列表=等待获取数据(“列表”);
log('LIST:'+JSON.stringify(LIST));
this.setState({list});
};
loadData();
}
render(){
const{list}=this.state;
log('state:'+JSON.stringify(list));
if(list | | list.length(
{item}
))}
);
}
}

谢谢你的想法,但不幸的是,它也有同样的效果。哦,很抱歉,我错过了阅读已编辑的消息,现在你将componentDidMount更改为componentWillMount。谢谢,我会试试的。是的,我就是这么做的