bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/React/React Core
React•React Core

React Props

Props are arguments passed into React components.

Props are passed to components via HTML attributes.

props stands for properties.

React Props

React Props are like function arguments in JavaScript and attributes in HTML.

To send props into a component, use the same syntax as HTML attributes:

Example

Add a brand attribute to the Car element:

createRoot(document.getElementById('root')).render( <Car brand="Ford" /> );

The component receives the argument as a props object:

Example

Use the brand attribute in the Car component:

function Car(props) {
 return ( <h2>I am a {props.brand}!</h2> );
}

The name of the object is props , but you can call it anything you want.

Example

You can use myobj instead of props in the component:

function Car(myobj) {
 return ( <h2>I am a {myobj.brand}!</h2> );
}

Pass Multiple Properties

You can send as many properties as you want.

Every attribute is sent to the Car component as object properties.

Example

Send multiple properties to the Car component:

createRoot(document.getElementById('root')).render( <Car brand="Ford" model="Mustang" color="red" /> );

All properties are received in the Car component inside the props object:

Example

Use the property values in the Car component:

function Car(props) {
 return ( <h2>I am a {props.color} {props.brand} {props.model}!</h2> );
}

Different Data Types

React props can be of any data type, including variables, numbers, strings, objects, arrays, and more.

Strings can be sent inside quotes as in the examples above, but numbers, variables, and objects need to be sent inside curly brackets. Example Numbers has to be sent inside curly brackets to be treated as numbers: createRoot(document.getElementById('root')).render( <Car year={1969} /> ); Example Variables has to be sent inside curly brackets: let x = "Ford"; createRoot(document.getElementById('root')).render( <Car brand={x} /> ); Example Objects and Arrays has to be sent inside curly brackets: let x = [1964, 1965, 1966]; let y = {name: "Ford", model: "Mustang"}; createRoot(document.getElementById('root')).render( <Car years={x} carinfo={y} /> ); Object Props The component treats objects like objects, and you can use the dot notation to access the properties. Example Use the dot notation to access object properties: function Car(props) { return ( <> <h2>My {props.carinfo.name} {props.carinfo.model}!</h2> <p>It is {props.carinfo.color} and it is from {props.carinfo.year}!</p> </> ); } const carInfo = { name: "Ford", model: "Mustang", color: "red", year: 1969 }; createRoot(document.getElementById('root')).render( <Car carinfo={carInfo} /> ); Array Props Array props can be accessed using the indexes. Example Use the indexes to access array properties: function Car(props) { return ( <h2>My car is a {props.carinfo[0]} {props.carinfo[1]}!</h2> ); } const carInfo = ["Ford", "Mustang"]; createRoot(document.getElementById('root')).render( <Car carinfo={carInfo} /> ); Pass Props from Component to Component Attributes are also how you pass data from one component to another, as parameters. Example Send the brand attribute from the Garage component to the Car component: function Car(props) { return ( <h2>I am a {props.brand}!</h2> ); } function Garage() { return ( <> <h1>Who lives in my garage?</h1> <Car brand="Ford" /> </> ); } createRoot(document.getElementById('root')).render( <Garage /> ); Note: React Props are read-only! You will get an error if you try to change their value.

Example

Numbers has to be sent inside curly brackets to be treated as numbers:

createRoot(document.getElementById('root')).render( <Car year={1969} /> );

Example

Variables has to be sent inside curly brackets:

let x = "Ford";
createRoot(document.getElementById('root')).render( <Car brand={x} /> );

Example

Objects and Arrays has to be sent inside curly brackets:

let x = [1964, 1965, 1966];
let y = {name: "Ford", model: "Mustang"};
createRoot(document.getElementById('root')).render( <Car years={x} carinfo={y} /> );

Object Props

The component treats objects like objects, and you can use the dot notation to access the properties.

Example

Use the dot notation to access object properties:

function Car(props) {
 return ( <> <h2>My {props.carinfo.name} {props.carinfo.model}!</h2> <p>It is {props.carinfo.color} and it is from {props.carinfo.year}!</p> </> );
}
const carInfo = {
 name: "Ford", model: "Mustang", color: "red", year: 1969
};
createRoot(document.getElementById('root')).render( <Car carinfo={carInfo} /> );

Array Props

Array props can be accessed using the indexes.

Example

Use the indexes to access array properties:

function Car(props) {
 return ( <h2>My car is a {props.carinfo[0]} {props.carinfo[1]}!</h2> );
}
const carInfo = ["Ford", "Mustang"];
createRoot(document.getElementById('root')).render( <Car carinfo={carInfo} /> );

Pass Props from Component to Component

Attributes are also how you pass data from one component to another, as parameters.

Example

Send the brand attribute from the Garage component to the Car component:

function Car(props) {
 return ( <h2>I am a {props.brand}!</h2> );
}
function Garage() {
 return ( <> <h1>Who lives in my garage?</h1> <Car brand="Ford" /> </> );
}
createRoot(document.getElementById('root')).render( <Garage /> );

Note

React Props are read-only! You will get an error if you try to change their value.

Previous

React Class Components

Next

React Destructuring Props

This chapter

Overview
45

Lessons

142m

Read time

1. React Tutorial2. React Introduction3. React Getting Started4. Your First React App5. React Render HTML6. Upgrade React7. React ES68. React ES6 Classes9. React ES6 Arrow Functions10. React ES6 Variables11. JavaScript Array map()12. React ES6 Destructuring13. React ES6 Spread Operator14. React ES6 Modules15. React ES6 Ternary Operator16. React ES6 Template Strings17. React JSX18. React JSX Expressions19. React JSX Attributes20. React JSX If Statements21. React Components22. React Class Components23. React Props24. React Destructuring Props25. React Props Children26. React Events27. React Conditional Rendering28. React Lists29. React Forms30. React Submit Forms31. React Forms - Textarea32. React Forms - Select33. React Forms - Multiple Input Fields34. React Forms - Checkbox35. React Forms - Radio36. React Portals37. React Suspense38. Styling React Using CSS39. React CSS Modules40. React CSS-in-JS41. React Router42. React Transitions43. React forwardRef44. React Higher Order Components45. React Sass Styling

On this page

React PropsPass Multiple PropertiesDifferent Data TypesObject PropsArray PropsPass Props from Component to Component