Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React props destructuring when passing to component

Tags:

reactjs

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} />
like image 888
ZiiMakc Avatar asked May 09 '19 19:05

ZiiMakc


People also ask

Is it good to Destructure props in React?

Advantages of Destructuring:It makes developer's life easy, by assigning their own variables. Nested data is more complex, it takes time to access, but by the use of destructuring, we can access faster of nested data. It improves the sustainability, readability of code.

Can you Destructure props in a class component?

You can destructure the React props object within the render method inside a React class component. You can also destructure the React props object within a React functional component.

What is state in React?

What Is 'State' in ReactJS? The state is a built-in React object that is used to contain data or information about the component. A component's state can change over time; whenever it changes, the component re-renders.


1 Answers

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

<NavMenu {...{showNavMenu, setShowNavMenu}} />
like image 85
Andrii Golubenko Avatar answered Nov 15 '22 08:11

Andrii Golubenko