【问题标题】:Dynamic rightComponent in React navigationReact 导航中的动态 rightComponent
【发布时间】:2023-04-07 07:14:01
【问题描述】:

我想在我的 react-navigation 标题栏中添加一个动态的 rightComponent。可以在屏幕的 navigationOptions 中将正确的组件设置为按钮,但在用户与页面交互时无法动态更改按钮的 disabled-prop。

我尝试过的:

static navigationOptions = ({navigation}) => {
    return {
        headerTitle: "Pick images",
        headerRight:
            <Button
                title={"next"}
                type={"clear"}
                onPress={() => {navigation.navigate('Route')}}
                disabled={this.state.canContinue}
            />
    };

};

static navigationOptions = ({navigation}) => {
    return {
        headerTitle: "Pick images",
        headerRight:
            <Button
                title={"next"}
                type={"clear"}
                onPress={() => {navigation.navigate('Route')}}
                disabled={this.canContinue()}
            />
    };
};
constructor(props){
    super(props);
    this.canContinue = this.canContinue.bind(this);
}
canContinue(){
//just for testing
return true;
}

它向我抛出了未定义的错误。你们中有人挑战过同样的问题并得到了很好的解决方案吗?

干杯!

【问题讨论】:

    标签: reactjs react-native react-navigation


    【解决方案1】:

    您不能直接使用它。您需要将其保存在导航参数中。

    static navigationOptions = ({navigation}) => {
        return {
            headerTitle: "Pick images",
            headerRight:
                <Button
                    title={"next"}
                    type={"clear"}
                    onPress={() => {navigation.navigate('Route')}}
                    disabled={navigation.state.params?navigation.state.params.canContinue():false}
                />
        };
    };
    
    componentDidMount() {
      // set handler method with setParams
      this.props.navigation.setParams({ 
        canContinue: this.canContinue.bind(this),
      });
    }
    

    【讨论】:

    • 嗨!谢谢你的回答。我已经以稍微不同的形式尝试过。您的解决方案给了我错误“未定义不是对象”。我对其进行了如下修改:disabled={navigation.state.params? navigation.state.params.canContinue():false} 现在它可以工作了。上次我试图弄清楚时没有找到这个解决方案哈哈,谢谢!
    • 这是未定义的,因为它仅在组件渲染后才绑定(ComponentDidMount)所以最初你得到它未定义,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2022-12-19
    • 1970-01-01
    • 2011-01-26
    • 2013-03-08
    • 1970-01-01
    相关资源
    最近更新 更多