0 votes
in ReactJS by

What are the differences between call() and put() in redux-saga?

1 Answer

0 votes
by

Both call() and put() are effect creator functions. call() function is used to create effect description, which instructs middleware to call the promise. put() function creates an effect, which instructs middleware to dispatch an action to the store.

Let's take example of how these effects work for fetching particular user data.

function* fetchUserSaga(action) {
  // `call` function accepts rest arguments, which will be passed to `api.fetchUser` function.
  // Instructing middleware to call promise, it resolved value will be assigned to `userData` variable
  const userData = yield call(api.fetchUser, action.userId)

  // Instructing middleware to dispatch corresponding action.
  yield put({
    type: 'FETCH_USER_SUCCESS',
    userData
  })
}

Related questions

0 votes
asked Mar 4, 2020 in ReactJS by JackTerrance
+1 vote
asked Jun 25, 2021 in Redux by SakshiSharma
0 votes
asked Mar 3, 2020 in ReactJS by miceperry
...