Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with select in redux-saga. Error: call: argument of type {context, fn} has undefined or null `fn`

After looking through some answers to similar questions here, I just can't get my selector to work. Here's my selector.js:

export const getButtonStatus = state => state.buttonStatus;

(That's the entirety of the file. I don't know if I have to import anything into it. Didn't seem like it from looking at other answers I've seen here.)

and here's what I'm where I'm trying to access the selector in my 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 });
  }
}
...

Here's the error I'm receiving:

 Error: call: argument of type {context, fn} has undefined or null `fn`

I'm new to Saga. Can anyone tell me what I'm doing wrong?

like image 540
blutarch Avatar asked Oct 21 '19 04:10

blutarch


2 Answers

The error is not with your selector but with your yield call - it takes the function as an arg followed by the arguments to pass to the function: https://redux-saga.js.org/docs/api/#callfn-args. So it should be:

const response = yield call(getTweets, buttonStatus);

Otherwise looks good!

like image 154
azundo Avatar answered Nov 19 '22 04:11

azundo


For future seekers, you are probably doing this:

const foo = yield call(bar())

So you don't pass the function itself, but rather the function call.

Fix

Try to only send the function, not its call.

const foo = yield call(bar)
like image 32
Justice Bringer Avatar answered Nov 19 '22 03:11

Justice Bringer