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.
๐๐๐ฎ ๐๐๐๐ง๐๐๐ฉ๐๐ง๐๐จ๐ฉ๐๐๐จ ๐ค๐ ๐ฅ๐ง๐ค๐ฅ๐จ
:
๐๐บ๐บ๐๐๐ฎ๐ฏ๐น๐ฒ: 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.
๐ฅ๐ฒ๐ฎ๐ฑ-๐ผ๐ป๐น๐: 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.
๐๐ฎ๐๐ฎ ๐๐ฟ๐ฎ๐ป๐๐บ๐ถ๐๐๐ถ๐ผ๐ป: 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.
Now, the parent component sends a value as a prop
to the child component:
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
:
- Passing functions as props: You can also pass functions as props, which allows a child component to communicate events back to the parent component.
In this example, the child component (ChildComponent
) receives the function handleClick
as a prop and executes it when the button is clicked.
- ๐๐ง๐ค๐ฅ๐จ ๐ฌ๐๐ฉ๐ ๐๐ฎ๐ฃ๐๐ข๐๐ ๐ซ๐๐ก๐ช๐๐จ: Props can also be dynamic values, such as a component's state, data from an API, etc.
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)