Today is Day 2 of my journey to becoming a full-stack developer, and I learned about three important React hooks: useState
, useEffect
, and useRef
. These hooks help make React apps interactive, efficient, and easy to manage.
What is useState
and Why Should You Use It?
useState
is a hook that allows you to manage the state of a component in React. State refers to data that can change over time, like user input or a counter. Whenever the state changes, React will automatically update the UI to reflect those changes.
For example, if you want to create a button that changes the text when clicked, you would use useState
to store and update the text. Without useState
, React wouldn’t know when to update the UI.
Why **useEffect**
is Useful ?
useEffect
is used to handle side effects in React. Side effects are things that happen outside of React’s normal flow, like fetching data from an API or setting up event listeners. You need useEffect
to manage these tasks without blocking the app’s performance.
For example, you can use useEffect
to fetch data when a component first loads. It allows you to run code after the component has rendered, so your app stays responsive.
How useRef
Helps with DOM References
useRef
is used to keep a reference to a DOM element, like an input field or a button, without causing re-renders. If you need to access a DOM element directly (for example, to focus on an input field), useRef
is the tool to use.
Unlike state, useRef
doesn’t cause the component to re-render, which makes it efficient for things like tracking the previous value of a prop or directly interacting with DOM elements.
Why These Hooks Matter
These three hooks are essential for building interactive and efficient React apps. useState
helps manage data, useEffect
handles side effects like fetching data, and useRef
allows direct manipulation of the DOM.
Thank You
Thank you for following along on Day 1 of my full-stack learning journey. I’m excited to continue sharing my progress and insights as I work through new concepts every day. If you’re learning too or have tips to share, feel free to drop a comment or connect with me! Let’s learn together.