Problem with select in redux-saga. Error: call: argument of type {context, fn} has undefined or null `fn`
Problem with select in redux-saga. Error: call: argument of type {context, fn} has undefined or null `fn`
在这里浏览了一些类似问题的答案后,我就是无法让我的选择器工作。这是我的 selector.js:
export const getButtonStatus = state => state.buttonStatus;
(这是整个文件。我不知道我是否必须向其中导入任何内容。从我在这里看到的其他答案来看,似乎不是这样。)
这就是我在我的 saga 中尝试访问选择器的位置:
import { takeLatest, call, put, select } from "redux-saga/effects";
import { getButtonStatus } from "./selector.js";
...
export function* watcherSaga() {
yield takeLatest("get-tweets", workerSaga);
}
function* workerSaga() {
try {
const buttonStatus = yield select(getButtonStatus);
const response = yield call(getTweets(buttonStatus));
const tweets = response.tweets;
yield put({
type: "tweets-received-async",
tweets: tweets,
nextTweeter: response.nextTweeter
});
} catch (error) {
console.log("error = ", error);
yield put({ type: "error", error });
}
}
...
这是我收到的错误:
Error: call: argument of type {context, fn} has undefined or null `fn`
我是 Saga 的新手。谁能告诉我我做错了什么?
错误不在于您的选择器,而在于您的 yield call
- 它将函数作为参数,后跟要传递给函数的参数:https://redux-saga.js.org/docs/api/#callfn-args。所以应该是:
const response = yield call(getTweets, buttonStatus);
其他看起来不错!
对于未来的求职者,您可能正在这样做:
const foo = yield call(bar())
所以你不传递函数本身,而是传递函数调用。
修复
尝试只发送函数,而不是它的调用。
const foo = yield call(bar)
在这里浏览了一些类似问题的答案后,我就是无法让我的选择器工作。这是我的 selector.js:
export const getButtonStatus = state => state.buttonStatus;
(这是整个文件。我不知道我是否必须向其中导入任何内容。从我在这里看到的其他答案来看,似乎不是这样。)
这就是我在我的 saga 中尝试访问选择器的位置:
import { takeLatest, call, put, select } from "redux-saga/effects";
import { getButtonStatus } from "./selector.js";
...
export function* watcherSaga() {
yield takeLatest("get-tweets", workerSaga);
}
function* workerSaga() {
try {
const buttonStatus = yield select(getButtonStatus);
const response = yield call(getTweets(buttonStatus));
const tweets = response.tweets;
yield put({
type: "tweets-received-async",
tweets: tweets,
nextTweeter: response.nextTweeter
});
} catch (error) {
console.log("error = ", error);
yield put({ type: "error", error });
}
}
...
这是我收到的错误:
Error: call: argument of type {context, fn} has undefined or null `fn`
我是 Saga 的新手。谁能告诉我我做错了什么?
错误不在于您的选择器,而在于您的 yield call
- 它将函数作为参数,后跟要传递给函数的参数:https://redux-saga.js.org/docs/api/#callfn-args。所以应该是:
const response = yield call(getTweets, buttonStatus);
其他看起来不错!
对于未来的求职者,您可能正在这样做:
const foo = yield call(bar())
所以你不传递函数本身,而是传递函数调用。
修复
尝试只发送函数,而不是它的调用。
const foo = yield call(bar)