Konstantink1
Konstantink1

Reputation: 635

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

Upvotes: 1

Views: 1227

Answers (2)

Dennis Vash
Dennis Vash

Reputation: 53874

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}</>
}

Upvotes: 1

SomoKRoceS
SomoKRoceS

Reputation: 3043

Every React component that has a state influences its behavior (/render) or another component's behavior can be considered as a "stateful component". So for the function in the question - yes, App is stateful.

Upvotes: 1

Related Questions