2016-09-15 52 views
0

在文檔中有一些關於如何通過Redux操作發佈導航事件的信息(https://github.com/reactjs/react-router-redux#what-if-i-want-to-issue-navigation-events-via-redux-actions),但是如何通過操作訪問存儲?如何訪問商店中的動作來調度react-router-redux中的推送?

我的動作觸發訊(推( '/富')):

export function fetchReservationData(reservation_code, onError) { 

    return (dispatch) => { 
     fetch(apiConfig.find_my_product) 
      .then(function(response) { 

       if (response.ok) { 

        response.json().then(function (data) { 
         if (data.trolley.length > 0) { 
          dispatch({ type: UPDATE_TROLLEY_DATA, payload: data }); 
          // todo: use router-redux push instead 
          //window.location.pathname = '/your-trolley'; 
          dispatch(push('/your-trolley')); 
         } else { 
          dispatch({ type: TOGGLE_MODAL_WINDOW, payload: onError });       
         } 
        }); 

       } else { 

        // todo: handle exception 
        console.log('Network response was not ok.'); 
        dispatch({ type: TOGGLE_MODAL_WINDOW, payload: onError }); 

       } 

      }) 
      .catch(function (error) { 

       // todo: handle error 
       // handle it gracefully asked user to try again, if the problem keeps ocurring to 
       // talk with a staff member to report it to the backend team 
       console.log('There has been a problem with your fetch operation: ' + error.message); 
       dispatch({ type: TOGGLE_MODAL_WINDOW, payload: onError }); 

      }); 
    } 

} 

同時,如果從應用程序入口點它的作品叫(見註釋超時):

import React from 'react'; 
import ReactDOM from "react-dom"; 
import { Router, browserHistory } from 'react-router'; 
import routes from './routes'; 
import { syncHistoryWithStore, routerMiddleware, push } from 'react-router-redux' 
import { createStore, applyMiddleware } from 'redux'; 
import { Provider } from 'react-redux'; 
import promise from 'redux-promise'; 
import reducers from './reducers'; 
import thunk from 'redux-thunk'; 

// const createStoreWithMiddleware = applyMiddleware(promise)(createStore); 
// const store = createStoreWithMiddleware(reducers); 
const store = createStore(
    reducers, 
    applyMiddleware(thunk), 
    applyMiddleware(routerMiddleware(browserHistory)) 
); 
const history = syncHistoryWithStore(browserHistory, store); 

// this needs to work in the actions 
// setTimeout(() => { 
// store.dispatch(push('/your-trolley')); 
// }, 3000); 

ReactDOM.render(
    <Provider store={ store }> 
     <Router history={ history } routes={ routes } /> 
    </Provider>, 
    document.getElementById('app') 
); 

任何提示都表示讚賞!謝謝!

回答

2

但是如何從操作中訪問存儲?

我不認爲從actions訪問store是一個好主意。

但讓你在你的actions文件中尋找,結果你可以做

import { push } from 'react-router-redux'; 

export function changePage(url) { 
    return push(url); 
} 


// since you're using thunk middleware, you can also do 
export function someAction(url) { 
    return dispatch => { 
    dispatch(push(url)); 
    // and you can call dispatch multiple times here. 
    } 
} 

而且你還可以在這裏看看更新的答案, react-router-redux webpack dev server historyApiFallback fails

它顯示瞭如何您可以直接從組件訪問併發送push

相關問題