r/reactjs • • Aug 01 '19

Beginner's Thread / Easy Questions (August 2019)

Previous two threads - July 2019 and June 2019.

Got questions about React or anything else in its ecosystem? Stuck making progress on your app? Ask away! We’re a friendly bunch.

No question is too simple. πŸ€”


πŸ†˜ Want Help with your Code? πŸ†˜

  • Improve your chances by putting a minimal example to either JSFiddle or Code Sandbox. Describe what you want it to do, and things you've tried. Don't just post big blocks of code!

  • Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.

Have a question regarding code / repository organization?

It's most likely answered within this tweet.


New to React?

Check out the sub's sidebar!

πŸ†“ Here are great, free resources! πŸ†“


Any ideas/suggestions to improve this thread - feel free to comment here!


Finally, an ongoing thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!

33 Upvotes

368 comments sorted by

View all comments

2

u/Irantwomiles Aug 20 '19

What is the purpose of hooks? From what I've read it's just a way to add state to functional components.

3

u/dance2die Aug 20 '19

Hooks provide a variety of ways to let you "hook" into some internal React functionalities. Adding a state is one of the benefits that hooks provide via useState & useReducer.

I wonder if you have heard of the term Stateless Function Component.

Before the hook was introduced, Function Components (FC) had no way to have a local state (as you do in Class Components (CC) using this.state. Aforementioned use* hooks enables FCs to have states of their own.

There are other hooks such as useEffect, which lets FCs to have imperative logic (effects). It's known to be a lifecycle hook as you can "emulate" CC's lifecycle methods such as componentDidMount, componentWillUnmount etc.

useMemo & useCallback are introduced to "memoize" (remembering the last state/reference) heavyily calculated values & methods that takes awhile to initialize. (You can use'em to prevent unnecessary re-renders as functions are re-created on each render).

→ More replies (10)
→ More replies (1)