Hey everyone,
I'm trying to get a solid grasp on React Hooks, specifically `useState` and `useEffect`. I've been reading the documentation, but sometimes seeing practical examples really helps solidify the concepts.
For instance, when would you typically choose `useEffect` over setting state directly? What are some common pitfalls to avoid?
Here's a small example I've put together:
import React, { useState, useEffect } from 'react';
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
// This effect runs after every render
document.title = `You clicked ${count} times`;
console.log('Effect ran!');
// Cleanup function (runs before the next effect or unmount)
return () => {
console.log('Cleanup running...');
};
}); // No dependency array, runs on every render
return (
You clicked {count} times
);
}
export default Counter;
Any insights or other common use cases for hooks would be greatly appreciated!
Thanks!