最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Navigation: 'navigation.state.params' is always undefined in 'static navigationOption

programmeradmin3浏览0评论

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 'console.log' statement returns the navigation object plete with "navigation.state.params" containing the params that I have passed into it.

However, the second 'console.log' returns the 'navigation.state' object but for some reason the 'params' are undefined.

Here is how I am setting the navigation params (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-redux 5.0.5
react-navigation 1.0.0-beta.11

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 'console.log' statement returns the navigation object plete with "navigation.state.params" containing the params that I have passed into it.

However, the second 'console.log' returns the 'navigation.state' object but for some reason the 'params' are undefined.

Here is how I am setting the navigation params (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-redux 5.0.5
react-navigation 1.0.0-beta.11

Share Improve this question edited Aug 3, 2017 at 7:58 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Aug 1, 2017 at 2:28 philphil 5376 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Used like this

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" });

May be this can help you , Thanks

It is possible to go around this issue by implementing a method in the ponent class.

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

This will give you access to the parameters. But it does not work for nested objects.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论