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不透明的情况下,安全的遍 ...
随机推荐
- pushlet 之 Pushlet使用手把手实例
Pushlet(一种comet 架构的实现)是基于Servlet 机制,数据从server端的Java 对象直接推送(push) 到客户端浏览器的(动态)HTML 页面,而无需任何Java app ...
- 分享jQuery封装好的一些常用操作
1. 禁止右键点击 $(document).ready(function(){ $(document).bind("contextmenu",function(e){ ...
- 插入数据insert语句中出错:没有与这些操作数匹配的“+”运算符,操作数类型为:const char[ ]+CString
UpdateData(); CString n; n.Format(L"%d", m_n); _bstr_t strCmd_n = "insert into n(材料,折 ...
- [转] 在图标库中,找到合适的图标 ico
作者:xlrocket链接:https://www.zhihu.com/question/19857245/answer/241696797 在图标库中,找到合适的图标 或许,一些小伙伴会有收集图标的 ...
- python--数据类型--1
原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 视频教程::http://www.imooc.com/learn/177 http://www. ...
- SQLServer------如何让标识列重新开始计算
方法: DBCC CHECKIDENT (表名, RESEED, )
- Javascript中的感叹号和函数function
js函数前加分号和感叹号是什么意思?有什么用?:http://www.cnblogs.com/mq0036/p/4605255.html function与感叹号:https://swordair.c ...
- Python pymysql 模块
pymysql 是 Python3 连接 MySQL 的一个模块,常见用法如下: [root@localhost ~]$ pip3 install pymysql # 安装 pymysql 模块 In ...
- 当新增页面和编辑页面使用同一jsp时
<c:if test="${type eq '1'}"><title>新增页面</title></c:if> <c:if te ...
- django restframework 教程之Serialization(序列化)
介绍 本教程将会通过创建一个简单的粘贴代码项目,突出显示WebAPI,过程中,会介绍组成REST框架的各种组件,让你全面了解如何配合使用. 环境设置 建议使用virtualenv建立新的虚拟环境,确保 ...