首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 ><Provider>不支持在使用componentWillMount或componentWillUnMount时动态更改“`store`”

<Provider>不支持在使用componentWillMount或componentWillUnMount时动态更改“`store`”
EN

Stack Overflow用户
提问于 2018-05-24 11:43:10
回答 1查看 374关注 0票数 1

不支持动态更改store。您很可能会看到此错误,因为您更新到Redux2.x并响应Redux2.x,不再自动重新加载热减速器

当我使用componentWillMount和componentWillUnMount时,我会得到这个错误

代码语言:javascript
复制
import React, { Component } from 'react';
import { View, NetInfo, Image } from 'react-native';
import { Container, Text } from 'native-base';
import { RootNavigator } from './src/root';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import resducers from './src/reducers/index';

export default class App extends Component<Props> {
  constructor(Props){
    super(Props);
    this.state={
      connection:null,
    }
  }

  componentWillMount(){
    NetInfo.isConnected.addEventListener("connectionChange",this.handleConnectionChange);
    NetInfo.isConnected.fetch().done((isConnected)=>this.setState({connection:isConnected}));
  }

  componentWillUnMount(){
    NetInfo.isConnected.removeEventListener("connectionChange",this.handleConnectionChange);
  }

  handleConnectionChange=(isConnected)=>{
    this.setState({connection:isConnected});
  }

  handeView(){
    if(this.state.connection!==null && this.state.connection){
      return <RootNavigator />
    }else {
      return <View style={{flex:1, flexDirection:"row", alignItems:"center", justifyContent:"center"}}>
         <Image source={require("./images/connection.gif")} style={{height: 150, width: 150, resizeMode : "stretch"}}/>
      </View>
    }
  }

  render() {
    return (
      <Provider store={createStore(resducers)}>
         <Container>
             {this.handeView()}
         </Container>
      </Provider>
    );
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-24 11:58:17

每次组件呈现时,都会创建一个新存储区。尝试在组件之外创建存储。

代码语言:javascript
复制
import resducers from './src/reducers/index';    
const store = createStore(resducers);

--

代码语言:javascript
复制
<Provider store={store}>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50508544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档