【发布时间】:2025-12-19 17:35:12
【问题描述】:
我想将获取的道具中的部分数据(用户名)放在导航标题上。请看我的navigationOptions。
static navigationOptions = ({navigation, screenProps}) => ({
...
title: !_.isNil(screenProps.outfitDetail) ?
`${screenProps.outfitDetail.user.username}'s Post`: ''
});
我的问题是title 总是返回一个空白字符串,因为screenProps.outfitDetail 总是为空。我在这里做错了什么?为什么总是返回null?
我在componentWillMount() 上使用 Redux 获取了 suitDetail 道具
componentWillMount() {
const { id } = this.props.navigation.state.params;
const { token, hType} = this.props;
this.props.fetchOutfitDetail(token, hType, id);
}
我可以从render() function 那里获得outfitDetail 道具。
render () {
const detail = this.props.outfitDetail;
if(detail) {
return (
<ScrollView style={styles.root}>
...
我通过它的 id 调用我的组件
this.props.navigation.navigate('OutfitDetail', {id})
在用户资料上做同样的事情
console.log(this.props.currentUser.username); <- prints username
this.props.navigation.navigate('Profile', {username:this.props.currentUser.username});
但在个人资料屏幕中,也无法获取screenProps.username。
【问题讨论】:
-
你在哪里传递
screenProps?你在哪里设置outfitDetail?不看更多代码很难判断。 -
@Scott 感谢您的提问。我想我可以输入
screenProps来获取当前屏幕的道具。我应该把它传到某个地方吗?对于你的第二个问题,我在 mapStateToProps 上设置了装备细节。所以我可以从 render() 函数中获取对象。 -
这真的很难调试,因为我无法在 navigationOptions 上传递 console.log() :(
-
outfitDetail和fetchOutfitDetail是否被 Redux 容器注入? -
@Freez 是的,他们是。我认为 screenProps 没有连接到屏幕的道具。所以我想我必须把道具放在组件上。
标签: reactjs react-native react-redux react-navigation