React
回调函数作为 props,由子组件主动调用。
在线预览
Parent
组件通信
父组件把 onSend 回调交给子组件。
父组件收到:还没有收到消息
组件关系与数据流
StateComponent
差异焦点Callback 是普通函数 prop,子组件知道并直接调用它。
实线表示依赖、数据或命令传递,虚线表示事件、回调或清理路径。
flowchart TB
accTitle: React 回调属性通信机制
accDescr: ReactDemo 把函数作为 onSend prop 传给 MessageInput,子组件直接调用函数,setMessage 调度父组件重新渲染。
parent["ReactDemo"] -->|"传入 onSend 函数 prop"| child["MessageInput"]
input(["value state"]) --> child
child -.->|"直接调用 onSend(message)"| setter{{"setMessage"}}
setter --> message(["message state"])
message -->|"重新渲染"| parent
class input,message state
class parent,child component
class setter process
classDef state fill:#ffffff,stroke:#149eca,color:#172033,stroke-width:1.2px
classDef component fill:#e5f6fb,stroke:#149eca,color:#172033,stroke-width:2px,font-weight:600
classDef process fill:#f4fbfd,stroke:#149eca,color:#172033,stroke-width:1.6px
classDef resource fill:#f3f5f8,stroke:#778195,color:#172033,stroke-width:1.4px函数作为 props 向下传递;子组件调用函数后,setter 调度父组件重新渲染。
实际运行源码
import { useState } from "react";
type MessageInputProps = { onSend: (message: string) => void;};
function MessageInput({ onSend }: MessageInputProps) { const [value, setValue] = useState("");
function handleSubmit(event: React.FormEvent<HTMLFormElement>) { event.preventDefault(); const message = value.trim(); if (!message) return; onSend(message); setValue(""); }
return ( <form className="demo-form" onSubmit={handleSubmit}> <label className="demo-field"> 子组件输入 <input className="demo-input" value={value} placeholder="输入一条消息" onChange={(event) => setValue(event.target.value)} /> </label> <div className="demo-actions"> <button className="demo-button" type="submit"> 发送给父组件 </button> </div> </form> );}
export function ReactDemo() { const [message, setMessage] = useState("还没有收到消息");
return ( <div className="demo-stack"> <div className="demo-card"> <span className="demo-tag">Parent</span> <h2>组件通信</h2> <p>父组件把 onSend 回调交给子组件。</p> </div> <MessageInput onSend={setMessage} /> <p className="demo-result" aria-live="polite"> 父组件收到:{message} </p> </div> );}