React顶层API
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的更多相关文章
- React 顶层 API
概览 组件 使用 React 组件可以将 UI 拆分为独立且复用的代码片段,每部分都可独立维护.你可以通过子类 React.Component 或 React.PureComponent 来定义 Re ...
- ReactJS入门(三)—— 顶层API
本文基本跟着官方文档把API都走一遍,但会有实例来解释应该怎么用,木有比我更详细的API文档咯. React.createClass 参数:CONFIG(object) 创建一个ReactClass( ...
- React Router API文档
React Router API文档 一.<BrowserRouter> 使用HTML5历史记录API(pushState,replaceState和popstate事件)的<Rou ...
- React 组件 API
React 组件 API 在本章节中我们将讨论 React 组件 API.我们将讲解以下7个方法: 设置状态:setState 替换状态:replaceState 设置属性:setProps 替换属性 ...
- React LifeCycle API
React LifeCycle API old API & new API 不可以混用 demo https://codesandbox.io/s/react-parent-child-lif ...
- React入门--------顶层API
React.createClass 参数:config(object) 创建一个ReactClass(组件类),参数是一个对象且必须带有render属性方法,该方法必须返回一个封闭的容器(容器内可以由 ...
- [译]迁移到新的 React Context Api
随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...
- React Native API之 ActionSheetIOS
ok!先来演示是下效果: 官网教程:http://reactnative.cn/docs/0.31/actionsheetios.html#content 上代码:引入API组件: import Re ...
- [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 ...
随机推荐
- 2019CCPC网络赛——array(权值线段树)
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=6703 题目大意: 给出一个n(n<1e5)个元素的数组A,A中所有元素都是不重复的[1,n]. 有 ...
- ThinkPHP读取配置信息
use think\Config; dump(Config::get()); // 或者 dump(config());示例:dump(Config::get('database.database') ...
- 1234: 约瑟夫问题-输出最后的编号(Java)
WUSTOJ 1234: 约瑟夫问题-输出最后的编号 参考资料 约瑟夫问题--百度百科 Description n个人围成一圈,依次从1至n编号.从编号为1的人开始1至k报数,凡报数为k的人退出圈子, ...
- 1231: 删除字符串中指定的字符(Java)
WUSTOJ 1231: 删除字符串中指定的字符 题目 原题链接 Description 明天就要英语考试了,小明明正在挑灯夜战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词 ...
- JS 06 bom 框窗_页面_定时任务
BOM(Broswer Object Model) 凡是 window 的属性和方法,均可以省略“window.” 方法: 框窗 1.警告框 window.alert("msg") ...
- mysql 查询字段为空显示默认值
IFNULL() 函数用于判断第一个表达式是否为 NULL,如果为 NULL 则返回第二个参数的值,如果不为 NULL 则返回第一个参数的值. IFNULL() 函数语法格式为: IFNULL(exp ...
- 怎样解决多层this指向全局对象window的问题
如下所示, 得到的结果里面, 第二个this指向的是window这个全局对象而非f2, 原因就是多层this造成的指向不明引起的. var a = { f1: function(){ console. ...
- (一)mybatis介绍
一.mybatis简介 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- (五)Redis之List
一.List常用命令 两端添加 两端弹出 1.2. 两端添加和两端弹出 package myRedis01; import java.util.HashMap; import java.util.Li ...
- Python练习_集合和深浅拷贝_day7
1. 1.作业 1.把列表中所有姓周的人的信息删掉(升级题:此题有坑, 请慎重): lst = ['周老二', '周星星', '麻花藤', '周扒皮'] 结果: lst = ['麻花藤'] 2.车牌区 ...