React + Redux:“<provider>不支持动态更改`store`”

时间:2016-10-12 22:06:13

标签: javascript reactjs redux

我收到了这个错误:

  

<Provider>不支持动态更改store。您很可能会看到此错误,因为您已更新到Redux 2.x和React Redux 2.x,它们不再自动热重新加载Reducer。有关迁移说明,请参阅https://github.com/reactjs/react-redux/releases/tag/v2.0.0

我有一个组件:

import React, { Component } from 'react';
import {
  AppRegistry,
  NativeAppEventEmitter
} from 'react-native';
import { Provider } from 'react-redux';
import { createStore } from 'redux';
import reducers from './src/reducers';
import Onboarding from "./src/onboarding/Onboarding";
import Home from "./src/home/Home";
import codePush from 'react-native-code-push';

class Edmund extends Component {

  ...

  startScreen() {
    if (this.state.screen === "HOME" ) {
      return (<Home />);
    }

    return (
      <Onboarding />
    );
  }

  render() {
    return (
      <Provider store={ createStore(reducers) }>
        { this.startScreen() }
      </Provider>
    )
  }

AppRegistry.registerComponent('Edmund', () => Edmund)

我的src/reducers/index.js文件:

import { combineReducers } from 'redux';

export default combineReducers({
  libraries: () => []
});

我的包裹:

{
  "name": "Indigo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start"
  },
  "dependencies": {
    "lodash": "^4.13.1",
    "react": "^15.2.0",
    "react-native": "^0.27.1",
    "react-native-apple-pay": "0.0.0",
    "react-native-code-push": "^1.11.0-beta",
    "react-native-loading-spinner-overlay": "^0.3.0",
    "react-native-navigation": "^1.0.2",
    "react-native-paged-scroll-view": "^2.0.1",
    "react-native-progress": "^3.0.1",
    "react-redux": "latest",
    "redux": "^3.0.0",
    "underscore": "^1.8.3"
  },
  "devDependencies": {
    "eslint": "latest",
    "eslint-config-airbnb": "latest",
    "eslint-plugin-import": "^1.16.0",
    "eslint-plugin-jsx-a11y": "latest",
    "eslint-plugin-react": "latest"
  }
}

我甚至没有做任何花哨的事情,所以我不明白为什么会出现这个错误。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:15)

如果你仔细研究react-redux代码,你会看到这个

if (store !== nextStore) {
  warnAboutReceivingStore()
}

所以你似乎只需将createStore调用移到外面。

store = createStore(reducers)


class Edmund extends Component {

  ...

  startScreen() {
    if (this.state.screen === "HOME" ) {
      return (<Home />);
    }

    return (
      <Onboarding />
    );
  }

  render() {
    return (
      <Provider store={ store }>
        { this.startScreen() }
      </Provider>
    )
  }

Haven未经过测试但应该可以使用。

答案 1 :(得分:0)

我遇到了同样的错误,解决方法是删除DevTools。

<Provider store={store} >
   <App />
</Provider>

在组件App的render()方法中,有一个:

<DevTools />

删除DevTools后,错误消失了。

这不是一个好的解决方案,仅供您参考。