2017-01-24 5 views
1

redux-thunk에 문제가 있습니다. 파견은 내 액션 크리에이터 내부의 함수가 아니라는 것을 말하며 반환 된 인수를 위임하려고 시도했지만 아무 것도 없습니다. 여기 Redux-thunk - dispatch가 함수가 아닙니다.

코드 진행 : 그런데

액션

export function signUp(data) { 
    return dispatch => { 
    console.log(dispatch) 
    if (data.email === '[email protected]') { 
     dispatch(signIn(data,() => { 
     if (data.type === '2') { 
      browserHistory.push('/settings/profile') 
     } else { 
      browserHistory.push('/') 
     } 
     })) 
    } else { 
     return { 
     type: ActionTypes.USER_SIGN_UP__ERROR 
     } 
    } 
    } 
}` 

mapActionsToProps

const mapActionsToProps = dispatch => ({ 
    signUp (data) { 
    console.log(dispatch) 
    dispatch(userActions.signUp(data)) 
    } 
}) 

을, 당신은 내가 mapActionsToProps 내부 디스패치 기능을 위로를 참조 할 수 있습니다 그것이 예정되어 있었던 것에 따라 돌아오고있다 :

function (action) { 
    if (typeof action === 'function') { 
     return action(dispatch, getState, extraArgument); 
    } 

    return next(action); 
    } 

답변

1

발송은 작업 작성자가 전달하지 않았기 때문에 기능이 아닙니다.

게다가, mapActionsToProps 안에는 어떤 액션도 보내면 안됩니다. 연결된 구성 요소에서 액세스 할 수 있도록 바인딩해야합니다.

귀하의 mapActionsToProps

const mapActionsToProps = (dispatch) => { 
    return { 
    asyncAction: bindActionCreators(asyncAction, dispatch), 
    } 
} 

const Container = connect(mapStateToProps, mapActionsToProps)(Component); 

비동기 작업이

export const asyncAction = (email) => { 
    return (dispatch, getState) => { 

    const state = getState(); 

    dispatch(StartAsync()); 

    return fetch(`${apiUrl}/endpoint?email=${email}`, { 
     method: 'GET' 
    }) 
     .then(response => response.json()) 
     .then((result) => dispatch(finishedAsync(result)), 
       (error) => dispatch(failedAsync(error))) 
     .catch(e => { 
      console.log('error:', e); 
     }); 
    }; 
}; 

그런 다음, 연결된 기기에, 당신은 소품에서이 작업을 전달할 수 있습니다.

+0

실제로, redux 소스 코드에 따르면,이 [bindActionCreators 함수]는 "store.dispatch (MyActionCreators.doSomething())"를 "괜찮아"라고 부를 수 있으므로 편의 메소드 일뿐입니다. 코드 구조에 실제 차이점이없는 것 외에 다른 차이점이 있습니까? 감사합니다 –

+0

당신 말이 맞아요. 나는 지금 막 액션 크리에이터를 묶는 데 익숙하다. 'mapActionsToProps'에서 함수를 반환 해 보았습니까? = 파견 => – jdrzejb

+0

'const를 mapDispatchToProps { \t 반환 { 는 \t 가입 : (데이터) => { \t 파견 (userActions.signUp (데이터)) \t} \t} \t은}' – jdrzejb