为什么我的 this.props.navigation.setParams 不起作用? [英] Why is my this.props.navigation.setParams not working?

查看:151
本文介绍了为什么我的 this.props.navigation.setParams 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 selectedStyleIds 上设置我的整数数组.为什么我的 this.props.navigaion.setParams 不起作用?

I'm setting my integer array on selectedStyleIds. Why is my this.props.navigaion.setParams not working?

  _setSelectedStyleIds = (selectedStyleIds) => {
    const action = NavigationActions.setParams({ 
        params: {selectedStyleIds},
        key: 'id-1509842157447-6' <- got this from console
    });
    this.props.navigation.dispatch(action);
    this.props.navigation.setParams({styleIds:selectedStyleIds});
    this.setState({selectedStyleIds});
  }

onCheck = ({selectedStyleIds}) => {
    console.log('onCheck');
    console.log(selectedStyleIds); <- [1,2,3,4,5]
    this.props._setSelectedStyleIds(selectedStyleIds);
    this.setState({selectedStyleIds}); <- working
}



_handleTagStylePress = () => {
    this.props.navigation.navigate('TagStyle', {onCheck: this.onCheck, selectedStyleIds: this.state.selectedStyleIds});
}

onNavigatioOptions(仅用于调试)

on onNavigatioOptions (just for debugging)

onPress={() => {
          let {image, text, ..., selectedSeasonIds,
            gender, selectedSizeIds, selectedColorIds, selectedStyleIds,
            brand, ..., base64 } = navigation.state.params;
          console.log('onsave')
          console.log(navigation.state.params.selectedStyleIds) <<-- []  

我已经检查这些行超过 50 次,但显然 this.props.navigation.setParams({selectedStyleIds}); 不起作用.

I've been checking these lines more than 50 times but apparently this.props.navigation.setParams({selectedStyleIds}); is not working.

所有其他设置状态和参数都没有问题,但只有 selectedStyleIds 不能设置为 navigation.state.params.

All other things have no problem on setting state and params but ONLY the selectedStyleIds can't be set to navigation.state.params.

推荐答案

我遇到了类似的问题,关于从组件内部设置导航栏的标题.

I had a similar problem, regarding setting the title of the navigation bar from within the component itself.

以下对我有用.

1)我定义了navigationOptions方法(react导航设置导航属性的回调方法)

1) I defined the navigationOptions method (callback method of react navigation to set navigation properties)

当此方法运行时,您很可能无法访问设置导航器状态所需的属性,因此只需返回当前参数,我就是这样做的:

When this method runs, you are very likely not to have access to the properties that are needed to set the navigator state, so simply return the current parameters, this is what I did:

static navigationOptions = ({ navigation }) => {
    const { params } = navigation.state;
    return params;
};

2) 我在 componentDidMount 中实现了标题构造,设置了状态:

2) I implemented the title construction in componentDidMount, setting the state:

componentDidMount(){
    t = "The window title";
    this.props.navigation.setParams({title: t });
}

我认为同样可以在任何用户界面事件中完成,例如 onPress,并且可以应用于任何属性.

I think the same could be done in any user interface event, like the onPress, and can be applied to any property.

重要说明:对我来说不工作工作的区别在于navigationOptions 方法,即使它是一个什么都不做"的方法(返回它得到的东西,不变)

Important note: What made the difference to me between not working and working was the definition of the navigationOptions method, even if it is a "do-nothing" method (returns what it gets, unchanged)

这篇关于为什么我的 this.props.navigation.setParams 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆