如何将HOC转换成custom hook

来源:3-11 自定义 Hook 第二部分 - HOC的劣势

ywang04

2021-07-09

export const GroupedInputWithLabel = (props) => {
  const { required, children, fieldName } = props;
  const inputComponent = (
    <>
      <ControlLabel htmlFor={fieldName} required={required} />
      {children}
    </>
  );
  return <GroupedInput {...props}>{inputComponent}</GroupedInput>;
};

export const withGroupInput = (props, Component) => (
  <GroupedInputWithLabel {...props}>
    <Component {...props} />
  </GroupedInputWithLabel>
);

老师我有这么一段逻辑 withGroupInput 是用HOC形式完成的 想问下这种不涉及state的逻辑封装有必要用custom hook做吗?如果有 请问如何改成custom hook? 谢谢

写回答

1回答

张轩

2021-07-10

同学你好 你这个组件更多考虑的是界面的展示规则,所以用 HOC 非常适合,在这里建议就按你写的来办,custom hook 针对的是和界面无关的逻辑可以提取到自定义函数中。并不适合你这个场景。

0
1
ywang04
非常感谢!
2021-07-10
共1条回复

React18+TS高仿AntD从零到一打造组件库

设计,开发,测试,发布再到 CI/CD,从0到1造轮子

2124 学习 · 959 问题

查看课程