Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get action.params from saga

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?

like image 219
Amio.io Avatar asked Jul 27 '16 06:07

Amio.io


People also ask

What is takeEvery in Redux saga?

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 .

What is function * In Redux saga?

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();}

How do I use dispatch in Redux saga?

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.

What is fork in Redux saga?

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.

How do I get user start action from Saga?

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.

What does the takelatest Saga Effect do?

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.

What happens to the returned data when Saga is running?

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, };

How to check if sagas are running or not?

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.


1 Answers

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

like image 54
Kokovin Vladislav Avatar answered Oct 05 '22 04:10

Kokovin Vladislav