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

2

21

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?

Goodyear answered 21/10, 2019 at 4:22 Comment(0)
A
50

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!

Amritsar answered 21/10, 2019 at 4:40 Comment(0)
W
8

The Problem

You are probably doing this:

const foo = yield call(bar())

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

The Fix

Try to only send the function, not its call.

const foo = yield call(bar)

Notice that we have bar only, not bar().

Widget answered 9/2, 2021 at 9:36 Comment(2)
I was spending 2 hours and I did not see the () at the end.Astrodynamics
Thank you for your comment! I have added an extra line in my answer to underline that.Widget

© 2022 - 2024 — McMap. All rights reserved.