React Native填坑之旅--组件生命周期
这次我们来填React Native生命周期的坑。这一点非常重要,需要有一个清晰的认识。如果你了解Android或者iOS的话,你会非常熟悉我们今天要说的的内容。 基本上一个React Native的组件会经历三个阶段最终渲染在界面上,他们分别是:开始渲染、更新、卸载。 开始渲染:componentWillMountcomponentWillMount(): void 组件开始渲染的时候调用这个方法 componentDidMountcomponentDidMount(): void 组件的渲染完成之后调用这个方法。子组件的 更新componentWillReceivePropscomponentWillReceiveProps(nextProps: Object): void 当有新的Props发送给组件的时候,这个方法被触发。最开始的render并不会调用这个方法! 使用这个方法可以在更新state, 例如: componentWillReceiveProps(nextProps) { this.setState({ currentCategory: nextProps.category !== this.props.category ? nextProps.category : this.props.category }); } shouldComponentUpdateshouldComponentUpdate(nextProps: Object,nextState: Object): void 当收到新的props或者state的时候触发这个方法。默认情况下 componentWillUpdatecomponentWillUpdate(nextProps: Object,nextState: Object): void 在props或者state更新之后立即调用这个方法。这个方法不会在第一次render的时候调用。 renderrender(): ReactElement<{}> 当 componentDidUpdatecomponentDidUpdate(prevProps: Object,prevState: Object): void 每次组件更新之后调用。第一次render的时候不会调用。 卸载componentWillUnmount(): void 组件被卸载之后调用。可以在这个方法里执行一些清理操作。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |