Originally published by Rajdeep Chandra at thecoderswag.com

React 16.8.xx is shipped with React Hooks feature support in which developers can build functional components which can have own local state and props. (OH WOW)

What is a Hook?

Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don’t work inside classes — they let you use React without classes.

Let’s see what are the out of the box hooks they have for us:

State Hook:

Here, useState is a Hook (we’ll talk about what this means in a moment). We call it inside a function component to add some local state to it. React will preserve this state between re-renders. useState returns a pair: the current state value and a function that lets you update it. You can call this function from an event handler or somewhere else. It’s similar to this.setState in a class, except it doesn’t merge the old and new state together.

Effect Hook:

You’ve likely performed data fetching, subscriptions, or manually changing the DOM from React components before. We call these operations “side effects” (or “effects” for short) because they can affect other components and can’t be done during rendering.

The Effect Hook, useEffect, adds the ability to perform side effects from a function component. It serves the same purpose as componentDidMountcomponentDidUpdate, and componentWillUnmount in React classes, but unified into a single API.

Rules of Hooks

Hooks are JavaScript functions, but they impose two additional rules:

  • Only call Hooks at the top level. Don’t call Hooks inside loops, conditions, or nested functions.
  • Only call Hooks from React function components. Don’t call Hooks from regular JavaScript functions. (There is just one other valid place to call Hooks — your own custom Hooks. We’ll learn about them in a moment.)

useContext Hook:

It lets you subscribe to React context without introducing nesting. For e.g when you want to use your logged-in data deep down to some of the nested components: For e.g:

const locale = useContext(LocaleContext);
const theme = useContext(ThemeContext);

useReducer Hook:

useReducer hook lets you manage local state of complex components with a reducer:

Custom Hook:

Sometimes, we want to reuse some stateful logic between components. Traditionally, there were two popular solutions to this problem: higher-order components and render props. Custom Hooks let you do this, but without adding more components to your tree.

Below is a custom Hook:

The above custom hook is called useFetch which can be used as a custom hooks by calling the below method in your component

Other Useful Reads

Check out other cool React.js articles in the dedicated section at this link

Conclusion

If you found this article helpful, it would mean a lot if you commented it and shared to help others find it! And feel free to leave a comment below.

Originally published by Rajdeep Chandra at thecoderswag.com

=======================================================

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

☞ Understanding TypeScript

☞ Typescript Masterclass & FREE E-Book

☞ React - The Complete Guide (incl Hooks, React Router, Redux)

☞ Modern React with Redux [2019 Update]

☞ The Complete React Developer Course (w/ Hooks and Redux)

☞ React JS Web Development - The Essentials Bootcamp

☞ React JS, Angular & Vue JS - Quickstart & Comparison

☞ The Complete React Js & Redux Course - Build Modern Web Apps

☞ React JS and Redux Bootcamp - Master React Web Development

#reactjs #web-development

React Hooks: Understanding The Weird Parts
4 Likes33.90 GEEK