内置的 React API
除了 Hooks 和 Components 之外,react
包还导出了一些其他的 API,这些 API 对于创建组件非常有用。本页面将介绍这些剩余的 React API。
createContext
API 可以创建一个 context,你可以将其提供给子组件,通常会与useContext
一起配合使用。forwardRef
允许组件将 DOM 节点作为 ref 暴露给父组件。lazy
允许你延迟加载组件,直到该组件需要第一次被渲染。memo
允许你在 props 没有变化的情况下跳过组件的重渲染。通常useMemo
与useCallback
会一起配合使用。startTransition
允许你可以标记一个状态更新是不紧急的。类似于useTransition
。
Resource APIs
Resources can be accessed by a component without having them as part of their state. For example, a component can read a message from a Promise or read styling information from a context.
To read a value from a resource, use this API:
function MessageComponent({ messagePromise }) {
const message = use(messagePromise);
const theme = use(ThemeContext);
// ...
}