React之使用Props的小技巧

简介: React之使用Props的小技巧

props 操作小技巧


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

2,混入 props


function Son(props) {
    console.log(props)
    return <div>hello,wowld</div>
}
function Father(props) {
    const fatherProps = {
        mes: 'let us learn React',
    }
    return <Son {...props} {...fatherProps} />
}
function Index() {
    const indexProps = {
        name: 'alien',
        age: '28',
    }
    return <Father {...indexProps} />
}

结果:

886abc074960f2487d58e3c0f0da4cfa.png

Father组件一方面直接将Index组件的IndexProps抽象传递给了Son组件,一方面也混入了自己的fatherProps


3,抽离Props


有时候如果要从父组件Props中抽离某个属性,再传递给子组件。


function Son(props) {
    console.log(props)
    return <div>hello,world</div>
}
function Father(props){
    const {age,...fatherProps} = props
    return <Son {...fatherProps}></Son>
}
function Index () {
    const indexProps = {
        name: 'zhaomin',
        age: '29',
        mes: 'let us learn React!'
    }
    return <Father {...indexProps}></Father>
}

打印结果

834f975d0c77dac7bed6c1bf03b58118.png


4,注入props

a. 显式注入props(指的就是能够直观看见标签中绑定的props)

function Son(props){
    console.log(props) // name: zhaomin age: 28
      return <div>hello,world</div>
}
function Father (props) {
    return props.children
}
function Index () {
    return <Father>
        <Son name="zhaomin" age="28"></Son>
    </Father>
}

b.隐式注入 props (指的是通过React.cloneElement 对props.children 克隆再混入新的props)

function Son (props) {
    console.log(props) // name: zhaomin age: 28  mes: 这是测试de 
      return <div>hello,world</div>
}
function Father (props) {
   return React.cloneElement(props.children,{mes: '这是测试de'}) 
}
function Index () {
    return <Father>
        <Son name="zhaomin" age="28"></Son>
    </Father>
}


关于props.children的操作

ddd8f4d32912488ab85130fdcb1f3bb0.png

打印效果

447f615b2aaef6a0eb8ea8008f060850.png

上述代码中,Container 为父级组件,Children为子级组件,父级组件嵌套子级组件的时候,在父级组件中可以通过props.children获取到父组件下的子级组件的元素。


代码为:

props.children.map || props.children.forEach


在获取到子元素后通过React.cloneElement 将数据混入props中。

目录
相关文章
|
5月前
|
前端开发 JavaScript 安全
除了高阶组件和render props,还有哪些在 React 中实现代码复用的方法?
除了高阶组件和render props,还有哪些在 React 中实现代码复用的方法?
246 62
|
前端开发
React函数式组件props的使用(六)
【8月更文挑战第14天】React函数式组件props的使用(六)
151 1
React函数式组件props的使用(六)
|
前端开发 JavaScript
react学习(13)props
react学习(13)props
140 7
|
前端开发
学习react基础(2)_props、state和style的使用
本文介绍了React中组件间数据传递的方式,包括props和state的使用,以及如何在React组件中使用style样式。
162 0
|
前端开发
React添加路径别名alias、接受props默认值、并二次封装antd中Modal组件与使用
本文介绍了在React项目中如何添加路径别名alias以简化模块引入路径,设置组件props的默认值,以及如何二次封装Ant Design的Modal组件。文章还提供了具体的代码示例,包括配置Webpack的alias、设置defaultProps以及封装Modal组件的步骤和方法。
266 1
React添加路径别名alias、接受props默认值、并二次封装antd中Modal组件与使用
|
前端开发 JavaScript
React 中的 props 属性传递技巧
【9月更文挑战第6天】本文详细介绍了React中`props`的基本用法,包括传递基本数据类型、对象和数组。文章通过多个代码示例展示了如何正确使用`props`,并探讨了常见的问题及解决方法,如`props`不可变性、默认值设置及类型检查等。正确掌握这些技巧有助于提升编程效率,编写出更健壮的代码。
269 17
|
前端开发 JavaScript
React类组件props的使用(五)
【8月更文挑战第14天】React类组件props的使用(五)
127 1
React类组件props的使用(五)
|
前端开发
react学习(15)函数式组件中使用props
react学习(15)函数式组件中使用props
107 8
|
前端开发
react学习(14)类式组件的构造器与props
react学习(14)类式组件的构造器与props
127 6
|
前端开发 JavaScript CDN
React Props
10月更文挑战第8天
96 0