React native 在react native中呈现之前,数据不可用
我正在尝试从应用程序开始时的永久存储中提取数据,然后使用此数据运行登录功能,但当我尝试运行该功能时,数据不可用。 例如:React native 在react native中呈现之前,数据不可用,react-native,React Native,我正在尝试从应用程序开始时的永久存储中提取数据,然后使用此数据运行登录功能,但当我尝试运行该功能时,数据不可用。 例如: import store from 'react-native-simple-store' class ExampleScreen extends Component { constructor(props) { super(props) this.state = { username: "", password: ""
import store from 'react-native-simple-store'
class ExampleScreen extends Component {
constructor(props) {
super(props)
this.state = {
username: "",
password: ""
}
componentWillMount(){
try {
store.get('User').then((User) => this.setState({username: User}));
store.get('Pass').then((Pass) => this.setState({password: Pass}));
} catch(error){
alert(error)
};
this.onSubmitPress;
}
onSubmitPress = async () => {
alert(this.state.username);
alert(this.state.password);
}
}
函数返回开始时设置的空状态
如何使用渲染前获得的数据运行函数
Edit1:在呈现登录屏幕之前,我需要在呈现之前运行登录函数的数据。
我尝试在ComponentDidMount上运行,但在渲染过程开始之前,数据不可用。我可以将this.state.username渲染为渲染中的文本您不能保证在渲染之前可以获得数据。您只能在上一个屏幕中获取数据,或在当前屏幕中显示ActivityIndicator以等待。就像每个人都共享的一样,您可能希望在componentWillMount之后调用您的函数在componentDidMount期间调用。不确定为什么需要数据进行渲染,但我假设需要基于永久存储来渲染UI 根据您的编辑,我尝试在ComponentDidMount上运行,但在渲染过程开始之前,数据不可用。该语句不正确,因为生命周期如下所示: componentWillMount>render>componentDidMount。 也就是说,UI已经呈现。然后您的函数将在componentDidMount上启动 如果在componentDidMount中看不到数据,请调试以验证数据是否存在于永久存储器中
旁注:应考虑停止使用。 该名称将继续使用到第17版
为什么在安装组件之前需要这些数据?在Willmount中,您可以从DidMount使用它吗?我是说,你测试过它是否有负载吗?