React Navigation: "navigation.state.params" is always undefined in 'static navigationOptions'

The heart of my problem is that when I have this in my navigationOptions:

static navigationOptions = ({ navigation }) => {
    console.log(navigation)
    console.log(navigation.state)
}

      

The first statement, console.log, returns a navigation object complete with "navigation.state.params" containing the parameters that I passed to it.

However the second "console.log" returns a "navigation.state" object, but for some reason "params" is undefined.

This is how I set the navigation options (from redux):

function mapStateToProps(state, props) {
    let sum = 0
    for (let product in state.cart) {
        sum += state.cart[product]
    }
    return props.navigation.state = {params: {cartSum: sum}}
}

      

And my developer environment:

node 6.10.1
react-native 0.46.4
redux 3.7.1
react-reduction 5.0.5
react-navigation 1.0.0-beta.11

+3


source to share


2 answers


Used like

static navigationOptions = ({ navigation }) => {
    const { params = {} } = navigation.state;
    console.log(params)**will show you the handlesave with object value hello**
};

this.props.navigation.setParams({ handleSave: "Heloo" });

      



Maybe this can help you, thanks

+2


source


You can work around this problem by implementing the method in the component class.

getNavigationParams() {
        return this.props.navigation.state.params || {};
    }

      



This will give you access to the parameters. But this doesn't work for nested objects.

-1


source







All Articles