我学习react-saga
了,按照这个来源的例子做了一切:https ://hackernoon.com/redux-saga-tutorial-for-beginners-and-dog-lovers-aa69a17db645
以前,我react-thunk
在一个单独的文件actionTypes.js
(然后我注意到,如果您在 中takeEvery
替换一个变量API_CALL_REQUEST
,则没有任何效果,但如果您替换"API_CALL_REQUEST"
它,它会起作用。这是为什么?
动作/actionTypes.js
export const API_CALL_SUCCESS = "API_CALL_SUCCESS";
export const API_CALL_FAILURE = "API_CALL_FAILURE";
export const API_CALL_REQUEST = "API_CALL_REQIEST";
传奇/index.js
import { call, put, takeEvery } from 'redux-saga/effects';
import axios from 'axios';
import {
API_CALL_REQUEST,
API_CALL_SUCCESS,
API_CALL_FAILURE
} from '../actions/actionTypes';
export function* watcherSaga() {
yield takeEvery("API_CALL_REQUEST", workerSaga);
}
function fetchDog() {
return axios({
method: "GET",
url: "https://dog.ceo/api/breeds/image/random"
});
}
function* workerSaga() {
try {
const response = yield call(fetchDog);
const dog = response.data.message;
yield put({ type: API_CALL_SUCCESS, data: dog });
} catch (error) {
yield put({ type: API_CALL_FAILURE, data: error });
}
}
如果是这样:
export function* watcherSaga() {
const test = "API_CALL_REQUEST";
yield takeEvery(test, workerSaga);
}
那么它可以工作,但如果是这样:
export function* watcherSaga() {
const test = API_CALL_REQUEST;
yield takeEvery(test, workerSaga);
}
再次没有。常数有什么问题API_CALL_REQUEST
?同一文件中的其他常量没有问题。