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. Laravel-admin图片本地上传配置问题

    先打开config/filesystems.php 修改添加其中的 'admin' => [ 'driver' => 'local', 'root' => public_path(' ...

  2. MySQL之创建用户和授权

    一 权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete.update.grant等操作.那么一般情况在公司之后DBA工程师会创建 ...

  3. selenium cookie 登录

    前言 爬虫方向的小伙伴们都知道网页爬虫经常遇到的问题就是登录账户,有些简单的网站我们可以简单的send key来输入账户密码就可以登录,但是有很多网站需要验证码之类的就不太好用了,这时候就体现到了co ...

  4. jacascript Date 学习

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! Date dateObject.getDate();  返回一个月中的某一天(1-31) dateObje ...

  5. IntelliJ IDEA 最新版 2019.2.4 激活 (持续更新)(含windows和Mac)

    IntelliJ IDEA 最新版 2019.2.4 激活 最新版激活包下载地址: 百度网盘 提取码: i28c 转载: Neo Peng Jetbrains系列产品 激活方式 同下述方式相同,只需要 ...

  6. 把zTree前的展开收起图标改为三角形,且只有在点击三角形图标时才展开子节点解决方案

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  7. JS 断点调试心得

    1.断点调试是啥?难不难? 断点调试其实并不是多么复杂的一件事,简单的理解无外呼就是打开浏览器,打开sources找到js文件,在行号上点一下罢了.操作起来似乎很简单,其实很多人纠结的是,是在哪里打断 ...

  8. Go map使用

    前言 map 是在 Go 中将值(value)与键(key)关联的内置类型.通过相应的键可以获取到值. 在一个map里所有的键都是唯一的,而且必须是支持==和!=操作符的类型,切片.函数以及包含切片的 ...

  9. dfs · leetcode-22.产生括号组?

    题面 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  10. 日志实时查看、轮询、统计、监控工具 Loggrove

    Loggrove 是对日志文件进行 阅读.轮询.关键词匹配.监控告警.图表展示 的 Web 服务. 下载地址 https://github.com/olajowon/loggrove 超轻组件 Pyt ...