【问题标题】:React Navigation - Can't get the header updatedReact Navigation - 无法更新标题
【发布时间】:2019-01-28 13:14:52
【问题描述】:

我的 react 本机应用程序中有以下代码,但在登录后导航到仪表板屏幕时无法更新屏幕标题。这很简单,但因为我不明白这里有什么问题,所以我发布了这个。

登录调用后,我导航到仪表板。

this.props.navigation.navigate('Dashboard', {
     title: 'Dashboard'
})

我的 DashboardScreen.js 如下。

这与 react 导航库的文档中的完全一样。

class DashboardScreen extends React.Component {
    static navigationOptions = ({navigation}) => {
        const {params} = navigation.state;

        let title = navigation.state.params.title ? navigation.state.params.title : 'This is Sparta!';

        return {
            title: title
        };
    };

    constructor(props) {
        super(props);
    }

    render() {
        return (
            <ScrollView>
                <Card>
                    <Text>Dashboard Screen</Text>
                </Card>
            </ScrollView>
        )
    }
}

我的基础 StackNavigator。

const StackNavigator = createStackNavigator({
    Login: {
        screen: LoginScreen
    },
    Dashboard: {
        screen: DashboardScreen
    }
}, {
    initialRouteName: "Login",
});

class App extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return (
            <StackNavigator/>
        )
    }
}

export default App;

我是 react native 的新手,所以 react 导航对我来说也是新的。尝试了各种方法,包括使用 navigationOptions 传递额外的道具。

有人能指出确切的问题吗?提前致谢。

【问题讨论】:

  • 能否分享您登录时的导航代码?
  • 问题中有。我在this.props.navigation.navigate('Dashboard', { title: 'Dashboard' }) 下面的代码中导航它会导航但不会更新标题。

标签: javascript reactjs react-native react-navigation


【解决方案1】:
class DashboardScreen extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            title: this.props.navigation.state.params.title
        };
    }

    render() {
        return (
            <ScrollView>
                <Card>
                    <Text>{this.state.title}</Text>
                </Card>
            </ScrollView>
        )
    }
}

希望对你有帮助。

【讨论】:

  • 谢谢,但是为什么在 render() 中使用它?这将如何更新标题?
  • 我只是用的例子。你想用什么地方都可以。
猜你喜欢
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2017-08-08
  • 2019-03-16
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2017-07-12
相关资源
最近更新 更多