DEV Community

Cover image for What are Props React?
Ricardo Maia
Ricardo Maia

Posted on

What are Props React?

In React, ๐™ฅ๐™ง๐™ค๐™ฅ๐™จ (short for "properties") are parameters that you can pass to components, allowing you to share data between parent and child components. They are a way to ๐™ข๐™–๐™ ๐™š ๐™˜๐™ค๐™ข๐™ฅ๐™ค๐™ฃ๐™š๐™ฃ๐™ฉ๐™จ ๐™ง๐™š๐™ช๐™จ๐™–๐™—๐™ก๐™š and to pass dynamic information to a component, which can then be used within the component to alter the display or behavior.

๐™†๐™š๐™ฎ ๐™˜๐™๐™–๐™ง๐™–๐™˜๐™ฉ๐™š๐™ง๐™ž๐™จ๐™ฉ๐™ž๐™˜๐™จ ๐™ค๐™› ๐™ฅ๐™ง๐™ค๐™ฅ๐™จ:

  1. ๐—œ๐—บ๐—บ๐˜‚๐˜๐—ฎ๐—ฏ๐—น๐—ฒ: Props are ๐˜๐˜ฎ๐˜ฎ๐˜ถ๐˜ต๐˜ข๐˜ฃ๐˜ญ๐˜ฆ within the component that receives them, meaning a component cannot modify its own props directly. They are passed from the parent component to the child component and remain unchanged during the component's lifecycle.

  2. ๐—ฅ๐—ฒ๐—ฎ๐—ฑ-๐—ผ๐—ป๐—น๐˜†: A child component can access the props but cannot modify them. If you need to change the value of props, this change must be made in the parent component and passed down again to the child.

  3. ๐——๐—ฎ๐˜๐—ฎ ๐˜๐—ฟ๐—ฎ๐—ป๐˜€๐—บ๐—ถ๐˜€๐˜€๐—ถ๐—ผ๐—ป: Props are used to pass data, such as strings, numbers, functions, or even other components, from one component to another.

๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ ๐—ฒ๐˜…๐—ฎ๐—บ๐—ฝ๐—น๐—ฒ ๐—ผ๐—ณ ๐˜‚๐˜€๐—ถ๐—ป๐—ด `๐—ฝ๐—ฟ๐—ผ๐—ฝ๐˜€:

Let's create two components: a parent component and a child. The parent will send data to the child using props.

Image description

Now, the parent component sends a value as a prop to the child component:

Image description

In this example, the parent component (ParentComponent) passes the prop name="Ricardo" to the child component (ChildComponent). The child component receives this prop as an argument (props) and uses it to render the string "Hello, Ricardo!".

More advanced examples of props:

  1. Passing functions as props: You can also pass functions as props, which allows a child component to communicate events back to the parent component.

Image description

In this example, the child component (ChildComponent) receives the function handleClick as a prop and executes it when the button is clicked.

  1. ๐™‹๐™ง๐™ค๐™ฅ๐™จ ๐™ฌ๐™ž๐™ฉ๐™ ๐™™๐™ฎ๐™ฃ๐™–๐™ข๐™ž๐™˜ ๐™ซ๐™–๐™ก๐™ช๐™š๐™จ: Props can also be dynamic values, such as a component's state, data from an API, etc.

Image description

Here, the value of name is stored in the parent component's state and passed to the child component as a prop.

๐—ฆ๐˜‚๐—บ๐—บ๐—ฎ๐—ฟ๐˜†:

  • ๐—ฃ๐—ฟ๐—ผ๐—ฝ๐˜€ allow you to pass data and functions from a parent component to a child component.
  • They are ๐˜๐˜ฎ๐˜ฎ๐˜ถ๐˜ต๐˜ข๐˜ฃ๐˜ญ๐˜ฆ in the component that receives them.
  • They are very useful for making components ๐˜ณ๐˜ฆ๐˜ถ๐˜ด๐˜ข๐˜ฃ๐˜ญ๐˜ฆ and ๐˜ฅ๐˜บ๐˜ฏ๐˜ข๐˜ฎ๐˜ช๐˜ค.
  • You can pass any type of data as props: strings, numbers, arrays, objects, functions, etc.

๐—ฃ๐—ฟ๐—ผ๐—ฝ๐˜€ are one of the most fundamental concepts in React for developing dynamic and modular components.

Top comments (0)