react-props-and-expressions Flashcards
What are props in React?
Props in React are arguments (or “arbitrary inputs” per the official React docs) that you can pass into a React component. They can also be any JavaScript expression, but they must be surrounded by curly braces ( { } ).
How do you pass props to a component?
First, you pass in “props” as a parameter to the component (“props” is an object). Then, inside the code block of the component, you specify the property of the “props” object. For example, if you want to pass in a “number” property of the “props” object, you specify “props.number” in the code block of the component. Then, when you use the component, you specify the property of the “props” object. For example, < NumberDescriber number = 2 / >.
If the property is a JavaScript expression, you must surround it with curly braces (i.e. < MyComponent foo = { 1 + 2 + 3 + 4 } / > ). Inside the code block of MyComponent, you would specify “props.foo”.
*Note: Added spaces to the examples to prevent Brainscape from interpreting any HTML text
How do you write JavaScript expressions in JSX?
You must surround JavaScript expressions with curly braces in JSX