简体   繁体   English

React Native:setState +已卸载或正在安装的组件

[英]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. 方法二: HomeScreencomponentDidMount生命周期方法内调用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.

相关问题 React - 未安装组件上的 setState() - React - setState() on unmounted component 警告:无法在React Native中的已卸载组件上调用setState(或forceUpdate) - Warning: Can't call setState (or forceUpdate) on an unmounted component in React Native 如何避免警告:无法在未安装的组件上设置状态? 反应本机 - How to avoid warning: Cannot setState on unmounted component? REACT-NATIVE React Native组件未安装 - React Native component not mounting React,setState警告已安装/未安装的组件 - React, setState warning for mounted/unmounted component 如何解决:无法在 React-Native 中的未挂载组件警告上调用 setState(或 forceUpdate)? - How to solve: Can't call setState(or forceUpdate) on an unmounted component warning in React-Native? React 组件渲染但未安装,因此无法设置状态 - React Component rendering but not mounting thus unable to setState 只能更新已安装或安装的组件。 这通常意味着您在已卸载的组件上调用了setState() - Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component setState(...):只能更新已安装或安装的组件。 这通常意味着您在已卸载的组件上调用了setState()。 这是一个无操作 - setState(…): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op 如何测试依赖setState的已卸载React组件的方法? - How to test an unmounted React component's method that relies on setState?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM