【问题标题】:React Native - Rerunning the render methodReact Native - 重新运行渲染方法
【发布时间】:2020-04-01 16:41:41
【问题描述】:

我在这里有一个文件,它定义了标题的图标。

    static navigationOptions = ({ navigation }) => {
        return {
            headerRight: () => (<HomeHeaderIcon/>)
        }
    };

HomeHeaderIcon.js

export default class HomeHeaderIcon extends Component {
    async componentDidMount(){
        const token = await AsyncStorage.getItem('token');
        this.setState({token});
    }
    state={
        token:null
    };

    render() {
        return (
            <View>
                {
                    this.state.token ===null
                        ?
                        (
                            <TouchableOpacity
                                onPress={() => (NavigationService.navigate("LogStack"))}>
                                <Icon name="ios-power" size={30} style={{color: "white",marginRight:wp("5%")}}/>
                            </TouchableOpacity>
                        )
                        :
                        (
                            <TouchableOpacity
                                onPress={() => (NavigationService.navigate("Profile"))}>
                                <Icon name="ios-home" size={30} style={{color: "white",marginRight:wp("5%")}}/>
                            </TouchableOpacity>)
                }
            </View>
        );
    }
}

系统完全按照我的意愿运行。如果有令牌,我说 icon1 或 icon2 显示。问题是我在componentDidMount中这样做,图标在不刷新页面的情况下不会改变。如何重新渲染?

【问题讨论】:

    标签: react-native dynamic react-lifecycle


    【解决方案1】:

    componentDidMount 顾名思义,只在组件安装时调用一次。使用componentDidUpdate 根据propsstate 的哪一部分发生了变化来决定组件的行为方式。

    阅读documentation,了解有关生命周期方法的更多信息。

    【讨论】:

    • 虽然他没有直接拿去解,但是他解释了不间接的原因。非常感谢。
    猜你喜欢
    • 2019-09-26
    • 2018-12-27
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多