Skip to content
Advertisement

Would a function component that uses useState hooks be considered as stateful component?

Would a component of type

function App() {
  const [state,  setState] = React.useState()

  return (
     [...]
  )
}

be considered as a stateful component by the definition? Or would be still a stateless functional component since it does not extend React.Component explicitly and does not declare a state with passing super(props)?

Best regards, Konstantin

Advertisement

Answer

Stateless Component is when a component is purely a result of props alone, no state, the component can be written as a pure function avoiding the need to create a React component instance.

const Component = ({ name }) => {
  return <>{name}</>;
};

So, if it is not stateless, it is a stateful component.

function App() {
  const [state,setState] = React.useState()
  return <>{state}</>
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement