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).
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.
a simple call could be comma separated, yield call(fnName, param1, param2)
call with , function name and arguments
call(funcname, ...args)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With