redux-saga 中选择的问题。错误:调用:{context, fn} 类型的参数未定义或为空 `fn`

blu*_*rch 14 javascript select redux redux-saga

在浏览了类似问题的一些答案后,我无法让我的选择器工作。这是我的 selector.js:

export const getButtonStatus = state => state.buttonStatus;
Run Code Online (Sandbox Code Playgroud)

(这就是文件的全部内容。我不知道是否必须将任何内容导入其中。从我在这里看到的其他答案中看似乎不是这样。)

这就是我在我的传奇中尝试访问选择器的地方:

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 });
  }
}
...
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误:

 Error: call: argument of type {context, fn} has undefined or null `fn`
Run Code Online (Sandbox Code Playgroud)

我是佐贺的新手。谁能告诉我我做错了什么?

azu*_*ndo 34

错误不在于您的选择器,而在于您的yield call- 它将函数作为 arg 后跟要传递给函数的参数:https : //redux-saga.js.org/docs/api/#callfn-args。所以应该是:

const response = yield call(getTweets, buttonStatus);

否则看起来不错!


Jus*_*ger 7

问题

你可能正在这样做:

const foo = yield call(bar())
Run Code Online (Sandbox Code Playgroud)

因此,您不传递函数本身,而是传递函数调用。

修复方法

尝试仅发送函数,而不发送其调用。

const foo = yield call(bar)
Run Code Online (Sandbox Code Playgroud)

请注意,我们只有bar,而不是bar()