0% found this document useful (0 votes)
32 views

React Props

Props in React are arguments passed to components, similar to function arguments in JavaScript and HTML attributes. They allow data to be sent from one component to another, and can be passed as strings, variables, or objects. Examples demonstrate how to define and use props within components like Car and Garage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

React Props

Props in React are arguments passed to components, similar to function arguments in JavaScript and HTML attributes. They allow data to be sent from one component to another, and can be passed as strings, variables, or objects. Examples demonstrate how to define and use props within components like Car and Garage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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:

ExampleGet your own React.js Server

Add a "brand" attribute to the Car element:

const myElement = <Car brand="Ford" />;

The component receives the argument as a props object:

Example

Use the brand attribute in the component:

function Car(props) {

return <h2>I am a { props.brand }!</h2>;

Run Example »

ADVERTISEMENT

Pass Data

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

Example

Send the "brand" property 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" />

</>

);

const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(<Garage />);

Run Example »

If you have a variable to send, and not a string as in the example above, you just put the variable
name inside curly brackets:

Example

Create a variable named carName and send it to the Car component:

function Car(props) {

return <h2>I am a { props.brand }!</h2>;

function Garage() {

const carName = "Ford";

return (

<>

<h1>Who lives in my garage?</h1>

<Car brand={ carName } />

</>

);

const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(<Garage />);

Run Example »
Or if it was an object:

Example

Create an object named carInfo and send it to the Car component:

function Car(props) {

return <h2>I am a { props.brand.model }!</h2>;

function Garage() {

const carInfo = { name: "Ford", model: "Mustang" };

return (

<>

<h1>Who lives in my garage?</h1>

<Car brand={ carInfo } />

</>

);

const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(<Garage />);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy