ZiiMakc
ZiiMakc

Reputation: 36946

React props destructuring when passing to component

Is there a way to use es6 shorthand when passing a props with the same name?

const [showNavMenu, setShowNavMenu] = useState(false)

So this:

<NavMenu showNavMenu={showNavMenu} setShowNavMenu={setShowNavMenu} />

Will became this:

<NavMenu {showNavMenu} {setShowNavMenu} />

Upvotes: 8

Views: 4727

Answers (2)

Sdarb
Sdarb

Reputation: 183

you could always use the spread operator, which is my favorite way to pass props to a component

propsToPassthrough = {showNavMenu, setShowNavMenu}

then

<NavMenu {...propsToPassthrough}>

Upvotes: 5

Andrii Golubenko
Andrii Golubenko

Reputation: 5179

If you don't want to use an extra variable, you can do this:

<NavMenu {...{showNavMenu, setShowNavMenu}} />

Upvotes: 14

Related Questions