2017-01-31 9 views
5

ではありません私のコードですリアクト:はstore.getStateはここで機能

store.js

import {createStore, applyMiddleware, compose} from 'redux'; 
import {fromJS} from 'immutable'; 
import {routerMiddleware} from 'react-router-redux'; 
import createSagaMiddleware from 'redux-saga'; 
import createReducer from './reducers'; 

const sagaMiddleware = createSagaMiddleware(); 

export default function configureStore(initialState = {}, history) { 
    // Create the store with two middlewares 
    // 1. sagaMiddleware: Makes redux-sagas work 
    // 2. routerMiddleware: Syncs the location/URL path to the state 
    const middlewares = [sagaMiddleware, routerMiddleware(history)]; 

    const enhancers = [applyMiddleware(...middlewares)]; 

    const store = createStore(createReducer, fromJS(initialState), enhancers); 

    // Extensions 
    store.runSaga = sagaMiddleware.run; 
    store.asyncReducers = {}; // Async reducer registry 

    return store; 
} 

Routes.js

import React from 'react'; 
import {Route, Router, IndexRoute, browserHistory} from 'react-router'; 
import {syncHistoryWithStore} from 'react-router-redux'; 
import store from './store'; 

import Welcome from './containers/Welcome'; 

const history = syncHistoryWithStore(browserHistory, store); 

const routes = (
    <Router history={history}> 
     <Route path="/"> 
       <IndexRoute component={Welcome} /> 
     </Route> 
    </Router> 
); 

export default routes; 

Index.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import {browserHistory} from 'react-router'; 
import { Providers } from 'react-redux'; 
import configureStore from './store'; 
import routes from './routes'; 


const initialState = {}; 
const store = configureStore(initialState, browserHistory); 

ReactDOM.render(
    <Provider store={store}> 
     {routes} 
    </Provider>, document.getElementById('main-content') 
); 

犯人がどこにいるのかわかりません。私はそれをデバッグしようとしましたが、本当にそれらのエラーになるものは見つかりませんでした。エラー:未知の型エラー:store.getStateは関数ではありません

すべての解決策はありますか?

+0

理由が 'ありエンハンサーは配列ですか?また、ルートファイルでストアを初期化する必要があります。 – hunterc

答えて

6

Routes.jsstoreが正しく初期化されていないことに注意してください。これらの行を追加する:

const initialState = {}; 
    const store = configureStore(initialState, browserHistory); 

と同じように、index.jsファイルを追加してください。

2

これが役立つが、{Provider}の代わりにインポート{Providers}という名前をreact-reduxライブラリから指定したかどうかは不明です。

1

私は(ダイナミックが必要)これをやっていた。..

const store = require('../store/app') 
    state = store.getState() 

しかしrequire代わりのimportを使用して、あなたがこれをしなければならない何らかの理由..

const store = require('../store/app') 
    state = store.default.getState() 
関連する問題