React-Navigation----goBack()理解

the key property for goBack() is a dynamically created string, created by react-navigationwhenever navigate to a new route.
goBack()参数key为react-navigation动态分配的,而不是指定的routeName,所以当在调用goBack('routeName')是并不会返回到相应页面

借用图片

key 存储于this.props.navigation.state.key,当你想从EditPage 返回到Cover时,你需要做的将EditCover的key传递到EditPage。
为何传递的不是Cover的key 而是EditCover的key
这是因为 react-navigation提供的goBack()方法是从哪个key返回,而不是返回到哪个key。

Optionally provide a key, which specifies the route to go back from. By default, goBack will close the route that it is called from. If the goal is to go back anywhere, without specifying what is getting closed, call .goBack(null);

EditCover.js

render() {
    const { state, navigate } = this.props.navigation;    
    return (
        <View>
            <Button title="Go to Page" onPress={ () => {
                /* pass key down to *EditPage* */
                navigate('EditPage', { go_back_key: state.key });
            }} />
        </View>
    );
}

EditPage.js

render() {
    const { state, goBack } = this.props.navigation;    
    const params = state.params || {};

    return (
        <View>
            <Button title="Back to Cover" onPress={ () => {
                /* go back from *EditCover* to *Cover* */
                goBack(params.go_back_key);
            }} />
        </View>
    );
}

原文

https://stackoverflow.com/questions/45489343/react-navigation-back-and-goback-not-working

猜你喜欢

转载自blog.csdn.net/aexwx/article/details/79467133