-1

For example I have a action calling crazyAction it has three parameter second one and third one can be null but first one has to something as fallows;

crazyAction(firstParameter, secondParameter = null, thirdParameter = null){
      return{
         type:    CONSTANTS.WHATEVER,
         payload: {firstParameter, secondParameter, thirdParameter}      
      }
}

I should send the parameters by sequence when I want to dispatch to this, like;

    dispatch( uiActions.navigateToPage(firstParameter, null, thirdParameter));

So the question is if I want to send only thirdParameter without second one there is another option without null things on the middle?

like this one, How is that possible?

dispatch( uiActions.navigateToPage(firstParameter, thirdParameter));
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
Uğur Erdal
  • 67
  • 1
  • 12

1 Answers1

1

With Destructuring Assignment,you can do something like this.

The missing params will come as a null, which will help you to not pass params in order.

    function crazyAction({ firstParameter, secondParameter, thirdParameter}){

      console.log(firstParameter, secondParameter, thirdParameter);
      //output "first"    null    "3"
    }

Define object holding the passed properties and call function.

const objParams = {
  firstParameter : "first", thirdParameter:"3"
};
crazyAction(objParams); 

Working codesandbox demo

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53