2016-06-28 27 views
6

Geçerli konumun ve redüktördeki redux-yönlendirici ile mevcut konumun sorgulamasını nasıl alırım. MapStateToProps ile bileşen içerisindeki yol adını kolayca alabiliyorum, ancak redüktördeki mevcut yola erişmek istiyorum. Redux-yönlendirici 1.0.0-beta7, tepki-yönlendirici 1.0.3 kullanıyorum.Redüktördeki mevcut konuma nasıl erişilir (redux-router)?

+0

gelen pas yol adı belki eylem creater yoluyla redüktör içine yol adı geçebilir? –

+1

Geçerli yolun "gerçekliğin kaynağı" nı neden yinelediniz? Eğer mevcut yollara erişmeniz gerekiyorsa, 'yönlendirici' otomatik olarak geçtiğinden bunu sadece 'props' den elde edin. Yönlendirici, rota konumu için gerçekliğin kaynağıdır, bunu redux'ta çoğaltmaya gerek yoktur. Yazar Redux'dan duymanız gerekiyorsa şunu izleyin: https://egghead.io/lessons/javascript-redux-filtering-redux-state-with-react-router-params – lux

+0

Bu nasıl [redux-router] (https://github.com/acdlite/redux-router#differences-with-react-router-redux) çalışır –

cevap

12

.way - orta katman yazmak ve her hareket yol adı - pass redux-thunk ve getState() yoluyla, özellikle harekete ile

const someAction = data =>(dispatch,getState)=>{ 
    dispatch({ 
     type:'SOME_ACTION', 
     pathname:getState().router.pathname 
    }) 
} 

.way yol adı geçmesi

///write middleware 
export const attachPathNameToAction = store => next => action=>{ 
    action.pathname = store.getState().router.pathname //<-----passing pathname 
    next(action) 
}; 


///then in reducer you allways can get pathname 
case 'SOME_ACTION': 
    let pathname = action.pathname \\ <------------- 
    return {...state, action.data} 

.way - bileşenin this.props.location.pathname

//in component 
let {pathname}= this.props.location; 
this.props.someAction(data, pathname); 

//workflow: component -> action -> reducer 
+2

Ara katman yazılımı mükemmeldir! Bunun için teşekkürler. Bunun için neden bir eklentisi olmadığını merak ediyorum. – Tomer

+0

Not: .router.location.pathname', '... .router.pathname' değil. –