加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

reactjs – react-native组件生命周期方法不会在导航上触发

发布时间:2020-12-15 05:04:50 所属栏目:百科 来源:网络整理
导读:我遇到一个问题,我在多个组件中基于AsyncStorage中的相同键设置了State.由于状态是在componentDidMount中设置的,并且这些组件不一定在导航时卸载和装载,因此状态值和AsyncStorage值可能不同步. 这是我能做的最简单的例子. 组件A. 一个只是设置导航和应用程序
我遇到一个问题,我在多个组件中基于AsyncStorage中的相同键设置了State.由于状态是在componentDidMount中设置的,并且这些组件不一定在导航时卸载和装载,因此状态值和AsyncStorage值可能不同步.

这是我能做的最简单的例子.

组件A.

一个只是设置导航和应用程序.

var React = require('react-native');
var B = require('./B');

var {
    AppRegistry,Navigator
} = React;

var A = React.createClass({
    render() {
        return (
            <Navigator
                initialRoute={{
                    component: B
                }}
                renderScene={(route,navigator) => {
                    return <route.component navigator={navigator} />;
                }} />
        );
    }
});

AppRegistry.registerComponent('A',() => A);

组件B.

B在mount上从AsyncStorage读取,然后设置为state.

var React = require('react-native');
var C = require('./C');

var {
    AsyncStorage,View,Text,TouchableHighlight
} = React;

var B = React.createClass({
    componentDidMount() {
        AsyncStorage.getItem('some-identifier').then(value => {
            this.setState({
                isPresent: value !== null
            });
        });
    },getInitialState() {
        return {
            isPresent: false
        };
    },goToC() {
        this.props.navigator.push({
            component: C
        });
    },render() {
        return (
            <View style={{ flex: 1,alignItems: 'center',justifyContent: 'center' }}>
                <Text>
                    {this.state.isPresent
                        ? 'Value is present'
                        : 'Value is not present'}
                </Text>

                <TouchableHighlight onPress={this.goToC}>
                    <Text>Click to go to C</Text>
                </TouchableHighlight>
            </View>
        );
    }
});

module.exports = B;

组件C.

C从AsyncStorage读取与B相同的值,但允许您更改该值.更改切换状态和AsyncStorage中的值.

var React = require('react-native');
var {
    AsyncStorage,TouchableHighlight
} = React;

var C = React.createClass({
    componentDidMount() {
        AsyncStorage.getItem('some-identifier').then(value => {
            this.setState({
                isPresent: value !== null
            });
        });
    },toggle() {
        if (this.state.isPresent) {
            AsyncStorage.removeItem('some-identifier').then(() => {
                this.setState({
                    isPresent: false
                });
            })
        } else {
            AsyncStorage.setItem('some-identifier','some-value').then(() => {
                this.setState({
                    isPresent: true
                });
            });
        }
    },goToB() {
        this.props.navigator.pop();
    },justifyContent: 'center' }}>
                <Text>
                    {this.state.isPresent
                        ? 'Value is present'
                        : 'Value is not present'}
                </Text>

                <TouchableHighlight onPress={this.toggle}>
                    <Text>Click to toggle</Text>
                </TouchableHighlight>

                <TouchableHighlight onPress={this.goToB}>
                    <Text>Click to go back</Text>
                </TouchableHighlight>
            </View>
        );
    }
});

module.exports = C;

如果在C中切换然后返回到B,则B中的状态和AsyncStorage中的值现在不同步.据我所知,navigator.pop()不会触发我可以用来告诉B刷新值的任何组件生命周期函数.

我所知道的一个解决方案,但并不理想,是让B的状态成为C的支柱,并给C一个回调道具来切换它.如果B和C总是直接是父和子,那么这将很有效,但在真实应用中,导航层次结构可能更深.

无论如何在导航事件之后触发组件上的功能,或者我缺少的其他东西?

和你一样陷入同样的??问题.我希望这会改变,或者我们在组件上获得额外的事件来监听(componentDidRenderFromRoute)或类似的东西.无论如何,我如何解决它是将我的父组件保留在范围内,因此子导航栏可以调用组件上的方法.我正在使用: https://github.com/Kureev/react-native-navbar,但它只是Navigator的一个包装器:
class ProjectList extends Component {
  _onChange(project) {
    this.props.navigator.push({
      component: Project,props: { project: project },navigationBar: (<NavigationBar
        title={project.title}
        titleColor="#000000"
        style={appStyles.navigator}
        customPrev={<CustomPrev onPress={() => {
          this.props.navigator.pop();
          this._sub();
        }} />}
      />)
    });
  }
}

我正在推动一个带有道具数据的项目组件,并附加了我的导航栏组件. customPrev是react-native-navbar将替换为其默认值的内容.所以在新闻中,我调用pop,并在我的ProjectList实例上调用_sub方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读