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中浅拷贝和深拷贝分析

    首先,我们知道Python3中,有6个标准的数据类型,他们又分为可以变和不可变.不可变:Number(数字).String(字符串).Tuple(元组).可以变:List(列表).Dictionary ...

  2. 转录组测序(RNA-seq)技术

        转录组是某个物种或者特定细胞类型产生的所有转录本的集合.转录组研究能够从整体水 平研究基因功能以及基因结构,揭示特定生物学过程以及疾病发生过程中的分子机理,已广泛应 用于基础研究.临床诊断和药 ...

  3. Oracle复习

    这阵子忙着面试 ,之前搞项目一直用的 mysql ,然后面试的大公司!(呵呵)偏偏用的是 oracle 没办法恶补 前面学过的但是没怎么用的Oracle 基础知识,打算主要从下面几点入手. 环境搭建: ...

  4. centos源码安装nginx

    1.安装依赖 nginx对以下工具包有依赖,我们可以一键安装,命令: yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-dev ...

  5. PHP函数问题

    有时候,运行nginx和PHP CGI(PHP FPM)web服务的Linux服务器,突然系统负载上升,用top命令查看,很多phpcgi进程的CPU利用率接近100%后来通过跟踪发现,这种情况与PH ...

  6. docker-社区版(CE)安装

    目录 docker-社区版(CE)安装 安装步骤 docker-社区版(CE)安装 该安装方法是 基于centeros7 及其以上版本的安装方式,完全参考 docker官网提供的安装文档,官网安装文档 ...

  7. Array+DP leetcode-11.装更多的水

    11. Container With Most Water 题面 Given n non-negative integers a1, a2, ..., an , where each represen ...

  8. ECharts雷达图详细配置说明

    雷达图表配置说明: // 指定图表的配置项和数据 var option = { backgroundColor: 'rgba(204,204,204,0.7 )', // 背景色,默认无背景 rgba ...

  9. [LeetCode] 784. 字母大小写全排列 ☆☆☆(回溯、深度优先遍历)

    https://leetcode-cn.com/problems/letter-case-permutation/solution/shen-du-you-xian-bian-li-hui-su-su ...

  10. Orangepi 修改 Debian国内源

    1.导出sources.list 1   cat /etc/apt/sources.list >  sources.list  2.修改sources.list内容为如下: 1234   deb ...