文章目录
概念
高阶组件是参数为组件,返回值为新组件的函数。它是一种基于 React 的组合特性而形成的设计模式。
使用原则
不要改变原始组件,而应该使用组合。
约定:将不相关的 props 传递给被包裹的组件
这种约定保证了 HOC 的灵活性以及可复用性。
约定:最大化可组合性
const ConnectedComment = connect(commentSelector, commentActions)(CommentList);
// connect 是一个函数,它的返回值为另外一个函数。
const enhance = connect(commentListSelector, commentListActions);
// 返回值为 HOC,它会返回已经连接 Redux store 的组件
const ConnectedComment = enhance(CommentList);
约定:包装显示名称以便轻松调试
function withSubscription(WrappedComponent) {
class WithSubscription extends React.Component {/* ... */}
WithSubscription.displayName = `WithSubscription(${getDisplayName(WrappedComponent)})`;
return WithSubscription;
}
function getDisplayName(WrappedComponent) {
return WrappedComponent.displayName || WrappedComponent.name || 'Component';
}
注意事项
不要在 render 方法中使用 HOC
render() {
// 每次调用 render 函数都会创建一个新的 EnhancedComponent
// EnhancedComponent1 !== EnhancedComponent2
const EnhancedComponent = enhance(MyComponent);
// 这将导致子树每次渲染都会进行卸载,和重新挂载的操作!
return <EnhancedComponent />;
}
务必复制静态方法
你可以使用 hoist-non-react-statics
自动拷贝所有非 React 静态方法:
import hoistNonReactStatic from 'hoist-non-react-statics';
function enhance(WrappedComponent) {
class Enhance extends React.Component {/*...*/}
hoistNonReactStatic(Enhance, WrappedComponent);
return Enhance;
}
除了导出组件,另一个可行的方案是再额外导出这个静态方法。
// 使用这种方式代替...
MyComponent.someFunction = someFunction;
export default MyComponent;
// ...单独导出该方法...
export { someFunction };
// ...并在要使用的组件中,import 它们
import MyComponent, { someFunction } from './MyComponent.js';
Refs 不会被传递
因为 ref 实际上并不是一个 prop - 就像 key 一样,它是由 React 专门处理的。