2017-03-22 3 views
1

redux 스토어에 이미 연결되어 있고 dispatch이 삽입 된 구성 요소가 있습니다. 이 같은 withReducerwithHandlers 사용하여 구성 요소의 로컬 상태를 업데이트하려고 : 내가 찾을withHedlers의 withReducer에서 재구성 된 로컬 디스패치 기능

const reducer = (state, action) => { 
    switch (action.type) { 
    case 'SET_ACTIVE_TAB': 
     console.log('recieved action', action) 
     const { activeTab } = action 
     return activeTab 
    default: 
    return state 
    } 

}

const initialState = 'actions' 

const handlers = withHandlers({ 
    onTabClick: props => (e, { name }) => { 
    const { dispatchLocal } = props 
    dispatchLocal({ 
     type: 'SET_ACTIVE_TAB', 
     activeTab: name 
    }) 
    } 
}) 

const enhancer = withReducer('activeTab', 'dispatchLocal', reducer, initialState) 

을 그 때 compose :

compose(handlers, enhancer)(LayerListItem) 

dispatchLocal 소품 handler 내에 정의되지 않았습니다. recompose으로 작업 작성자를 만들고 바인딩하는 가장 좋은 방법은 응용 프로그램 상태를 업데이트하는 것입니다.

답변

0

당신은 withHandlers의 오른쪽에 withReducer를 이동해야합니다 :

이런 식으로
compose(
    withReducer(...), 
    withHandlers(...) 
) 

, withReducer 먼저 파견 함수를 생성하고 props에 추가 한 후 withHandlers 그것을 소비 할 수 있습니다.