paint-brush
ReactJS: An Overview of Class Components and Functional Components with Hooksby@CoolMonkTechie
492 reads
492 reads

ReactJS: An Overview of Class Components and Functional Components with Hooks

by Sunil MishraMay 26th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

ReactJS: An Overview of Class Components and Functional Components with Hooks. We will learn about class components and functional components differences and which one is best use for Hooks in React. With Hooks, the composition of a component changes from being a combination of life-cycle Hooks — to functionalities with some render at the end. There are some benefits you get by using functional components in React: It will get easier to separate container and presentational components because you need to think more about your component’s state.
featured image - ReactJS: An Overview of Class Components and Functional Components with Hooks
Sunil Mishra HackerNoon profile picture

Hello Readers, CoolMonkTechie heartily welcomes you in this article.

In this article, we will learn about class components and functional components differences and which one is best use for Hooks. A famous quote about learning is :

"The beautiful thing about learning is that nobody can take it away from you."


So Let's begin.

The simplest way to define a component in React is to write a JavaScript function that returns a React element:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

The Welcome component accepts props which is an object that contains data and returns a React element. We can then import and render this component in another component.

The class component uses a programming methodology called Encapsulation which basically means that everything relevant to the class component will live within it. Life-cycle methods (constructors, componentDidMount(), render, and so on) give components a predictable structure.

Encapsulation is one of the fundamentals of OOP (Object-Oriented Programming). It refers to the bundling of data within the methods that operate on that data, and is used to hide the values or state of a structured data object inside a class — preventing unauthorized parties’ direct access to them.

With Hooks, the composition of a component changes from being a combination of life-cycle Hooks — to functionalities with some render at the end.

FUNCTIONAL COMPONENT

The example below shows how custom Hooks can be used in a functional component (without showcasing what the body is). However, what it does or can do is not limited. It could be instantiating state variables, consuming contexts, subscribing the component to various side effects — or all of the above if you’re using a custom hook!

function {
  useHook{...};
  useHook{...};
  useHook{...};
  return (
    ...
  );
}

CLASS COMPONENT

A class component requires you to extend from React.Component and create a render function which returns a React element. This requires more code but will also give you some benefits.

class {
  constructor(props) {...}
  componentDidMount() {...}
  componentWillUnmount() {...}
  render() {...}
}

So, Which one is best for Hooks ?

The Answer is :

There are some benefits you get by using functional components in React:

It will get easier to separate container and presentational components because you need to think more about your component’s state if you don’t have access to setState() in your component.Functional components are much easier to read and test because they are plain JavaScript functions without state or lifecycle-hooks.You end up with less code.The React team mentioned that there may be a performance boost for functional components in future React versions.

This leads to the first best practice when using React Hooks.

That's all about in this article.

Conclusion

In this article, We understood the Class component and functional component differences and benefits in Hooks with Which one is best for Hooks.

Thanks for reading ! I hope you enjoyed and learned about React class components and functional components differences and which one is best use for Hooks. Reading is one thing, but the only way to master it is to do it yourself.

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!😊😊😊