React native 是否在navigationOption中获取状态值?
我有一个状态(currentLocation)中的值,我想将其保留在导航栏的右侧。我该怎么做? navigationOptions中不允许使用this.state.currentLocation&给定undefined不是对象(计算“\u this4.state.currentLocation”)错误React native 是否在navigationOption中获取状态值?,react-native,React Native,我有一个状态(currentLocation)中的值,我想将其保留在导航栏的右侧。我该怎么做? navigationOptions中不允许使用this.state.currentLocation&给定undefined不是对象(计算“\u this4.state.currentLocation”)错误 构造函数(道具){ 超级(道具); (this.state={ 当前位置:空 }); 这是。_getCurrentLocation(); } 静态导航选项=({navigate,navigatio
构造函数(道具){
超级(道具);
(this.state={
当前位置:空
});
这是。_getCurrentLocation();
}
静态导航选项=({navigate,navigation})=>({
标题:“搜索”,
头灯:(
{this.state.currentLocation}//这无法完成,如何在headerRight中获取currentLocation值?
)
});
_getCurrentLocation=async()=>{
这是我的国家({
currentLocation:等待AsyncStorage.getItem(“currentLocation”)
});
};
我不确定,因为我目前没有什么可测试的,但您应该能够检索如下状态值:
import { store } from "./App"; // Where you create your redux store
const currentLocation = store.getState().currentLocation;
解决方案之一是使用
componentDidMount(){
这是。_getCurrentLocation()
}
静态导航选项=({navigate,navigation})=>({
标题:“搜索”,
头灯:(
{navigation.state.params&&navigation.state.params.currentLocation}
)
});
_getCurrentLocation=async()=>{
const currentLocation=await AsyncStorage.getItem(“currentLocation”)
this.setState({currentLocation},()=>this.props.navigation.setParams({currentLocation}));
};
hi,它给出了undefined不是对象(评估navigation.state.params.currentLocation)错误。但是使用const{params={}}=navigation.state;和params.currentLocation workedUpdated,因为它最初未定义。
import { store } from "./App"; // Where you create your redux store
const currentLocation = store.getState().currentLocation;
componentDidMount () {
this._getCurrentLocation()
}
static navigationOptions = ({ navigate, navigation }) => ({
title: 'Search',
headerRight: (
<View>
<Text>{navigation.state.params && navigation.state.params.currentLocation}</Text>
</View>
)
});
_getCurrentLocation = async () => {
const currentLocation = await AsyncStorage.getItem("currentLocation")
this.setState({currentLocation}, () => this.props.navigation.setParams({currentLocation}));
};