I am using redux-saga
. In the code yield* ReduxSaga.takeEvery('MY_ACTION', updatePorts);
how can I access the action
to get its fields.
For example I have an action creator:
function status(){
type: 'MY_ACTION',
status: true
}
How can I access action.status
from my saga? Or do I have to access data only via getState()
and select?
Calling them spawns a saga on each action dispatched to the Store that matches pattern. takeEvery: The most common takeEvery function is very similar to redux-thunk in its behaviour and methodology. It's basically a wrapper for yield take of a pattern or channel and yield fork .
In Redux saga, yield is a built in function which allows to use generator functions sequentially. When used in Javascript, the generator functions will allow to yield all values from the nested functions. function* one(){}function* abc(){const result = yield* one();}
Create a plain JavaScript Object to instruct the middleware that we need to dispatch some action, and let the middleware perform the real dispatch. This way we can test the Generator's dispatch in the same way: by inspecting the yielded Effect and making sure it contains the correct instructions.
In redux-saga you can dynamically fork tasks that execute in the background using 2 Effects. fork is used to create attached forks. spawn is used to create detached forks.
At the bottom of sagas, there is a watcher saga watchGetUserStart (), which waits for the GET_USER_START action to be called (this action is usually called from some component or from different saga), and if so, it calls handler saga handleGetUserStart and passes it the action with payload—if there’s any.
The takeLatest saga effect will cancel the previous saga task (in our example handleGetUserStart) and start a new one—if the same action has been called multiple times. In handler saga handleGetUserStart, we wait until fetch Promise with a given URL is resolved and assign this value to response constant.
Assuming the saga actually is running, you never do anything with the returned data, which explains why this.props.data is null. You can try this. case CreateActionType.CREATE_SUCCESS: return { ...state, createStatus: true, data: action.params.data, };
If you are not sure if the sagas are running or not, you should add some logging calls in api.js and sagas.js. I don't see any reason why the sagas should not work, but you might try to simplify it a bit more. I don't think there's any benefit to yield arrays of effects vs. yielding a single effect.
const actionCreator=()=>({
type: 'MY_ACTION',
status:true
})
function* updatePorts(action) {
console.log(action.status)
}
function* watchUpdatePorts() {
yield* takeEvery('MY_ACTION', updatePorts)
}
https://github.com/redux-saga/redux-saga/tree/master/docs/api#takeeverypattern-saga-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