请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

如何将HOC转换成custom hook

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回答

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

0 回复 有任何疑惑可以回复我~
  • 提问者 ywang04 #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2021-07-10 14:50:45
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号