Programing

환원기(저감-라우터)의 현재 위치에 액세스하는 방법?

c10106 2022. 3. 23. 20:06
반응형

환원기(저감-라우터)의 현재 위치에 액세스하는 방법?

환원기에 환원기를 넣은 상태에서 현재 위치의 현재 경로 및 쿼리를 얻는 방법은?mapStateToProps로 구성 요소 내에서 경로 이름을 쉽게 얻을 수 있지만, 현재 경로에 대한 Reducer에 액세스하고 싶다.Reducx-router 1.0.0-beta7 react-router 1.0.3을 사용하고 있다.

1.way - rendix-thunk 및 getState()를 통해 특정 동작이 포함된 패스 경로 이름

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

2.way - 모든 동작으로 미들웨어 및 패스 경로 이름 작성

///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}

3.way - 구성 요소의 this.location.pathname에서 pass pathname을(통과 경로 이름)

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

//workflow:  component -> action -> reducer

참조URL: https://stackoverflow.com/questions/38084121/how-to-access-current-location-in-reducer-redux-router

반응형