简体   繁体   English

React Native / Redux-只能更新已安装或正在安装的组件

[英]React Native / Redux - Can only update a mounted or mounting component

I am just fetching the items from server and displaying them in the list component. 我只是从服务器获取项目并将其显示在列表组件中。 The component structure is like 组件结构像

ServiceScreen(parent) -> ServicesList(child- used inside ServiceScreen) -> ServiceItem (child used inside ServicesList)

Every thing works well and it displays the services fetched but it gives me the following warning 一切正常,它显示获取的服务,但它给我以下警告

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 YellowBox component. 请检查YellowBox组件的代码。

Code is as follows 代码如下

ServiceScreen.js ServiceScreen.js

constructor(props) {
    super(props);
    this.props.actions.fetchServices();
  }
    render() {
        const { isFetching } = this.props;
        return (
          <View style={styles.container}>
            <ServicesList services={this.props.services} navigation={this.props.navigation} />
          </View>
        );
      }

ServicesList.js ServicesList.js

render() {
    return (
      <View style={{ flex: 1 }}>
        <FlatList
            data={this.props.services}            
            renderItem={
                ({ item }) => 
                <ServiceItem navigation={this.props.navigation} item={item} />
            }
        />
      </View>
    );
  }

ServiceItem.js ServiceItem.js

render() {
        const { item } = this.props;
        return (
            <TouchableOpacity onPress={this.singleService}>
                <Text>{item.service_name}</Text>
            </TouchableOpacity>
        );
    }

As I am using redux for state management, I have mapped the services state to my ServiceScreen component. 在使用redux进行状态管理时,我已将服务状态映射到我的ServiceScreen组件。 And I am passing down it to child component. 我将其传递给子组件。

You should dispatch the action in componentDidMount. 您应该在componentDidMount中调度操作。 The constructor is called before the component is mounted, which is why you're seeing the error. 在装入组件之前调用了构造函数,这就是为什么看到错误的原因。

Here's a diagram with the react lifecycle. 这是带有反应生命周期的图表。 You should only call methods that change state in the "commit" phase. 您只应在“提交”阶段调用更改状态的方法。 在此处输入图片说明

This error is firing exactly when said in the yellowBox. 当在yellowBox中说时,此错误正好触发。 Somewhere you trying to update already unmounted component. 您尝试在某个地方更新已卸载的组件。 I handled this issue by using temporary variable componentIsMounted: bool and wrapping setState method: 我通过使用临时变量componentIsMounted: bool和包装setState方法处理了此问题:

class SomeClass extends Component {
  constructor(props) {
    this._isComponentMounted = false;
  }

  componentDidMount() {
    this._isComponentMounted = true;
  }

  componentWillUnmount() {
    this._isComponentMounted = false;
  }

  setState(...args) {
    if (!this._isComponentMounted) {
      return;
    }

    super.setState(...args);
  }
}

Also, you should remember that you should not call state updates before the component get mounted (it`s not an alternative way - just an addition). 另外,您应该记住,在安装组件之前,请勿调用状态更新(这不是替代方法,只是附加方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应本机错误:只能更新已安装或正在安装的组件 - React Native Error: Can only update a mounted or mounting component 反应母语。 只能更新已安装或正在安装的组件 - React-Native. Can only update a mounted or mounting component 反应:只能更新已安装或正在安装的组件 - React: Can only update a mounted or mounting component 警告:在setInterval(React Native)中调用api时只能更新已安装或正在安装的组件 - Warning: Can only update a mounted or mounting component while calling api inside setInterval (React Native) React setState只能更新已安装或安装的组件 - React setState can only update a mounted or mounting component React setstate只能在重新渲染时更新已安装或安装的组件 - React setstate can only update a mounted or mounting component — on rerender React JS setState(…):只能更新已安装或正在安装的组件 - React JS setState(…): Can only update a mounted or mounting component React-setState(…)只能更新已安装或正在安装的组件 - React - setState(…) Can only update a mounted or mounting component React-setState(…):只能更新已安装或正在安装的组件 - React - setState(…): Can only update a mounted or mounting component 反应警告:setState(…)只能更新已安装或正在安装的组件 - React Warning: setState(…) Can only update a mounted or mounting component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM