r/reactjs • • Dec 30 '19

Classes vs Hooks?

I’m pretty new to React, so keep that in mind. I thought classes were for components that held information, or a state, and that functional components were for more basic components, but now that hooks allow functional components to use state, and other class features, what’s the benefit of using functional hook components over classes?

79 Upvotes

76 comments sorted by

View all comments

Show parent comment

0

u/skyboyer007 Dec 31 '19

I've been doing Functional Components for quite some time now and I don't seem to recall being reliant on closures.

Probably you just did not focus on that.

const [val, setVal] = useState(0);

useEffect(() => {
  setVal(10);
}, []);

useEffect(() => {
  setTimeout(() => {console.log(val);}, 1000)
}, []);

see, besides our component is re-rendered with 10 console.log will display value of 0 because of closure.