2017-02-26 2 views
2

다음과 같이 호출하는 두 가지 동작이 있습니다. 뷰가 처음로드Redux 동작이 두 번째로 호출되지 않음

export function action1(params) { 
    //This line is always called. 
    return (dispatch) => { 
    //This line is not called the second time. 
    return MyApi.call1(params) 
    .then(response => { 
     // some logic 
     return dispatch(someFunction1()); 
    }) 
    .catch(error => { 
     throw(error); 
    }); 
    }; 
} 

export function action2(params) { 
    return (dispatch) => { 
    return MyApi.call2(params) 
    .then(response => { 
     // call the first API again 
     action1(); 
     return dispatch(someFunction2()); 
    }) 
    .catch(error => { 
     throw(error); 
    }); 
    }; 
} 

, action1는 뷰의 생성자에서 호출된다. 작업을 수행하고 action2을 동일한보기에서 트리거하면 서버에서 업데이트 된 목록을 얻으려면 을 action2의 성공에 호출해야합니다. 아쉽게도 action1을 두 번 호출하면 오류없이 코드가 중단됩니다.

무엇이 여기에 있습니까?

+5

btw, 당신은 action1을 발송하지 않았습니다 :'dispatch (action1 (params))'; –

+1

효과가있었습니다. 그것은 내 바보 같았습니다. 수락 할 수 있도록 답변으로 게시 할 수 있습니까? –

답변

2

action1을 발송하지 않았습니다. dispatch없이 action1() 호출

dispatch(action1(params))

은 함수를 반환합니다. 반환 된 함수에서 dispatch을 얻으려면 그 함수가 dispatch이어야합니다. 그런 다음 redux-thunk 미들웨어에 걸립니다. 미들웨어는 dispatch을 전달하고 함수를 호출합니다.

관련 문제