rigel_betaOri
rigel_betaOri

Reputation: 37

In React, Can a Class Component be Stateless and now with Hooks a Function Component be Stateful?

Before Hooks were introduced, for me there was a clear distinction between stateful and stateless components. However, with React Hooks, is it now safe to say that function components can be stateless or stateful depending on how it is being used (stateful if using useState for instance and/or using using are Hooks)?

Also, can a class component (extending to the React component base class) be considered stateless if state is not being used and/or using lifecycle methods?

I've been reading articles on stackoverflow regarding stateless vs. stateful (class vs. function) components, but a lot of the questions were answered before React v16.8.

Upvotes: 0

Views: 611

Answers (1)

Chris Sandvik
Chris Sandvik

Reputation: 1927

Class components could always be stateless, if state is never used. Functional components were originally only used as a simpler way of defining components which did not require state, but with the introduction of hooks they now have the ability of being stateful.

So to answer your question yes, both class components and functional components can be both stateful and stateless. However, with the introduction of hooks, the React devs have said that they believe all components will eventually move towards being functional using hooks so if I were you, I'd try and use functional components.

Upvotes: 1

Related Questions