Different ways of destructuring props in react

Phantom

I have seen two ways to destructure props in React.

    function MyComponent({name,age,height}){
            // do stuff here
    }

or

    function MyComponent(props){
            const {name,age,height} = props
            // do stuff here
    }

Suppose this component is used as

<MyComponent name="bob" age={25} height = {175} haspets={false}/>

Here is my questions:

If I used the first way of destructuring, does it mean that I will have no access to other pros, in this case haspets

What are some pros/cons of these two ways?

tobiasfried

If you destructure your props from the function parameters, you won't be able to access any other props later on**, so your assumption is correct. As far as performance and other pros/cons though, these forms are pretty much identical. I happen to like this way, because I like as few additional variable declarations in my files as possible. It can, be a pain with functions that require lots of props.

**Unless you use the spread operator and store them in a separate variable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Two different ways of using props in React

Destructuring state/props in React

React - TypeScript destructuring of props

React: Props destructuring and memory usage

Typescript + React: Use props AND (!) destructuring

React state from props that doesn't update (even doing destructuring and using different keys)

destructuring props in component getting different result

Must use destructuring props assignment (react/destructuring-assignment)

Destructuring ImmutableJS Map for React Component Props

React props destructuring when passing to component

React props destructuring after updates stopped working?

React destructuring state to pass into component props

React: Ways of passing component as a props

Difference for two ways of using React props

Performance issue when destructuring props on React render method

How can I implement destructuring assignment for functional react components props?

Reason for destructuring props in react - apart from syntactic sugar

Destructuring props from Utility Types (Extract and Exclude - TypeScript and React)

Different ways to import files in React

Destructuring props in ReactJS

component props destructuring error

Destructuring props dynamically?

destructuring props with the rest properties

Destructuring this.props into Component

Initialize Destructuring props if null

Pass props to different components in react

Is there any ways to use react navigation props as a define object in reusable component?

2 different ways to create React component

React different ways of calling a child method