As we all know that React offers us functional components, which can now manage internal state and side effects using Hooks, and class components, which traditionally manage stateful logic.
Hooks are functions that are actually allow us to hook into react state and it’s lifecycle features from function components.
And the most interesting part of React is😀 that we don’t have to use classes anymore, thanks to the introduction of Hooks.
And the best part of React hooks is that we can easily integrate it within our existing code.
The important think is React has a very clear notion (Conception) of the state of the Application which is very different from when we use JavaScript.
In React we don’t have to manually garb some html tags to change the value or update it in the web page. Instead, we update the component’s state, and React takes care of updating the DOM automatically.
Example:- Suppose we are using JavaScript and want to change the content or value of an element on our web page. First, we need to grab that specific element using a method like getElementById
, querySelector
, etc. Then, using DOM manipulation, we can update its value or content. While this process is straightforward, it can become repetitive and cumbersome, especially when dealing with many elements or complex updates .
And this is where React gives us the benefit of react hooks.
If we determine some sort of system to manage the state it can handle the entire web page, the UI, or just its variables.
React introduced the concept of efficiently updating and rendering the user interface by managing the state of an application.
As we change the state of a React application, the component re-renders automatically to reflect the new state. The useState()
hook is a clear example of this mechanism, as it allows us to manage state and automatically triggers a re-render when the state changes
import { useState } from "react";
export default function FavoriteColor() {
const [color, setColor] = useState("red");
return <h1>My favorite color is {color}!</h1>
}
In the example above, we can see that it is easy to change the value of the variable color.