「React进阶」 React全部api解读+基础实践大全(夯实基础2万字总结)(工具类)

简介: 一份不错的 React 学习指南

接下来我们一起来探究一下react工具类函数的用法。

utils.jpg

createElement

一提到createElement,就不由得和JSX联系一起。我们写的jsx,最终会被 babel,用createElement编译成react元素形式。我写一个组件,我们看一下会被编译成什么样子,

如果我们在render里面这么写:

render(){
   
   
    return <div className="box" >
        <div className="item"  >生命周期</div>
        <Text  mes="hello,world"  />
        <React.Fragment> Flagment </React.Fragment>
        {
   
    /*  */ }
        text文本
    </div>
}

会被编译成这样:

render() {
   
   
    return React.createElement("div", {
   
    className: "box" },
            React.createElement("div", {
   
    className: "item" }, "\u751F\u547D\u5468\u671F"),
            React.createElement(Text, {
   
    mes: "hello,world" }),
            React.createElement(React.Fragment, null, " Flagment "),
            "text\u6587\u672C");
    }

当然我们可以不用jsx模式,而是直接通过createElement进行开发。

createElement模型:

React.createElement(
  type,
  [props],
  [...children]
)

createElement参数:

第一个参数:如果是组件类型,会传入组件,如果是dom元素类型,传入div或者span之类的字符串。

第二个参数::第二个参数为一个对象,在dom类型中为属性,在组件类型中为props

其他参数:,依次为children,根据顺序排列。

createElement做了些什么?

经过createElement处理,最终会形成 $$typeof = Symbol(react.element)对象。对象上保存了该react.element的信息。

cloneElement

可能有的同学还傻傻的分不清楚cloneElementcreateElement区别和作用。

createElement把我们写的jsx,变成element对象; 而cloneElement的作用是以 element 元素为样板克隆并返回新的 React 元素。返回元素的 props 是将新的 props 与原始元素的 props 浅层合并后的结果。

那么cloneElement感觉在我们实际业务组件中,可能没什么用,但是在一些开源项目,或者是公共插槽组件中用处还是蛮大的,比如说,我们可以在组件中,劫持children element,然后通过cloneElement克隆element,混入props。经典的案例就是 react-router中的Swtich组件,通过这种方式,来匹配唯一的 Route并加以渲染。

我们设置一个场景,在组件中,去劫持children,然后给children赋能一些额外的props:

function FatherComponent({
   
    children }){
   
   
    const newChildren = React.cloneElement(children, {
   
    age: 18})
    return <div> {
   
    newChildren } </div>
}

function SonComponent(props){
   
   
    console.log(props)
    return <div>hello,world</div>
}

class Index extends React.Component{
   
       
    render(){
   
         
        return <div className="box" >
            <FatherComponent>
                <SonComponent name="alien"  />
            </FatherComponent>
        </div>   
    }
}

打印:

cloneElment.jpg

完美达到了效果!

createContext

createContext用于创建一个Context对象,createContext对象中,包括用于传递 Context 对象值 valueProvider,和接受value变化订阅的Consumer

const MyContext = React.createContext(defaultValue)

createContext接受一个参数defaultValue,如果Consumer上一级一直没有Provider,则会应用defaultValue作为value只有当组件所处的树中没有匹配到 Provider 时,其 defaultValue 参数才会生效。

我们来模拟一个 Context.ProviderContext.Consumer的例子:

function ComponentB(){
   
   
    /* 用 Consumer 订阅, 来自 Provider 中 value 的改变  */
    return <MyContext.Consumer>
        {
   
    (value) => <ComponentA  {
   
   ...value} /> }
    </MyContext.Consumer>
}

function ComponentA(props){
   
   
    const {
   
    name , mes } = props
    return <div> 
            <div> 姓名: {
   
    name }  </div>
            <div> 想对大家说: {
   
    mes }  </div>
         </div>
}

function index(){
   
   
    const [ value , ] = React.useState({
   
   
        name:'alien',
        mes:'let us learn React '
    })
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <MyContext.Provider value={
   
   value}  >
          <ComponentB />
    </MyContext.Provider>
    </div>
}

打印结果:

createContent.jpg

ProviderConsumer的良好的特性,可以做数据的Consumer一方面传递value,另一方面可以订阅value的改变。

Provider还有一个特性可以层层传递value,这种特性在react-redux中表现的淋漓尽致。

createFactory

React.createFactory(type)

返回用于生成指定类型 React 元素的函数。类型参数既可以是标签名字符串(像是 'div' 或 'span'),也可以是 React 组件 类型 ( class 组件或函数组件),或是 React fragment 类型。

使用:

 const Text = React.createFactory(()=><div>hello,world</div>) 
function Index(){
   
     
    return <div style={
   
   {
   
    marginTop:'50px'  }} >
        <Text/>
    </div>
}

效果

createFactory.jpg

报出警告,这个api将要被废弃,我们这里就不多讲了,如果想要达到同样的效果,请用React.createElement

createRef

createRef可以创建一个 ref 元素,附加在react元素上。

用法:

class Index extends React.Component{
   
   
    constructor(props){
   
   
        super(props)
        this.node = React.createRef()
    }
    componentDidMount(){
   
   
        console.log(this.node)
    }
    render(){
   
   
        return <div ref={
   
   this.node} > my name is alien </div>
    }
}

个人觉得createRef这个方法,很鸡肋,我们完全可以class类组件中这么写,来捕获ref

class Index extends React.Component{
   
   
    node = null
    componentDidMount(){
   
   
        console.log(this.node)
    }
    render(){
   
   
        return <div ref={
   
   (node)=> this.node } > my name is alien </div>
    }
}

或者在function组件中这么写:

function Index(){
   
   
    const node = React.useRef(null)
    useEffect(()=>{
   
   
        console.log(node.current)
    },[])
    return <div ref={
   
   node} >  my name is alien </div>
}

isValidElement

这个方法可以用来检测是否为react element元素,接受待验证对象,返回true或者false。这个api可能对于业务组件的开发,作用不大,因为对于组件内部状态,都是已知的,我们根本就不需要去验证,是否是react element 元素。
但是,对于一起公共组件或是开源库,isValidElement就很有作用了。

实践

我们做一个场景,验证容器组件的所有子组件,过滤到非react element类型。

没有用isValidElement验证之前:

const Text = () => <div>hello,world</div> 
class WarpComponent extends React.Component{
   
   
    constructor(props){
   
   
        super(props)
    }
    render(){
   
   
        return this.props.children
    }
}
function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
            <Text/>
            <div> my name is alien </div>
            Let's learn react together!
        </WarpComponent>
    </div>
}

过滤之前的效果

isValidElement.jpg

我们用isValidElement进行react element验证:

class WarpComponent extends React.Component{
   
   
    constructor(props){
   
   
        super(props)
        this.newChidren = this.props.children.filter(item => React.isValidElement(item) )
    }
    render(){
   
   
        return this.newChidren
    }
}

过滤之后效果

isValidElement111.jpg

过滤掉了非react elementLet's learn react together!

Children.map

接下来的五个api都是和react.Chidren相关的,我们来分别介绍一下,我们先来看看官网的描述,React.Children 提供了用于处理 this.props.children 不透明数据结构的实用方法。

有的同学会问遍历 children用数组方法,mapforEach 不就可以了吗? 请我们注意一下不透明数据结构,什么叫做不透明结构?

我们先看一下透明的结构:

class Text extends React.Component{
   
   
    render(){
   
   
        return <div>hello,world</div>
    }
}
function WarpComponent(props){
   
   
    console.log(props.children)
    return props.children
}
function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
            <Text/>
            <Text/>
            <Text/>
            <span>hello,world</span>
        </WarpComponent>
    </div>
}

打印

chidrenmap.jpg

但是我们把Index结构改变一下:

function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
            {
   
    new Array(3).fill(0).map(()=><Text/>) }
            <span>hello,world</span>
        </WarpComponent>
    </div>
}

打印

chidrenmap2.jpg

这个数据结构,我们不能正常的遍历了,即使遍历也不能遍历,每一个子元素。此时就需要 react.Chidren 来帮忙了。

但是我们把WarpComponent组件用react.Chidren处理children:

function WarpComponent(props){
   
   
    const newChildren = React.Children.map(props.children,(item)=>item)
    console.log(newChildren)
    return newChildren
}

此时就能正常遍历了,达到了预期效果。

C71364B2-25E8-4F7D-A26D-50CA36AF4E33.jpg

注意
如果 children 是一个 Fragment 对象,它将被视为单一子节点的情况处理,而不会被遍历。

Children.forEach

Children.forEachChildren.map 用法类似,Children.map可以返回新的数组,Children.forEach仅停留在遍历阶段。

我们将上面的WarpComponent方法,用Children.forEach改一下。

function WarpComponent(props){
   
   
    React.Children.forEach(props.children,(item)=>console.log(item))
    return props.children
}

Children.count

children 中的组件总数量,等同于通过 mapforEach 调用回调函数的次数。对于更复杂的结果,Children.count可以返回同一级别子组件的数量。

我们还是把上述例子进行改造:

function WarpComponent(props){
   
   
    const childrenCount =  React.Children.count(props.children)
    console.log(childrenCount,'childrenCount')
    return props.children
}   
function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
            {
   
    new Array(3).fill(0).map((item,index) => new Array(2).fill(1).map((item,index1)=><Text key={
   
   index+index1} />)) }
            <span>hello,world</span>
        </WarpComponent>
    </div>
}

效果:

chidrencunt.jpg

Children.toArray

Children.toArray返回,props.children扁平化后结果。

function WarpComponent(props){
   
   
    const newChidrenArray =  React.Children.toArray(props.children)
    console.log(newChidrenArray,'newChidrenArray')
    return newChidrenArray
}   
function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
            {
   
    new Array(3).fill(0).map((item,index)=>new Array(2).fill(1).map((item,index1)=><Text key={
   
   index+index1} />)) }
            <span>hello,world</span>
        </WarpComponent>
    </div>
}

效果:

chuldeanarrgy.jpg

newChidrenArray ,就是扁平化的数组结构。React.Children.toArray() 在拉平展开子节点列表时,更改 key 值以保留嵌套数组的语义。也就是说, toArray 会为返回数组中的每个 key 添加前缀,以使得每个元素 key 的范围都限定在此函数入参数组的对象内。

Children.only

验证 children 是否只有一个子节点(一个 React 元素),如果有则返回它,否则此方法会抛出错误。

不唯一

function WarpComponent(props){
   
   
    console.log(React.Children.only(props.children))
    return props.children
}   
function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
            {
   
    new Array(3).fill(0).map((item,index)=><Text key={
   
   index} />) }
            <span>hello,world</span>
        </WarpComponent>
    </div>
}

效果

falseonly.jpg

唯一

function WarpComponent(props){
   
   
    console.log(React.Children.only(props.children))
    return props.children
}   
function Index(){
   
   
    return <div style={
   
   {
   
    marginTop:'50px' }} >
        <WarpComponent>
           <Text/>
        </WarpComponent>
    </div>
}

效果

only.jpg

React.Children.only() 不接受 React.Children.map() 的返回值,因为它是一个数组而并不是 React 元素。

相关文章
|
19天前
|
前端开发 JavaScript
深入理解并实践React Hooks —— useEffect与useState
深入理解并实践React Hooks —— useEffect与useState
84 1
|
11天前
|
存储 JSON API
深入解析RESTful API设计原则与实践
【9月更文挑战第21天】在数字化时代,后端开发不仅仅是编写代码那么简单。它关乎于如何高效地连接不同的系统和服务。RESTful API作为一套广泛采用的设计准则,提供了一种优雅的解决方案来简化网络服务的开发。本文将带你深入了解RESTful API的核心设计原则,并通过实际代码示例展示如何将这些原则应用于日常的后端开发工作中。
|
10天前
|
JSON API 网络架构
掌握RESTful API设计的艺术:从理论到实践
【9月更文挑战第22天】在数字化时代,API已成为连接不同软件和服务的桥梁。本文将引导你了解如何设计高效、可扩展且易于使用的RESTful API,涵盖从基础概念到高级最佳实践的全方位知识。我们将一起探索REST原则、HTTP方法的正确应用、资源命名策略等关键元素,并配以实际代码示例,让你能够将理论转化为实际操作,构建出既符合标准又能应对复杂业务需求的API。
|
24天前
|
监控 负载均衡 应用服务中间件
探索微服务架构下的API网关设计与实践
在数字化浪潮中,微服务架构以其灵活性和可扩展性成为企业IT架构的宠儿。本文将深入浅出地介绍微服务架构下API网关的关键作用,探讨其设计原则与实践要点,旨在帮助读者更好地理解和应用API网关,优化微服务间的通信效率和安全性,实现服务的高可用性和伸缩性。
38 3
|
28天前
|
前端开发 API 数据安全/隐私保护
打造高效后端API:RESTful设计原则与实践
【9月更文挑战第4天】在数字化时代的浪潮中,后端API作为连接数据和前端的桥梁,其设计质量直接影响着应用的性能和扩展性。本文将深入探讨RESTful API的设计哲学、核心原则以及如何在实际开发中应用这些原则来构建一个高效、易于维护的后端系统。我们将通过代码示例,揭示如何将理论转化为实践,从而为开发者提供一条清晰的道路,去创造那些能够在不断变化的技术环境中茁壮成长的API。
|
10天前
|
前端开发 JavaScript UED
深入React Hooks与性能优化实践
深入React Hooks与性能优化实践
21 0
|
1月前
|
存储 JSON API
构建高效后端API:实践与思考
【8月更文挑战第31天】本文深入探讨如何打造一个高效、可靠的后端API。我们将通过实际案例,揭示设计原则、开发流程及性能优化的关键步骤。文章不仅提供理论指导,还附带代码示例,旨在帮助开发者构建更优的后端服务。
|
2月前
|
XML JSON API
构建高效后端API:RESTful设计原则与实践
【8月更文挑战第31天】在数字化浪潮中,后端API成为连接世界的桥梁。本文将引导你探索RESTful API的设计哲学,通过实例展示如何构建一个高效、易于维护且具有扩展性的后端服务。从资源定义到HTTP方法的应用,再到状态码的精准使用,我们将一步步揭开高效后端API的秘密。
|
2月前
|
缓存 前端开发 API
打造高效后端API:RESTful设计原则与实践
【8月更文挑战第31天】 在数字化时代,后端API的设计至关重要。本文将深入探讨如何根据RESTful设计原则构建高效、易于维护和扩展的后端API。我们将从基础概念出发,逐步引导至高级实践,确保读者能够理解并应用这些原则,以提升API性能和开发效率。
|
2月前
|
开发者 安全 UED
JSF事件监听器:解锁动态界面的秘密武器,你真的知道如何驾驭它吗?
【8月更文挑战第31天】在构建动态用户界面时,事件监听器是实现组件间通信和响应用户操作的关键机制。JavaServer Faces (JSF) 提供了完整的事件模型,通过自定义事件监听器扩展组件行为。本文详细介绍如何在 JSF 应用中创建和使用事件监听器,提升应用的交互性和响应能力。
22 0
下一篇
无影云桌面