<Provider> does not support changing `store`

新手上路,请多包涵

Redux 热更新 不可用


// index.js

import React from 'react'
import { Provider } from 'react-redux'
import { render } from 'react-dom'

import configureStore from './configureStore'
import App from './app'

import './index.less'

const rootEl = document.getElementById('app')

const store = configureStore()

render(
  <Provider store={store}>
    <App />
  </Provider>,
  rootEl,
)

// configureStore.js

export default function configureStore(initialState) {
  const middleware = []
  const store = createStore(
    rootReducer,
    initialState,
    compose(
      applyMiddleware(...middleware),
      process.env.NODE_ENV !== 'production' &&
      window.__REDUX_DEVTOOLS_EXTENSION__ &&
      window.__REDUX_DEVTOOLS_EXTENSION__(),
    ),
  )

  if (module.hot) {
    module.hot.accept('./reducers', () => {
      const nextRootReducer = require('./reducers/index').default
      store.replaceReducer(nextRootReducer)
    })
  } else {
    console.log('not hot')
  }

  return store
}

目录结构
clipboard.png

阅读 2.5k
2 个回答
✓ 已被采纳新手上路,请多包涵

以解决问题, 热更新 更新了index.js 导致redux 热更新失败

store被修改了?
给运行正常的代码:
store.js

import {createStore, applyMiddleware, compose} from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import YourReducer from '../reducers/YourReducer.js'; //import your reducer
let createStoreWithMiddleware = null;

if(process.env.NODE_ENV === 'production') {
    createStoreWithMiddleware = compose(applyMiddleware(
        thunkMiddleware
    ))(createStore);
} else {
    createStoreWithMiddleware = compose(applyMiddleware(
        thunkMiddleware,
        createLogger()
    ), window.devToolsExtension ? window.devToolsExtension() : f => f)(createStore);
}

export default (initState) => {
    return createStoreWithMiddleware(YourReducer, initState);
}

index.js

import {Provider} from 'react-redux';
import store from './stores/store.js';
<Provider store={store()}>
//...
</Provider>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏