r/learnreactjs • u/BilboMcDoogle • May 09 '22
How come sometimes props are passed in with {} and sometimes they aren't? When do you use brackets for props and when do you not?
For example
import React from 'react'
function Component({example-props}) {
return (
<div>Component</div>
)
}
export default Componentvs
import React from 'react'
function Component(example-props) {
return (
<div>Component</div>
)
}
export default ComponentSorry if stupid question.
*fixed
6
Upvotes
6
u/Emjp4 May 09 '22
Your first example is destructuring props, and accessed a property of props called props. Typically you wouldn't put "props" inside of this object, you'd put a property of props in there like "title", and have direct access to "title" in Component.
Your second example is passing props in it's entirety. So you'd need to use dot notation to access any of props' properties like "props.title"
1
u/BCsabaDiy May 09 '22
First sample is a bad usage, works when you do <component props=var>, but do not use prop named props.
1
u/Benimation May 09 '22
You probably edited this, but
example-propsis not a valid identifier, as they can't contain dashes. The convention is to use lower camel case, so:exampleProps