1.React.Children相关

1. React.Children.map(props.children, mapFunc)

1)该方法用于在props.children不透明的情况下,安全的遍历组件children。

2)该方法可以平铺嵌套数组的返回值。

import React from 'react';
import ReactDOM from 'react-dom'; function User(props) {
// props.children取值有三种:
// 1.无子节点-undefined
// 2.一个文本/元素-字符串或者对象
// 3.多个节点-数组
// 所以使用map可能会有问题,但是React.Children.map解决了这个问题
return (
<>
{
React.Children.map(props.children, (item,index) => <div key={index}>{item}</div>)
}
</>
)
} ReactDOM.render(
<User>
1
</User>, window.root)

2. React.Children.forEach(props.children, forEachFn)

遍历,但是不会返回一个数组

3 .React.Children.count(props.children)

返回子组件的个数

4. React.Children.only(children)

判断是否只有一个元素;如果是返回该元素;否则抛出异常。

5. React.Children.toArray(children)

以数组形式扁平展开,并返回。

2.React.Fragment

用于class组件返回多个并列的元素。不想额外添加div等。

render() {
return (
<React.Fragment>
<div>1</div>
<div>2</div>
</React.Fragment>
)
}

还可以用简写形式<></>;或者[,,,,]

3. React.Profiler(V16.9.0)

用于测试React组件渲染的时间和“代价”。

有两个属性:

1. id 指定组件的名称

2. onRender()方法,挂载完的回调函数。

function onRenderCallback(
id, // 发生提交的 Profiler 树的 “id”
phase, // "mount" (如果组件树刚加载) 或者 "update" (如果它重渲染了)之一
actualDuration, // 本次更新 committed 花费的渲染时间
baseDuration, // 估计不使用 memoization 的情况下渲染整颗子树需要的时间
startTime, // 本次更新中 React 开始渲染的时间
commitTime, // 本次更新中 React committed 的时间
interactions // 属于本次更新的 interactions 的集合
) {
// 合计或记录渲染时间。。。
}

使用示例:

<Profiler id="Navigation" onRender={onRenderCallback}>
  <Navigation {...props} />
</Profiler>

4. React.StrictMode(V16.3.0)

用于在开发环境下,进行如下检查:

1. 识别过时的Ref 字符串API的使用

2.识别过时的ContextAPI 的使用

3.警告非安全生命周期的使用

4.警告废弃的findReactDOMNode()的使用

5.检查一些副作用

代码示例:

      <React.StrictMode>
<ComponentOne />
<ComponentTwo />
</React.StrictMode>

5. React.memo(V16.6)

高阶组件,将函数组件作为参数传入,返回一个集成PureComponent的类组件。

function memo(WrappedComponent) {
return class extends React.PureComponent {
render() {
return (
<WrappedComponent {...this.props} />
)
}
}
}

6. React.cloneElement

语法:

React.cloneElement(
element,
props,
[...children]
)

相当于复制element元素后,再给其添加props和children。

<element.type {...element.props} {...props}>
{children}
</element.type>

7. React.isValidElement(obj)

判断一个对象是否是React元素。

React.isValidElement(<App/>) //true

8. React.createRef和React.forwardRef(V16.3.0)

React.createRef()用于生成ref对象,作为ref属性的值传递。

React.forwardRef((props,ref) => ())用于ref转发。

9. React.Suspense和React.lazy()(V16.6.0)

注意:React.lazy()需要支持Promise。

React.Suspense用于React.lazy()加载的组件外围,用于指定加载指示器。

防止某些组件尚未具备渲染条件。

React顶层API的更多相关文章

  1. React 顶层 API

    概览 组件 使用 React 组件可以将 UI 拆分为独立且复用的代码片段,每部分都可独立维护.你可以通过子类 React.Component 或 React.PureComponent 来定义 Re ...

  2. ReactJS入门(三)—— 顶层API

    本文基本跟着官方文档把API都走一遍,但会有实例来解释应该怎么用,木有比我更详细的API文档咯. React.createClass 参数:CONFIG(object) 创建一个ReactClass( ...

  3. React Router API文档

    React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...

  4. React 组件 API

    React 组件 API 在本章节中我们将讨论 React 组件 API.我们将讲解以下7个方法: 设置状态:setState 替换状态:replaceState 设置属性:setProps 替换属性 ...

  5. React LifeCycle API

    React LifeCycle API old API & new API 不可以混用 demo https://codesandbox.io/s/react-parent-child-lif ...

  6. React入门--------顶层API

    React.createClass 参数:config(object) 创建一个ReactClass(组件类),参数是一个对象且必须带有render属性方法,该方法必须返回一个封闭的容器(容器内可以由 ...

  7. [译]迁移到新的 React Context Api

    随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...

  8. React Native API之 ActionSheetIOS

    ok!先来演示是下效果: 官网教程:http://reactnative.cn/docs/0.31/actionsheetios.html#content 上代码:引入API组件: import Re ...

  9. [React] Use the new React Context API

    The React documentation has been warning us for a long time now that context shouldn't be used and t ...

随机推荐

  1. python学习-47 random模块

    random模块 随机模块 random 的方法: print(random.random()) # [0,1] 的浮点数 print(random.randint(1,3)) print(rando ...

  2. 模块 json 和 pickle

    目录 序列化 json 和 pickle 模块 序列化 序列:字符串 序列化:将其它数据类型转换成字符串的过程. 反序列化:字符串转成其它数据类型. 序列化的目的 1:以某种存储形式使用自定义对象持久 ...

  3. 20191011-构建我们公司自己的自动化接口测试框架-Util的读取excel常用方法模块

    包括获取excel的sheet名字,设定excel的sheet,读excel,写excel等常规操作. from openpyxl import Workbook from openpyxl impo ...

  4. Flash播放控件属性详解

    Flash 播放控件属性详解 一.属性篇 1.AlignMode(读写)  语法:AlignMode As Long  说明:对齐方式(与SAlign 属性联动).当控件的长宽比例与影片不一致且WMo ...

  5. linux 串口接收

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types. ...

  6. Django rest-framework框架-组件之渲染器

    渲染器: from rest_framework.renderers import BrowsableAPIRenderer,AdminRenderer,HTMLFormRenderer,JSONRe ...

  7. Java内存模型(JMM)

    JVM与线程(线程在JVM中) 1.JVM什么时候启动?         类被调用时启动,此时会启动JVM线程然后再是其他的线程(main) 2.JVM内存区域 除了程序计数器(PC)之外都有可能发生 ...

  8. 2、Java基础:概念

    1.面向对象和面向过程的区别 面向过程 优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开发.Linux/Unix等一般采用面向过程开发,性能是最重要的因素 ...

  9. spingboot启动报驱动Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of th

    原因: springboot应用了最新的驱动com.mysql.cj.jdbc.Driver,这个驱动需要用mysql-connector-java包的6.x版本才可以, 而mysql-connect ...

  10. express 和 vue-cli 的博客网站

    已经上传到github地址:https://github.com/13476075014/node-vue/tree/master/mynodeproject/15.TimeBlog # 个人博客系统 ...