Search code examples
react-nativereduxredux-saga

Redux saga pass multiple parameter as action and get it into saga function React native


i want to pass multiple parameters in Redux saga action so i did code as below

this.props.addCartData(_addToCartObject, 'guest') 

inside dispatch i am passing like below

addCartData: (value, isFor) => dispatch(addCartRequest(value, isFor)),

in saga function I am accessing as below

function* callAddCart(data, isFor) {
  try {
      console.log("isFor--->", isFor);
     }
   }

export function* cartSaga() {
return yield all([
    yield takeLatest(actionTypes.ADD_CART_REQUEST, callAddCart),
]);

}

But i log isFor I got undefined can you help me what i making wrong on this ?


Solution

  • it all works around your action creator addCartRequest,

    let's say it returns an object like:

    function addCartRequest(value, isFor) {
      return {
        type: actionTypes.ADD_CART_REQUEST,
        value,
        isFor
      };
    }
    

    your saga watcher yield takeLatest(actionTypes.ADD_CART_REQUEST, callAddCart), will pass the whole object to callAddCart,

    you can access their values assuming the first param as the whole object:

    function callAddCart(action) {
      console.log("actionType--->", action.type);
      console.log("value--->", action.value);
      console.log("isFor--->", action.isFor);
    }
    

    i created a working example in CodeSandbox:

    https://codesandbox.io/s/2021-03-25-redux-saga-action-creator-payload-hk4sb?file=/src/App.js