React 顶层API汇总

287 阅读4分钟

React

React 是 React 库的入口。如果你用 <script> 标签来加载 React,这些顶级 API 都在 React 这个全局变量上。如果你在 npm 下使用 ES6 ,你可以这样写 import React from 'react' 。如果你在 npm 下使用 ES5,你可以这样写 var React = require('react') 。

React.Component

React.Component 是 React 组件使用 ES6 类(classes) 定义时的基类。

class Greeting extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

React.PureComponent

React.PureComponent 与 React.Component 完全相同,但是在shouldComponentUpdate()中实现时,使用了 props 和 state 的浅比较。

如果你的React组件的 render() 函数在给定相同 props 和 state 情况下渲染相同的结果,在某些情况下,你可以使用React.PureComponent来提高性能。

React.PureComponent 的 shouldComponentUpdate() 仅对对象进行浅比较。 如果这些包含复杂的数据结构,它可能会在更深层数据差异比较时发生判断偏差。 所以扩展 PureComponent 只能用于具有简单 props 和 state 的组件,或者在知道深层数据结构已更改时使用forceUpdate() 来强制更新的组件。 或者,考虑使用不可变对象来帮助嵌套数据的快速比较。 再者,React.PureComponent 的 shouldComponentUpdate() 方法跳过了整个组件子树的 props 更新。所以确保所有的子组件也是“pure”。

React.createElement()

React.createElement(
  type,
  [props],
  [...children]
)

创建并返回一个新的给定类型的 React 元素 。这个 type(类型) 参数可以是一个标签名字字符串(例如 'div' 或'span'),或者是一个 React 组件 类型(一个类或者是函数),或者一个 React fragment 类型。

使用 JSX 编写的代码将被转成使用 React.createElement() 。 如果您使用JSX,则你可以不必显示地直接调用 React.createElement()

React.cloneElement()

React.cloneElement(
  element,
  [props],
  [...children]
)

使用 element 作为起点,克隆并返回一个新的 React 元素。 所产生的元素将具有原始元素的 props ,新的 props 为浅层合并。 新的子元素将取代现有的子元素, key 和 ref 将被保留。 React.cloneElement() 几乎等效于:

<element.type {...element.props} {...props}>{children}</element.type>

然而,它也会保留 ref 。这意味着,如果你通过它上面的 ref 获取自己的子节点,你将不会有机会从你的祖先获取它。你只会获得绑定在你的新元素上的相同ref 。

这个 API 引入作为废弃的 React.addons.cloneWithProps() 替代品。

React.createFactory()

React.createFactory(type)

返回一个函数,该函数生成给定类型的 React 元素。 像React.createElement()一样,type(类型) 参数可以是一个标签名字字符串(例如 'div' 或'span'),或者是一个 React 组件 类型(类或者是函数)。 参数可以是标签名称字符串(例如’div’或’span’)或者React组件类型(类或函数)。

这个函数被认为是遗留的API,并且我们鼓励你使用 JSX ,或直接使用 React.createElement()

如果您使用JSX,则通常不需要直接调用 React.createFactory()。请参阅不使用 JSX 的 React 了解更多信息。

React.isValidElement()

React.isValidElement(object)

验证对象是否是一个 React 元素。 返回 true 或 false 。

React.Children

React.Children 提供了处理 this.props.children 这种不透明数据结构的实用程序。

React.Children.map

React.Children.map(children, function[(thisArg)])

对包含在 children 中的每个直接子元素调用一个函数,使用 this 设置 thisArg 。 如果 children 是一个键片段(keyed fragment)或数组,它将被遍历:该函数永远不会传递容器对象(container objects)。 如果 children 为 null 或 undefined ,返回 null 或 undefined,而不是一个数组。

React.Children.forEach

React.Children.forEach(children, function[(thisArg)])

类似于 React.Children.map() ,但是不返回一个新数组。

React.Children.count

React.Children.count(children)

返回 children 中的组件总数,等于传递给 map 或 forEach 的回调将被调用的次数。

React.Children.only

React.Children.only(children)

返回 children 中的唯一子集。否则抛出异常。

注意:

React.Children.only() 不接受 React.Children.map()的返回值,因为它是一个数组而不是一个 React 元素。

React.Fragment

React.Fragment 组件允许你在 render() 方法中返回多个元素,而无需创建额外的 DOM 元素:

render() {
  return (
    <React.Fragment>
      Some text.
      <h2>A heading</h2>
    </React.Fragment>
  );
}

你也可以用简写的 <></> 语法来使用它。

React.createRef

React.createRef 创建一个 ref ,它可以通过 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();
  }
}

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>;

在上面的例子中,React 传递一个 ref 给 <FancyButton ref={ref}> 元素作为第二个参数传递给React.forwardRef 调用中的渲染函数。 该渲染函数将 ref 传递给 <button ref = {ref}> 元素。

因此,在 React 附加 ref 后, ref.current 将直接指向 <button> DOM 元素实例。