TypeError:_store_js__WEBPACK_IMPORTED_MODULE_12 __。default.subscribe不是函数

时间:2019-01-07 17:34:05

标签: javascript reactjs npm react-redux redux-saga

我有一个主要的App组件,通常会订阅商店:

class App extends Component {
  constructor(props) {
    super(props)
    this.state = {}
    this.unsubscribe = store.subscribe(() => {
      console.log(store.getState());
      this.setState(store.getState());
    })
  }

  render() {
    return (
      <Provider store={store}>
        <div id="App">
          <MainNavbar />
          <ContentHandler />
          <Footer />
        </div>
      </Provider>
    );
  }
}

store.js上,如果我仅使用root reducer创建商店,一切都会正常进行:

const store = createStore(reduceAppState);

尽管如此,如果我通过佐贺作为中间件创建它:

    import { createStore } from 'redux'
    import createSagaMiddleware from 'redux-saga'
    import { composeWithDevTools } from 'remote-redux-devtools';
    import reduceAppState from './reducers'

    const sagaMiddleware = createSagaMiddleware()
    const composeEnhancers = composeWithDevTools({trace: true, traceLimit: 25})

    const store = createStore(reduceAppState, undefined, composeEnhancers(
    sagaMiddleware
));

    export default store;

我收到此错误:

enter image description here

听起来像是个错误。在Google上找不到任何内容。你知道为什么会发生吗?

2 个答案:

答案 0 :(得分:0)

发现问题:我必须在sagaMiddleware上applyMiddleware

import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-saga'
import { composeWithDevTools } from 'remote-redux-devtools';
import reduceAppState from './reducers'

const sagaMiddleware = typeof createSagaMiddleware === 'function' ? createSagaMiddleware() : createSagaMiddleware.default()
const composeEnhancers = composeWithDevTools({trace: true, traceLimit: 25})

const store = createStore(reduceAppState, composeEnhancers(
    applyMiddleware(sagaMiddleware)
));

export default store;

答案 1 :(得分:-1)

尝试更改 subscribe(() =>subscribe()(