DEV Community

swetha palani
swetha palani

Posted on

PROPS in React

Props (short for properties) are read-only data passed from a parent component to a child component.They help in reusing components and data sharing.

Props are arguments passed into React components, Props are passed to components via HTML attributes.

EXAMPLE:

**parent class(app.jsx)**

function App(){
return(
<Welcome 
name="swetha"
/>
)
}

**child class(Welcome.jsx)**

function Welcome(props){
return(
<div>
<h1>HELLO{props.name}</h1>

);
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT

HELLO swetha
Enter fullscreen mode Exit fullscreen mode

Why are props used in React?

Pass data from parent to child component, Make components dynamic and reusable, Maintain one-way data flow

Can you modify props inside a component?

Props are immutable (cannot be changed).
If you try to modify them, React will throw a warning or behave unexpectedly.

How to pass multiple props to a component?

You can pass multiple props as key-value pairs.

App.jsx


Enter fullscreen mode Exit fullscreen mode

Are props read-only or mutable?

Props are read-only.
You cannot modify props inside the child component.
If data needs to change, use state instead

What happens if a prop is not passed?

If a prop is missing, it will be undefined unless you use default props.

Top comments (0)