千家信息网

React Props的原理是什么

发表于:2025-11-16 作者:千家信息网编辑
千家信息网最后更新 2025年11月16日,本篇文章为大家展示了React Props的原理是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。props理解props 是 React 组件通信最重要的
千家信息网最后更新 2025年11月16日React Props的原理是什么

本篇文章为大家展示了React Props的原理是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

    props理解

    props 是 React 组件通信最重要的手段

    props:对于在 React 应用中写的子组件,父组件绑定在它们标签里的 属性和方法,最终会变成 props 传递给它们。

    1)props 可以是:

    • ① props 作为一个子组件渲染数据源。

    • ② props 作为一个通知父组件的回调函数。

    • ③ props 作为一个单纯的组件传递。

    • ④ props 作为渲染函数。

    • ⑤ render props , 和④的区别是放在了 children 属性上。

    • ⑥ render component 插槽组件。

    /* children 组件 */function ChidrenComponent(){    return 
    In this chapter, let's learn about react props !
    }/* props 接受处理 */class PropsComponent extends React.Component{ componentDidMount(){ console.log(this,'_this') } render(){ const { children , mes , renderName , say ,Component } = this.props const renderFunction = children[0] const renderComponent = children[1] /* 对于子组件,不同的props是怎么被处理 */ return
    { renderFunction() } { mes } { renderName() } { renderComponent }
    }}/* props 定义绑定 */class Index extends React.Component{ state={ mes: "hello,React" } node = null say= () => this.setState({ mes:'let us learn React!' }) render(){ return
    my name is alien
    } // ④ props 作为渲染函数 > { ()=>
    hello,world
    } { /* ⑤render props */ } { /* ⑥render component */ }
    }}

    2)props在React充当角色(3个角度):

    ① 组件层级

    • 父传子:props子传父:props 的 callback

    • 将视图容器作为 props 进行渲染

    ② 更新机制

    在 fiber 调和阶段中,diff 可以说是 React 更新的驱动器,props 可以作为组件是否更新的重要准则

    (PureComponentmemo 等性能优化方案)

    ③ 插槽层面

    组件的闭合标签里的插槽,转化成 chidren 属性

    3)监听props改变:

    类组件: componentWillReceiveProps(废弃) componentWillReceiveProps(新)函数组件: useEffect (初始化会默认执行一次) props chidren模式

    ① props 插槽组件

        

    在 Container 组件中,通过 props.children 属性访问到 Chidren 组件,为 React element 对象。

    作用:

    • 可以根据需要控制 Chidren 是否渲染。

    • Container 可以用 React.cloneElement 强化 props (混入新的 props ),或者修改 Chidren 的子元素。

    ② render props模式

       { (ContainerProps)=>  }--------------------------------------------------------------------------------Container组件:function  Container(props) {    const  ContainerProps = {        name: 'alien',        mes:'let us learn react'    }     return  props.children(ContainerProps)}

    根据需要控制 Chidren 渲染与否。可以将需要传给 Children 的 props 直接通过函数参数的方式传递给执行函数 children 。

    操作 props

    1、抽象 props

    用于跨层级传递 props ,一般不需要具体指出 props 中某个属性,而是将 props 直接传入或者是抽离到子组件中。

    1)混入 props

    给父组件 props 中混入某个属性,再传递给子组件

    function Son(props){    console.log(props)    return 
    hello,world
    }function Father(props){ const fatherProps={ mes:'let us learn React !' } return }function Index(){ const indexProps = { name:'alien', age:'28', } return }
    2)抽离 props

    从父组件 props 中抽离某个属性,再传递给子组件

    function Son(props){    console.log(props)    return 
    hello,world
    }function Father(props){ const { age,...fatherProps } = props return }function Index(){ const indexProps = { age:'28', mes:'let us learn React !' } return }

    2、注入 props

    1)显式注入 props

    能够直观看见标签中绑定的 props

    function Son(props){    console.log(props)    return 
    hello,world
    }function Father(props){ const fatherProps={ mes:'let us learn React !' } return }function Index(){ const indexProps = { name:'alien', age:'28', } return }
    2)隐式注入 props

    一般通过 React.cloneElement 对 props.chidren 克隆再混入新的 props

    function Son(props){     console.log(props) // {name: "alien", age: "28", mes: "let us learn React !"}     return 
    hello,world
    }function Father(prop){ return React.cloneElement(prop.children,{ mes:'let us learn React !' })}function Index(){ return }

    上述内容就是React Props的原理是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

    组件 属性 函数 插槽 标签 更新 原理 重要 内容 层级 技能 模式 知识 处理 控制 不同 简明 直观 简明扼要 个子 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 无线网络技术带来的社会发展 安全风控和网络安全一样吗 福建多升金网络技术有限公司 负责华为服务器业务是哪个子公司 阿里云数据库中病毒 什么是网络安全顾问 dchp服务器地址池 主投互联网金融科技公司的基金 济南浪潮存储服务器服务 穿越火线排位总是服务器异常 中职男生学计算机网络技术好吗 开机禁止服务器管理器 网络安全学院落户 ios14邮箱收件服务器设置 山东 网络安全公司 邯郸程序软件开发服务费 四年级手抄报网络安全看清楚 scum官方硬核服务器 封面设计图片素材软件开发 于都网络安全监察部门在哪 金牛物联互联网科技公司 致远移动协同怎么设置服务器参数 信阳市网络安全委员会 战地5您与ea服务器断开 金山区本地网络技术哪家强 软件开发的11种模式分别是 如何改变数据库字符集 石林方便软件开发价格走势 河大商学院高考试数据库考试 更改dns服务器有什么要求吗
    0