[英]React Native: setState + unmounted or mounting component
I am trying to conditionally display either a Home or Slider component in the screen below, but when the onDone function runs, i am getting the error: 我试图在下面的屏幕中有条件地显示Home或Slider组件,但是当onDone函数运行时,出现了错误:
Warning: Can only update a mounted or mounting component. 警告:只能更新已安装或正在安装的组件。 This usually means you called setState, replaceState, or forceUpdate on an unmounted component.
这通常意味着您在未安装的组件上调用了setState,replaceState或forceUpdate。 This is a no-op.
这是无人值守。 Please check the code for the Onboarding component.
请检查入职组件的代码。
The Onboarding component is inside the Slider (react-native-onboarding-swiper - used for app intro)... Onboarding组件位于Slider内部(react-native-onboarding-swiper-用于应用程序简介)...
export default class HomeScreen extends Component {
static navigationOptions = {
headerStyle: {
backgroundColor: 'skyblue',
elevation: 0,
borderBottomWidth: 0,
},
headerLeft: null,
};
state = {
introLoaded: false,
};
async componentDidMount() {
const value = await AsyncStorage.getItem('@SKIP_INTRO');
if (value !== null) {
this.onDone();
}
};
onDone = async () => {
await this.setState({ introLoaded: true });
};
render() {
return this.state.introLoaded ? (
<Home navigation={this.props.navigation} />
) : (
<Slider onDone={this.onDone} />
);
}
}
Any help appreciated... 任何帮助表示赞赏...
Slider.js Slider.js
import React from 'react';
import { Image, Text } from 'react-native';
import PropTypes from 'prop-types';
import Onboarding from 'react-native-onboarding-swiper';
import styles from './styles';
const Slider = ({ onDone }) => (
<Onboarding
pages={[
{
backgroundColor: 'skyblue',
image: (
<Image source={require('../../assets/images/intro/pic1.png')} style={styles.image} />
),
title: <Text style={styles.title}>Title 1</Text>,
subtitle: <Text style={styles.subtitle}>Subtitle 1</Text>,
},
{
backgroundColor: 'skyblue',
image: (
<Image source={require('../../assets/images/intro/pic2.png')} style={styles.image} />
),
title: <Text style={styles.title}>Title 2</Text>,
subtitle: <Text style={styles.subtitle}>Subtitle 2</Text>,
},
]}
onDone={onDone}
/>
);
Slider.propTypes = {
onDone: PropTypes.func.isRequired,
};
export default Slider;
First setState
is not an asynchronous method . 首先
setState
不是异步方法 。 For more information read here . 欲了解更多信息, 请点击这里 。
Second in your approach HomeScreen
is calling method onDone
inside componentDidMount
lifecycle method as the component mounted it will automatically unload Slider
and just show error as you are changing state. 方法二:
HomeScreen
在componentDidMount
生命周期方法内调用onDone
方法,因为安装的组件将自动卸载Slider
并在更改状态时显示错误。
So, instead of using Onboarding
inside stateless component use it inside state component and use it in the Welcome Screen (the screen where user is not logged in and see for first time). 因此,不要在状态组件内部使用
Onboarding
,而应在状态组件内部使用它,并在“欢迎屏幕”(用户未登录并首次看到的屏幕)中使用它。 Once user logged in just navigate to the other screen so this welcome screen will not be visible to user again. 用户登录后,只需导航至另一个屏幕,这样该欢迎屏幕将不再对用户可见。 let me know if you need more information.
如果您需要更多信息,请与我们联系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.