【问题标题】:Typing component state with typescript when using react hooks使用反应钩子时使用打字稿键入组件状态
【发布时间】:2019-08-09 09:35:14
【问题描述】:

react 中的类组件有两个接口,分别是propsstate

Class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {}

然而,当使用钩子时,组件声明看起来像这样

interface IMyComponentProps {
  ...
}

const MyComponent: React.FC<IMyComponentProps> = ({...}) => {
  const [state, setState] = useState(...);

  return (
    <>...</>
  );
};

export default FormMessage;

在使用钩子时是否需要为state指定接口以及如何?

【问题讨论】:

    标签: reactjs typescript react-hooks


    【解决方案1】:

    你可以输入你的状态,useState 是一个通用函数

    const [state, setState] = useState<IMyComponentState>(...);
    

    【讨论】:

      猜你喜欢
      • 2018-01-08
      • 1970-01-01
      • 2021-07-09
      • 2021-06-30
      • 1970-01-01
      • 2021-08-30
      • 2019-04-19
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多