当前位置: 首页 > news >正文

React@16.x(16)Render Props

目录

  • 1,问题描述
  • 2,解决方式
    • 2.1,Render Props
    • 2.2,HOC
  • 3,使用场景

1,问题描述

当使用组件时,标签中的内容,会被当做 props.children 来渲染:

子组件:

import React, { PureComponent } from "react";export default class MyComponent extends PureComponent {state = {x: 0,y: 0,};render() {return <div>{this.props.children}</div>;}
}

父组件使用:

import React from "react";
import MyComponent from "./MyComponent";export default function index() {return (<div><MyComponent><h2>1次使用</h2></MyComponent><MyComponent><h2>2次使用</h2></MyComponent></div>);
}

现在的需求:父组件中如何使用子组件的状态?

2,解决方式

2.1,Render Props

注意到在子组件中默认渲染的是 props.children,那如果像上下文 <ctx.Consumer></ctx.Consumer> 一样,通过函数参数来传递指定内容,就可以解决了。

所以,props.children 变成函数即可

import React, { PureComponent } from "react";export default class MyComponent extends PureComponent {state = {x: 1,y: 2,};render() {return <div>{this.props.children(this.state)}</div>;}
}

使用

import React from "react";
import MyComponent from "./MyComponent";export default function index() {return (<div><MyComponent>{(childrenState) => <h2>{childrenState.x}</h2>}</MyComponent><MyComponent>{(childrenState) => <h2>{childrenState.y}</h2>}</MyComponent></div>);
}

另外,一般这种情况不会用 props.children,而是使用约定俗成的 props.render 来表示。

// 子组件
<div>{this.props.render(this.state)}</div>;// 父组件
<MyComponent render={(childrenState) => <h2>{childrenState.x}</h2>} />

注意,因为子组件 extends PureComponent,所以父组件应该将这个函数单独声明才行,否则每次都会重新渲染。(具体原因看这篇文章)

修改父组件如下:

import React from "react";
import MyComponent from "./MyComponent";const renderA = (childrenState) => <h2>{childrenState.x}</h2>;export default function index() {return (<div><MyComponent render={renderA}></MyComponent></div>);
}

2.2,HOC

高阶组件也能解决这个问题,但相比 Render props 有点繁琐。

import React, { PureComponent } from "react";export default function withState(Comp) {return class MyComponent extends PureComponent {state = {x: 1,y: 2,};render() {return <Comp {...this.props} x={this.state.x} />;}};
}

父组件使用

import React from "react";
import withState from "./withState";function ChildA(props) {return <h2>{props.x}</h2>;
}const ChildStateA = withState(ChildA);export default function index() {return (<div><ChildStateA /></div>);
}

3,使用场景

可以看到,效果类似 vue中作用域插槽 。

所以大多的使用场景:某些组件的各个功能和处理逻辑相同,只是渲染的UI不同


以上。

http://www.lryc.cn/news/361631.html

相关文章:

  • STM32 定时器问题
  • CSS学习笔记目录
  • 随笔-我在武汉一周了
  • Python 爬虫零基础:探索网络数据的神秘世界
  • 微信小程序的view的属性值和用法
  • Python优化、异常处理与性能提升技巧
  • Flink状态State | 大数据技术
  • go语言方法之方法值和方法表达式
  • TDMQ CKafka 版弹性存储能力重磅上线!
  • 24、Linux网络端口
  • Mysql全文搜索和LIKE搜索有什么区别
  • elementplu父级页面怎么使用封装子组件原组件的方法
  • el-date-picker选择开始日期的近半年
  • C++
  • nginx源码阅读理解 [持续更新,建议关注]
  • 笔试训练2
  • 构建坚不可摧的Web安全防线:深入剖析二阶注入与全面防御策略
  • (4) qml动态元素
  • 深度神经网络——什么是梯度下降?
  • 基本元器件 - 二极管
  • 【设计模式】单例模式(创建型)⭐⭐⭐
  • 《深入浅出C语言:从基础到指针的全面指南》
  • Typescript高级: 深入实践Record类型
  • 重构与优化-对象间特性搬移重构(2)
  • 网络流量监控与DNS流量分析
  • 【数据分析】打造完美数据分析环境:Python开发环境搭建全攻略
  • 我的app开始养活我了
  • linux中最基础使用的命令
  • 【算法实战】每日一题:17.1 订单处理问题(差分思想,二分搜索)
  • UML静态图-对象图