Table of Contents
Basic method: directly transmit props
Passing functions is also a common requirement
Be careful to avoid unnecessary re-rendering
Home Web Front-end Vue.js How to pass props to a child component?

How to pass props to a child component?

Jul 16, 2025 am 02:11 AM
props Subassembly

The most common way for a parent component to pass data to a child component is through props. The basic method is to directly pass props, for example, in the parent component , which is received and used by the child component through the function parameters; it can also pass a function as a prop, which is used by the child component to notify the parent component of events, such as passing the handleChildClick function as an onAction prop into the child component and called it internally; to avoid unnecessary re-rendering, useCallback should be used to keep the function reference stable, or use useMemo and React.memo to optimize the passing of objects and components.

In React, the most common way for the parent component to pass data to the child component is through props. It is actually not complicated to operate, but there are several key points that need to be paid attention to, especially when you first learn it, it is easy to get confused.

Basic method: directly transmit props

If the parent component wants to pass data to the child component, you only need to add the prop name and value you need in the place where the child component is used, just like writing HTML attributes.

For example, if you have a UserCard component, you want to pass a username from the parent component:

 // <UserCard name="Alice" /> in the parent component

Then in the subcomponent, use the function parameters to receive these props:

 function UserCard(props) {
  return <div>{props.name}</div>;
}

Or if you are using deconstruction, you can also write it like this:

 function UserCard({ name }) {
  return <div>{name}</div>;
}

This method is suitable for passing basic types such as strings, numbers, and boolean values, and is also suitable for passing objects and functions.

Passing functions is also a common requirement

Sometimes you not only want to pass data, but also want the child component to "notify" what happened to the parent component. At this time, you can pass a function as a prop.

For example, if you have a button in the child component, you should tell the parent component after clicking:

 // Parent component function handleChildClick() {
  console.log("Subcomponent triggered");
}

<ChildComponent onAction={handleChildClick} />

Then call this function in the child component:

 function ChildComponent({ onAction }) {
  Return (
    <button onClick={onAction}>
      Click me to notify the parent component</button>
  );
}

This is very common in scenarios where there are many forms and interactions, such as notifying the parent component of the update status when the input box changes.

Be careful to avoid unnecessary re-rendering

Although it is convenient to pass props, if the object or function you pass is a new reference every time, it may cause unnecessary re-rendering of the child components. Especially for components that are more sensitive to optimization performance, the following points can be considered:

  • Use useCallback to keep function references stable
  • Use useMemo or React.memo to optimize the object or component itself passed into the child component

For example, if a new function is created to the child component every time the parent component renders, even if the content of props does not change, the child component will be re-rendered if the reference changes. You can wrap it with useCallback :

 const handleClick = useCallback(() => {
  // Processing logic}, []);

Then pass it to the subcomponent, so the reference will be stable.

Basically that's it. Props is one of the most basic operations in React development. It looks simple, but after a thorough understanding can you write a more efficient component structure.

The above is the detailed content of How to pass props to a child component?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Beginner's Guide to RimWorld: Odyssey
1 months ago By Jack chen
PHP Variable Scope Explained
4 weeks ago By 百草
Tips for Writing PHP Comments
3 weeks ago By 百草
Commenting Out Code in PHP
3 weeks ago By 百草

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1509
276
How to use props and emits in vue3 and specify their type and default value How to use props and emits in vue3 and specify their type and default value May 19, 2023 pm 05:21 PM

Use of defineProps defineProps does not need to be introduced when using it, and it is a global method by default. Use constprops=defineProps({attr1:{type:String,//S must be capitalized default:"",},attr2:Boolean,attr3:{type:Number,required:true,},} in the vue3 project developed by js ); The usage in js environment is similar to that of vue2, except that the optional API is replaced by a combined API. The defined props type and default value are both the same as the vue2 type, which is used in vue3

How to solve Vue error: Unable to use props to pass data How to solve Vue error: Unable to use props to pass data Aug 17, 2023 am 10:06 AM

How to solve Vue error: Unable to use props to pass data Preface: During the development process of Vue, it is very common to use props to transfer data between parent and child components. However, sometimes we may encounter a problem, that is, when using props to pass data, an error will occur. This article will focus on how to solve the error that props cannot be used to pass data in Vue. Problem description: In Vue development, when we use props in the parent component to pass data to the child component, if

How to use props and emit in Vue3 How to use props and emit in Vue3 May 26, 2023 pm 06:13 PM

Function: The parent component passes data down to the child component through props; Purpose: When a type of component needs to be used multiple times, each call is only different in specific places, just like a personal profile form, which needs to be filled out each time. The information is different for each person, but the structure is the same. Usage 1 (simple acceptance without specifying the type): Introduce the subcomponent into the parent component, pass the parameters through the label attribute of the subcomponent, and define a props option in the subcomponent for reception and use. Note that there is no need to add props in the subcomponent. Other places are defined in advance above. You can see that the age passed in is a string type. If you want the passed in value to be automatically increased by 1, you cannot add 1 when using the subcomponent. As shown in the figure below, it will become a string.

VUE3 Quick Start: Use Props to pass data to subcomponents VUE3 Quick Start: Use Props to pass data to subcomponents Jun 15, 2023 pm 09:30 PM

VUE3 is the latest version of Vue.js, which has been greatly enhanced in terms of performance, experience and flexibility. In this article, we will learn how to use VUE3’s Props to pass data to child components. Vue.js is an MVVM (Model-View-ViewModel) framework, which is based on components. Each component has a local state and possible actions, so communication between components is crucial. In Vue.js, parent components can pass Props

How to use $children to access child component instances in Vue How to use $children to access child component instances in Vue Jun 11, 2023 am 10:09 AM

How to use $children to access subcomponent instances in Vue Vue is a JavaScript framework oriented to the MVVM pattern. It provides many convenient APIs to create responsive data, control views, etc. Among them, components are an important concept in Vue, which can make the code more modular, reusable, and easy to maintain. In Vue, each component has its own instance, which can be accessed through this. However, if you want to access the instance of a child component, you need to use the $children attribute. Book

Vue and Vue-Router: How to use route parameters in child components? Vue and Vue-Router: How to use route parameters in child components? Dec 17, 2023 am 10:26 AM

Vue and Vue-Router: How to use route parameters in child components? Introduction: Vue is a popular JavaScript framework for building user interfaces. Vue-Router is Vue's official routing manager, used to implement navigation functions for single-page applications. In application development, we often need to pass parameters between components, and Vue-Router provides a simple way to handle this problem. This article will introduce how to use routing parameters in Vue subcomponents.

VUE3 basic tutorial: props and computed using Vue.js responsive framework VUE3 basic tutorial: props and computed using Vue.js responsive framework Jun 15, 2023 pm 08:44 PM

Vue.js is a popular JavaScript framework for building web applications with responsive systems. Vue.js provides an easy-to-use set of directives and components to simplify the development process. In this article, we will learn an important concept - props and computed. Props are the way to pass information in Vue.js components. It allows us to pass data from parent component to child component. In child components, we can use the passed data for binding and processing

Solve Vue error: Unable to use props correctly for parent-child component communication Solve Vue error: Unable to use props correctly for parent-child component communication Aug 17, 2023 pm 06:25 PM

Solve Vue error: Unable to use props correctly for parent-child component communication In Vue, communication between parent-child components is a very common and important operation. Props are a way to transfer data between Vue components, and it is also the most commonly used way. However, in the process of using props to communicate between parent and child components, sometimes it is inevitable to encounter some problems, such as the inability to use props to transfer data correctly, resulting in error reports. This article will analyze common causes of errors and provide solutions. Common newspapers

See all articles