201-React顶级API
一、概述
React是React库的入口点。如果您从<script>标记加载React,则这些顶级API可在React全局中使用。如果你使用npm的ES6,你可以写:import React from 'react',如果你使用npm的ES5,你可以写:var React = require('react').
二、简介
2.1、Components
React组件让你可以将UI分成独立的,可重用的部分,并且可以独立思考每个部分。 React组件可以通过继承React.Component或React.PureComponent来定义。
2.2、创建React元素
建议使用JSX来描述你的用户界面应该是什么样子。每个JSX元素只是用于调用React.createElement()的语法糖。如果您使用JSX,通常不会直接调用以下方法。
2.3、转换元素
2.4、片段
React还提供了一个组件,用于在没有包装的情况下呈现多个元素。
2.5、Refs
三、参考使用
3.1、React.Component
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}
3.2、React.PureComponent
几乎与React.Component一致,区别是React.Component没有实现shouldComponentUpdate(),React.PureComponent用浅道具和状态比较来实现它。
如果您的React组件的render()函数在给定相同的道具和状态的情况下呈现相同的结果,则可以使用React.PureComponent在某些情况下提高性能。
注意:React.PureComponent的shouldComponentUpdate()仅浅显地比较对象。如果这些包含复杂的数据结构,则可能会对更深的差异产生假阴性结果。只有在您期望拥有简单的道具和状态时才可以扩展PureComponent,或者在知道深层数据结构发生变化时使用forceUpdate()。或者,考虑使用不可变对象以便快速比较嵌套数据。 此外,React.PureComponent的shouldComponentUpdate()会跳过整个组件子树的prop更新。确保所有儿童组件都是“纯”的。
3.3、createElement()
React.createElement(
type,
[props],
[...children]
)
3.4、cloneElement()
React.cloneElement(
element,
[props],
[...children]
)
createFactory()
React.createFactory(type)返回一个生成给定类型的React元素的函数。与React.createElement()类似,type参数可以是标记名称字符串(例如'div'或'span'),React组件类型(类或函数)或React片段类型。这个帮助器被认为是遗留的,我们鼓励您直接使用JSX或直接使用React.createElement()。
3.5、isValidElement()
React.isValidElement(object)验证对象是一个React元素。返回true或false
3.6、React.Children
React.Children提供了用于处理this.props.children不透明数据结构的实用程序。
React.Children.map
React.Children.map(children, function[(thisArg)]):调用此设置为thisArg的子项中包含的每个直接子项的函数。如果children是keyed片段或数组,它将被遍历:该函数永远不会传递容器对象。如果children为null或未定义,则返回null或undefined,而不是数组。
React.Children.forEach
React.Children.forEach(children, function[(thisArg)]):像React.Children.map()一样,但不返回数组。
React.Children.count
React.Children.count(children):返回子组件的总数,等于调用传递给map或forEach的回调次数。
React.Children.only
React.Children.only(children):验证孩子只有一个孩子(一个React元素)并返回它。否则,此方法会引发错误。
React.Children.toArray
React.Children.toArray(children):将子不透明数据结构作为平面数组返回给每个孩子。如果你想在你的渲染方法中操作子集合,这很有用,特别是如果你想在传递它之前重新排序或者分割this.props.children。
3.7、React.Fragment
React.Fragment组件允许您在render()方法中返回多个元素而不创建额外的DOM元素:
render() {
return (
<React.Fragment>
Some text.
<h2>A heading</h2>
</React.Fragment>
);
}
3.8、React.createRef
React.createRef创建一个可以通过ref属性附加到React元素的引用。
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
}
render() {
return <input type="text" ref={this.inputRef} />;
}
componentDidMount() {
this.inputRef.current.focus();
}
}
3.9、React.forwardRef
React.forwardRef创建一个React组件,它将它接收的ref属性转发给树中下面的另一个组件。这种技术并不常见,但在两种情况下特别有用:
React.forwardRef接受渲染函数作为参数。 React将使用props和ref作为两个参数来调用这个函数。这个函数应该返回一个React节点。
const FancyButton = React.forwardRef((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
)); // You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
201-React顶级API的更多相关文章
- 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 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 ...
- React Context API
使用React 开发程序的时候,组件中的数据共享是通过数据提升,变成父组件中的属性,然后再把属性向下传递给子组件来实现的.但当程序越来越复杂,需要共享的数据也越来越多,最后可能就把共享数据直接提升到最 ...
- React 顶层 API
概览 组件 使用 React 组件可以将 UI 拆分为独立且复用的代码片段,每部分都可独立维护.你可以通过子类 React.Component 或 React.PureComponent 来定义 Re ...
- React顶层API
1.React.Children相关 1. React.Children.map(props.children, mapFunc) 1)该方法用于在props.children不透明的情况下,安全的遍 ...
随机推荐
- php常见的坑
10.filesize缓存的问题 PHP的filesize居然会缓存(当然还有不少,这里仅用filesize举例,其它会缓存的函数,以官方文档为准)线上代码经常随机出各种问题,排查了1个月,线上加各种 ...
- MVC+LINQToSQL的Repository模式之(二)数据基类
namespace Data.TEST{ /// <summary> /// 数据操作基类 /// </summary> public abstract ...
- Ubuntu 12.04.2 安装 Oracle11gR2
#step 1: groupadd -g 2000 dbauseradd -g 2000 -m -s /bin/bash -u 2000 griduseradd -g 2000 -m -s /bin/ ...
- 二:Java之异常处理
一.异常的概念 异常,也就是非正常情况. 其实.异常本质上是程序上的错误,包含程序逻辑错误和系统错误. 错误在我们编敲代码的过程中会常常发生,包含编译期间和执行期间的错误,在编译期间出现的错误有编译器 ...
- Vs2010创建WebService
在Visual Studio 2010中已经找不到直接创建WebService的模板方式了,但下面的方法可心实现: 在 Visual Studio 2010 的新建 Web 应用程序或者 Web 网站 ...
- python2.0_day22_web聊天室二
上节内容已经实现了客户端使用长轮询的方式获取消息的功能.但是还没有展现到前端.本节内容将实现1.展现消息到前端窗口.2.客户端之间发送图片和文件.3.文件上传时显示进度条 下面我们来实现上面3个功能. ...
- partition的分配策略简单代码实现
先说说partition的好处:Partition的好处是可以并发的获取同类数据,提高效率. 第一步需要实现Partitioner对象. public class ProducerPartitione ...
- 解决Bootstrap布局注册表单input标签前增加必填项*提示与input框不在同一行问题
注册表单部分代码如下: <form id="registForm" class="form-horizontal" action="${page ...
- mqtt 服务器与客户端通讯
mqtt 服务器与客户端通讯. 服务器端 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- 关于C#我今天的六个小时
嘿嘿,今天周六啦,我们是自由学习啦,于是还记得前几天纠结的事情,还有好多不太熟悉的题目那,于是今天就找出来练习下拉,而且这些题目在老师给我们复习时间也给我们讲解过啦,这样一来应该做起来还是容易点的啦, ...