React RenderProps模式运用过程浅析

 更新时间:2023年03月06日 09:20:54   作者:ฅQSω[*邱╭  
render props是指一种在 React 组件之间使用一个值为函数的 prop 共享代码的技术。简单来说,给一个组件传入一个prop,这个props是一个函数,函数的作用是用来告诉这个组件需要渲染什么内容,那么这个prop就成为render prop

1.引入

上代码:

import React, { Component } from 'react'
import './index.css'
export default class Parent extends Component {
	render() {
		return (
			<div className="parent">
				<h3>我是Parent组件</h3>
				<A/>
			</div>
		)
	}
}
class A extends Component {
	render() {
		console.log(this.props);
		return (
			<div className="a">
				<h3>我是A组件</h3>
			</div>
		)
	}
}

结果很简单就能猜到

改一下呢?

import React, { Component } from 'react'
import './index.css'
export default class Parent extends Component {
	render() {
		return (
			<div className="parent">
				<h3>我是Parent组件</h3>
				<A>Hello !</A>
			</div>
		)
	}
}
class A extends Component {
	render() {
		console.log(this.props);
		return (
			<div className="a">
				<h3>我是A组件</h3>
			</div>
		)
	}
}

页面是没有现实Hello !的,但是之前一次的封装NaLink也有传递过标签体内容的,在子组件的props中,children:(内容)

所以A组件想要展示传递的标签体内容的话,还要改一下A组件

class A extends Component {
	render() {
		console.log(this.props);
		return (
			<div className="a">
				<h3>我是A组件</h3>
				{this.props.children}
			</div>
		)
	}
}

2.改一下呢

import React, { Component } from 'react'
import './index.css'
export default class Parent extends Component {
	render() {
		return (
			<div className="parent">
				<h3>我是Parent组件</h3>
				<A>
					<B/>
				</A>
			</div>
		)
	}
}
class A extends Component {
	state ={ name:'Mike'}
	render() {
		console.log(this.props);
		return (
			<div className="a">
				<h3>我是A组件</h3>
				{this.props.children}
			</div>
		)
	}
}
class B extends Component {
	render() {
		console.log('B--render');
		return (
			<div className="b">
				<h3>我是B组件</h3>
			</div>
		)
	}
}

A,B组件成了父子组件

但是这样,如果A组件想传自己的值给B组件,貌似是行不通的

3.再改一下呢

import React, { Component } from 'react'
import './index.css'
import C from '../1_setState'
export default class Parent extends Component {
	render() {
		return (
			<div className="parent">
				<h3>我是Parent组件</h3>
				<A render={(name) => <B name={name}/>} />
			</div>
		)
	}
}
class A extends Component {
	state ={ name:'Mike'}
	render() {
		const {name} =this.state;
		console.log(this.props);
		return (
			<div className="a">
				<h3>我是A组件</h3>
				{this.props.render(name)}
			</div>
		)
	}
}
class B extends Component {
	render() {
		console.log('B--render');
		return (
			<div className="b">
				<h3>我是B组件,接收到的name:{this.props.name}</h3>
			</div>
		)
	}
}

主要是Parent组件和A组件之间调用要注意:

Parent组件中,render(当然可以去其他的名字这里)这样写,相当于预留了一个插槽,如果你需要渲染其他组件(例如例子中的B组件),在A组件中调用this.props.render()就可以渲染出B组件,不写的话就不会渲染出B组件

4.总结一下

如何向组件内部动态传入带内容的结构(标签)

Vue中:

使用slot技术, 也就是通过组件标签体传入结构

React中:

使用children props: 通过组件标签体传入结构

使用render props: 通过组件标签属性传入结构, 一般用render函数属性

children props

<A>
  <B>xxxx</B>
</A>
{this.props.children}
问题: 如果B组件需要A组件内的数据, ==> 做不到 

render props

<A render={(data) => <C data={data}></C>}></A>
A组件: {this.props.render(内部state数据)}
C组件: 读取A组件传入的数据显示 {this.props.data} 

到此这篇关于React RenderProps模式运用过程浅析的文章就介绍到这了,更多相关React RenderProps内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • React+TypeScript项目中使用CodeMirror的步骤

    React+TypeScript项目中使用CodeMirror的步骤

    CodeMirror被广泛应用于许多Web应用程序和开发工具,之前做需求用到过codeMirror这个工具,觉得还不错,功能很强大,所以记录一下改工具的基础用法,对React+TypeScript项目中使用CodeMirror的步骤感兴趣的朋友跟随小编一起看看吧
    2023-07-07
  • react中hook介绍以及使用教程

    react中hook介绍以及使用教程

    这篇文章主要给大家介绍了关于react中hook及使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • 如何在react项目中做公共配置文件

    如何在react项目中做公共配置文件

    这篇文章主要介绍了如何在react项目中做公共配置文件问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • 详解react组件通讯方式(多种)

    详解react组件通讯方式(多种)

    这篇文章主要介绍了详解react组件通讯方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • react-router4 嵌套路由的使用方法

    react-router4 嵌套路由的使用方法

    本篇文章主要介绍了react-router4 嵌套路由的使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • React Navigation 使用中遇到的问题小结

    React Navigation 使用中遇到的问题小结

    本篇文章主要介绍了React Navigation 使用中遇到的问题小结,主要是安卓和iOS中相对不协调的地方,特此记录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • React为什么需要Scheduler调度器原理详解

    React为什么需要Scheduler调度器原理详解

    这篇文章主要为大家介绍了React为什么需要Scheduler调度器原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • React类组件中super()和super(props)的区别详解

    React类组件中super()和super(props)的区别详解

    这篇文章给大家详细介绍了React类组件中super()和super(props)有什么区别,文中通过代码示例给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-01-01
  • React 组件中的 bind(this)示例代码

    React 组件中的 bind(this)示例代码

    这篇文章主要介绍了 React 组件中的 bind(this) ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-09-09
  • react-native 配置@符号绝对路径配置和绝对路径没有提示的问题

    react-native 配置@符号绝对路径配置和绝对路径没有提示的问题

    本文主要介绍了react-native 配置@符号绝对路径配置和绝对路径没有提示的问题,文中通过图文示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-01-01

最新评论