Address
304 North Cardinal St.
Dorchester Center, MA 02124
Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM
React Hooks are functions, such as useState and useEffect that allow you to use state and lifecycle methods in a component without writing a class-based component. They were introduced in React 16.8 and have since become a popular way of creating React Components. Hooks enable you to reuse stateful logic across components and make it easier to write reusable and testable code.
React Hooks have revolutionized React development by simplifying state and side effect management in functional components. They promote code reusability, enhance component readability, and simplify the development process. With Hooks, developers can create powerful and efficient React applications, leveraging cleaner code and improved component reusability. They have significantly transformed how state and side effects are handled, making components more maintainable and separating concerns effectively. Hooks have brought about a paradigm shift, enabling developers to build robust React applications with ease and efficiency.
useState is a hook that allows you to add a state to a functional component. It takes an <strong>initial value</strong>
as an argument and returns an array containing the current state value and a function to update the state.
<strong>useState.js</strong>
or whatever you want to and add the following code within that fileuseState.js
import React, { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
function handleClick() {
setCount(count + 1);
}
return (
<div>
<p>You Clicked the button {count} times.</p>
<button onClick={handleClick}>Click</button>
</div>
);
}
export default Counter;
<strong>App.js</strong>
App.js
import React from "react";
import Counter from "./useState";
function App() {
return (
<div>
<Counter />
</div>
);
}
export default App;
npm start
useEffect is a hook that allows you to perform side effects in a functional component. It takes a function as an argument and runs it after every render. You can use it to fetch data, add event listeners or update the DOM
<strong>useEffect.js</strong>
or whatever you want to and add the following code within that fileuseEffect.js
import React, { useEffect, useState } from "react";
function Effect() {
const [data, setData] = useState([]);
useEffect(() => {
fetch("https://jsonplaceholder.typicode.com/posts")
.then((res) => res.json())
.then((data) => setData(data));
}, []);
return (
<div>
<ul>
{data.map((item) => (
<div key={item.id}>
<li>{item.title}</li>
</div>
))}
</ul>
</div>
);
}
export default Effect;
<strong>App.js</strong>
App.js
import React from "react";
import Effect from "./useEffect";
function App() {
return (
<div>
<Effect />
</div>
);
}
export default App;
npm start
Some other React Hooks which is used in our React Applications to create multiple features are:
That’s all to understand useState and useEffect as well as other React hooks. To learn how to create the routes in the ReactJS project, check out “Create routes in ReactJS project“.
If your want to learn more about ReactJS, you can follow all the ReactJS tutorials starting from “Introduction to React” on devcribe.com. Happy Coding!