React
嵌套内容作为 children,具名区域继续使用普通 prop。
在线预览
本周学习进度
已经完成 3 篇 React / Vue 对比笔记。
正文作为 children,按钮作为 footer prop。
组件关系与数据流
ComponentContent OutletContent
差异焦点内容是父组件创建的 React 元素对象,并随 props 一起传递。
实线表示依赖、数据或命令传递,虚线表示事件、回调或清理路径。
flowchart TB
accTitle: React children 与元素 prop
accDescr: ReactDemo 在每次 render 中创建正文和按钮 React 元素,分别作为 children 与 footer prop 传给 ProfileCard,子组件直接把 props 放进自己的 JSX。
parent["ReactDemo render"] --> elements[("创建 React elements")]
elements -->|"children prop"| card["ProfileCard"]
elements -->|"footer prop"| card
card --> output[("组合后的元素树")]
output -.->|"按钮事件更新父 state"| parent
class parent,card component
class elements,output resource
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嵌套 JSX 最终也是 children prop,具名区域通常再设计一个普通 prop。
实际运行源码
import { useState } from "react";import { ProfileCard } from "./ProfileCard";
export function ReactDemo() { const [completed, setCompleted] = useState(3);
return ( <ProfileCard title="本周学习进度" footer={ <button className="demo-button" type="button" onClick={() => setCompleted((current) => current + 1)} > 完成一篇 </button> } > <p>已经完成 {completed} 篇 React / Vue 对比笔记。</p> <p className="demo-muted">正文作为 children,按钮作为 footer prop。</p> </ProfileCard> );}import type { ReactNode } from "react";
type ProfileCardProps = { children: ReactNode; footer: ReactNode; title: string;};
export function ProfileCard({ children, footer, title }: ProfileCardProps) { return ( <section className="demo-card"> <span className="demo-tag">children / footer prop</span> <h2>{title}</h2> <div>{children}</div> <div className="demo-actions">{footer}</div> </section> );}