Reusing Logic with Hooks in React
📖 Scenario: You are building a simple React app that shows a counter. You want to reuse the counting logic in multiple places without repeating code.
🎯 Goal: Create a custom hook called useCounter that manages a counter value and a function to increase it. Then use this hook inside a functional component to display the count and a button to increase it.
📋 What You'll Learn
Create a custom hook named
useCounter that returns a count state and an increment function.Initialize the count state to 0 inside the hook.
Create a functional component named
CounterDisplay that uses the useCounter hook.Render the current count and a button labeled
Increase inside CounterDisplay.Clicking the button should increase the count by 1.
💡 Why This Matters
🌍 Real World
Custom hooks help you reuse logic like counters, form handling, or data fetching across many components without repeating code.
💼 Career
Understanding custom hooks is essential for React developers to write clean, maintainable, and reusable code in real projects.
Progress0 / 4 steps