Using React Hooks to Manage State
Learn how to use React Hooks to manage state in your React applications.
Introduction
To get started with this tutorial, you'll need to have Node.js and npm installed on your computer.
Step 1: Create a new React app
Create a new React app using create-react-app by running the following command in your terminal:
npx create-react-app react-hooks-app
Step 2: Create a Counter Component
Create a new file called Counter.js and add the following code:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<h1>Counter: {count}</h1>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
export default Counter;
Step 3: Render the Counter Component
Open the App.js file and replace the existing code with the following:
import React from 'react';
import Counter from './Counter';
function App() {
return (
<div>
<Counter />
</div>
);
}
export default App;
Conclusion
You now have a basic understanding of how to use React Hooks to manage state in your React applications.
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based react video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked react books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.