Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass urls, params, headers to call() in redux-saga?

Theory regarding working of call() is well explained on many websites. However, I did not found any site which explains accurately with proper working example.

I have written following code:

export function* loadUser() {
    try {
    const user = yield call(getUser);
    yield put({type: 'FETCH_USER_SUCCESS', payload: user});

  } catch(error) {
    yield put({type: 'FETCH_FAILED', error});
  }
}    

here, I want to send 'get' request with some parameters and some header using call() .But I don't know how to achieve it. Please, if you have time, tell it with proper working example(Codepen or jsFiddle).

like image 785
Kiran Avatar asked Sep 07 '18 09:09

Kiran


3 Answers

If you read the Redux Saga documentation you can see call takes a function and a spread array of arguments (comma separated):

call(fn, ...args)

You can use it like so:

const getUsers = (options) => {
  return axios(options)
}

function *fetchUsers() {
  const users = yield call(getUsers, { method: 'get', url: `https://api.github.com/users` }, {user: 'my_username'})
  console.log(users)
}

Pretty straight forward.

like image 185
Ivor Avatar answered Sep 17 '22 20:09

Ivor


a simple call could be comma separated, yield call(fnName, param1, param2)

like image 28
Prashant Avatar answered Sep 18 '22 20:09

Prashant


call with , function name and arguments

call(funcname, ...args)
like image 39
ulduz Avatar answered Sep 20 '22 20:09

ulduz