2016-09-20 3 views
2

에서 작동하지 않습니다REDUX-사가 내가 아래와 같이 REDUX-사가이 처음

export function* loadApplianceSaga() { 
    try { 
    let {request, success, error} = yield take(ActionTypes.APPLIANCE.LOAD); 
    request.url = yield select(getResourceLink, request.resource); 
    const response = yield call(makeRequest, request); 
    if (!response.error) { 
     yield put({type: success, payload: response.body}); 
    } else { 
     yield put({type: error, payload: response}); 
    } 
    } catch (e) { 
    yield put({type: ActionTypes.REQUEST.CALL_ERROR, error: e}); 
    } 
} 

export function* watchLoadAppliance() { 
    while (true) { 
    yield* takeEvery(ActionTypes.APPLIANCE.LOAD, loadApplianceSaga); 
    } 
} 

및 루트 사가 :

export default function* rootSaga() { 
    yield [ 
    fork(watchLoadAppliance) 
    ] 
} 

나는 문제에 직면하고있어 그 loadApplianceSaga 아무튼 처음에는 일하지 않습니다. 로그 한 후 처음으로 ActionTypes.APPLIANCE.LOAD 액션을 전달한 것으로 보았으므로 아무 작업도 수행되지 않습니다. 그러나 두 번째로 나는 성공한 행동이나 파견 된 실패한 행동을 볼 수 있습니다.

아무도 저에게 무엇이 잘못되었는지 말할 수 있습니까? 미리 감사드립니다!

업데이트 작업을는 :

export const loadAppliances =() => { 
    return { 
    type: ActionTypes.APPLIANCE.LOAD, 
    request: { 
     resource: Resources.Appliances, 
     param: { 
     page: 0, 
     size: 5, 
     sort: 'name,desc' 
     }, 
     header: { 
     Accept: 'application/json' 
     } 
    }, 
    success: ActionTypes.APPLIANCE.LOAD_SUCCESS, 
    error: ActionTypes.APPLIANCE.LOAD_ERROR 
    } 
}; 
+0

조치에 {요청, 성공, 오류}가 포함되어 있습니까? –

+0

예, 작업에 모두 포함됩니다. –

+0

조치를 추가했습니다. @Utro –

답변

2

당신은 take 두 번 사용했다. 시도해보십시오.

export function* loadApplianceSaga(action) { 
    try { 
    let {request, success, error} = action; 
    request.url = yield select(getResourceLink, request.resource); 
    const response = yield call(makeRequest, request); 
    if (!response.error) { 
     yield put({type: success, payload: response.body}); 
    } else { 
     yield put({type: error, payload: response}); 
    } 
    } catch (e) { 
    yield put({type: ActionTypes.REQUEST.CALL_ERROR, error: e}); 
    } 
} 

export function* watchLoadAppliance() { 
    while (true) { 
    yield* takeEvery(ActionTypes.APPLIANCE.LOAD, loadApplianceSaga); 
    } 
} 
+0

정말 고마워요! 그거야. 왜이 문제를 만났는지 조금 설명해 주시겠습니까? 나는 redux-saga에 처음이다. –

+0

내가 생각하기에, 당신은 @AnNguyen 액션을 돌연변이해서는 안된다. –

+0

아, 나는 그것을 고려할 것이다. 감사! –

관련 문제