2

I'm creating a personal project with React and Next.JS. I'm trying to pass a parameter with a default value in order to don't need to specify a value each time I'm using this function. How can I do this? To understand I want shouldShuffle parameter to be default false.

const getItems = async ({shouldShuffle}) => {
 // my code
}

How can I do this?

AWX MEDIA
  • 41
  • 1
  • 3

1 Answers1

4

If you are looking to just pass default value to a parameters you can do like this ,

 function say(message='Hi') {
    console.log(message);
}

    say(); // 'Hi'
    say('Hello') // 'Hello'

But if you are looking for a passing a default value as props to a component try, to define an interface and destructure the Props with the default value in case Props don't have an optional argument passed to the Component.

Manu Jo Varghese
  • 360
  • 1
  • 14
  • Just to clarify, the same principal applies to arrow functions - `({ shouldShuffle = true })` – Sean W Jun 10 '22 at 03:10