简体   繁体   English

在同构应用程序中使用redux

[英]Use redux in a isomorphic app

I'm trying to create a isomorphic/universal app with react+redux+express, and I'm having some problems to create the store from server.js. 我正在尝试使用react + redux + express创建一个同构/通用应用程序,我在从server.js创建存储时遇到了一些问题。

In server.js I have: 在server.js我有:

import { configureStore } from './store/configureStore';
const store = configureStore();

In my configureStore: 在我的configureStore中:

import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger'
import rootReducer from '../reducers/index';

const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);

export default function configureStore(initialState) {
  const store = createStoreWithMiddleware(rootReducer, initialState);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextRootReducer = require('../reducers').default
      store.replaceReducer(nextRootReducer)
    })
  }

  return store;
}

And I get this error: 我收到这个错误:

TypeError: (0 , _configureStore.configureStore) is not a function

on line: const store = configureStore(); 在线:const store = configureStore();

Why ? 为什么? what I'm doing wrong ? 我做错了什么?

If can be useful know, my directory structure is like this: - src -- store ---- configureStore.js -- server.js 如果知道有用,我的目录结构如下: - src - store ---- configureStore.js - server.js

You're exporting a default value so remove the curlies around your import: 您正在导出默认值,因此请删除导入周围的curlies:

import configureStore from './store/configureStore';

Alternatively, remove the default from your export and continue using curlies. 或者,从导出中删除default并继续使用curlies。

Whenever you see that error it's usually cause of how you import/export functions so pay close attention to those and you'll be good to go! 每当你看到这个错误时,通常会导致你导入/导出函数,所以要密切注意这些错误,你就会好起来!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM