Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Props | Common Principles
Foundations of React Native

PropsProps

Theory

Props (short for properties) are a way to pass data from a parent component to a child component. They are similar to function arguments. Props allow you to customize and configure child components based on the requirements of the parent component.

Why do we need Props?

Props make components reusable and configurable. They enable the parent component to communicate with its children by passing data and functionality to them.

Working with Props

Passing Props

We pass props by including them as attributes when we use a component.

In this example, the ParentComponent passes a prop called message with the value 'Hello from parent' to ChildComponent.

Receiving Props

In the receiving component (ChildComponent in this case), we can access the passed props as properties of the props object.

The ChildComponent receives the message prop and renders it within a Text component.

Default Props

We can define default values for props in case they are not provided.

Code Description
  • Line 5: component accepts props as its argument, which represents the properties passed to it.
  • Line 7: Declares a constant message which is assigned the value of props.message if it's provided. If props.message is not provided (or evaluates to false, e.g., null or undefined), it defaults to 'Default Message'.
  • Line 9: Renders a Text component with the content of the message constant inside.

Here, if message is not provided as a prop, it defaults to 'Default Message'.

Example

Consider a scenario where we have a UserProfile component that receives user data as props.

Code Description
  • Line 5: Defines a functional component named UserProfile that takes a prop named data.
  • Line 6: Destructures the data prop to extract username and bio. This assumes that the data prop is an object with properties username and bio.
  • Lines 8-13: The component renders the username and bio values within a View using Text components.

Now, when we use UserProfile in the App component, we can pass user data as props:

Code Description
  • Lines 7-10: Creates a sample user object with properties username and bio. This data will be passed to the UserProfile component as a prop.
  • Line 15: Renders the UserProfile component and passes the user data as a prop named data. This prop is accessed within the UserProfile component.

In Practice

Все було зрозуміло?

Секція 3. Розділ 2
course content

Зміст курсу

Foundations of React Native

PropsProps

Theory

Props (short for properties) are a way to pass data from a parent component to a child component. They are similar to function arguments. Props allow you to customize and configure child components based on the requirements of the parent component.

Why do we need Props?

Props make components reusable and configurable. They enable the parent component to communicate with its children by passing data and functionality to them.

Working with Props

Passing Props

We pass props by including them as attributes when we use a component.

In this example, the ParentComponent passes a prop called message with the value 'Hello from parent' to ChildComponent.

Receiving Props

In the receiving component (ChildComponent in this case), we can access the passed props as properties of the props object.

The ChildComponent receives the message prop and renders it within a Text component.

Default Props

We can define default values for props in case they are not provided.

Code Description
  • Line 5: component accepts props as its argument, which represents the properties passed to it.
  • Line 7: Declares a constant message which is assigned the value of props.message if it's provided. If props.message is not provided (or evaluates to false, e.g., null or undefined), it defaults to 'Default Message'.
  • Line 9: Renders a Text component with the content of the message constant inside.

Here, if message is not provided as a prop, it defaults to 'Default Message'.

Example

Consider a scenario where we have a UserProfile component that receives user data as props.

Code Description
  • Line 5: Defines a functional component named UserProfile that takes a prop named data.
  • Line 6: Destructures the data prop to extract username and bio. This assumes that the data prop is an object with properties username and bio.
  • Lines 8-13: The component renders the username and bio values within a View using Text components.

Now, when we use UserProfile in the App component, we can pass user data as props:

Code Description
  • Lines 7-10: Creates a sample user object with properties username and bio. This data will be passed to the UserProfile component as a prop.
  • Line 15: Renders the UserProfile component and passes the user data as a prop named data. This prop is accessed within the UserProfile component.

In Practice

Все було зрозуміло?

Секція 3. Розділ 2
some-alt