1

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?

1 Answer 1

3

You can type your state, useState is a generic function

const [state, setState] = useState<IMyComponentState>(...);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.