Home:ALL Converter>Typing component state with typescript when using react hooks

Typing component state with typescript when using react hooks

Ask Time:2019-08-09T17:35:14         Author:Rahul Yadav

Json Formatter

Class components in react take two interfaces namely for props and state

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

However while using hooks the component declaration looks like this

interface IMyComponentProps {
  ...
}

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

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

export default FormMessage;

Is it necessary to specify interface for state while using hooks and how?

Author:Rahul Yadav,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/57427275/typing-component-state-with-typescript-when-using-react-hooks
yy