如何将内部组件状态变为全局状态?
How to make inside component state to global state?
我用react native做了个天气app。
从 accuweather 获取天气数据并保存到状态。
我需要在子页面(第 1、2、3 页)中显示状态数据。
我怎么办?请帮助我。
你应该通过 props 将你的状态数据发送给另一个 components/pages。
在构造函数中定义您的状态。
constructor(props) {
super(props);
this.state = {
experienceId: '',
desktopDetail: this.props.navigation.state.params,
clientId: '',
installedVR: '',
activeVR: '',
};
}
在您的组件中的任意位置使用 this.state.clientId
访问您的变量。
由于您没有提供任何特定的代码,我只能假设您想将一些道具传递给页面。我在你的仓库中找到了这篇文章:
navigateToScreen = (route) => () => {
const navigateAction = NavigationActions.navigate({
routeName: route
});
this.props.navigation.dispatch(navigateAction);
}
此功能需要将道具传递给您要导航到的页面。
你可以这样做 this.props.navigation.navigate(route, {paramName: this.state.paramName});
在该页面上,您可以使用 this.props.navigation.getParam(paramName, defaultValue)
或 this.props.navigation.state.params
访问这些道具。
我用react native做了个天气app。 从 accuweather 获取天气数据并保存到状态。 我需要在子页面(第 1、2、3 页)中显示状态数据。
我怎么办?请帮助我。
你应该通过 props 将你的状态数据发送给另一个 components/pages。
在构造函数中定义您的状态。
constructor(props) {
super(props);
this.state = {
experienceId: '',
desktopDetail: this.props.navigation.state.params,
clientId: '',
installedVR: '',
activeVR: '',
};
}
在您的组件中的任意位置使用 this.state.clientId
访问您的变量。
由于您没有提供任何特定的代码,我只能假设您想将一些道具传递给页面。我在你的仓库中找到了这篇文章:
navigateToScreen = (route) => () => {
const navigateAction = NavigationActions.navigate({
routeName: route
});
this.props.navigation.dispatch(navigateAction);
}
此功能需要将道具传递给您要导航到的页面。
你可以这样做 this.props.navigation.navigate(route, {paramName: this.state.paramName});
在该页面上,您可以使用 this.props.navigation.getParam(paramName, defaultValue)
或 this.props.navigation.state.params
访问这些道具。