Whether you're just starting or building advanced apps, these quick React tips will level up your code quality and developer experience. Let’s go 🚀
- Use useCallback to avoid unnecessary re-renders Avoid re-creating functions on every render.
const handleClick = useCallback(() => {
console.log('Clicked!');
}, []);
Especially useful when passing functions to child components.
- Group state with useReducer for complex logic
const reducer = (state, action) => {
switch (action.type) {
case 'increment': return { count: state.count + 1 };
default: return state;
}
};
const [state, dispatch] = useReducer(reducer, { count: 0 });
Cleaner than managing multiple useState hooks in complex components.
- Use React.memo to optimize functional components
const MyComponent = React.memo(({ value }) => {
return <div>{value}</div>;
});
It prevents re-renders if props haven’t changed.
- Debounce expensive operations like API calls
const debounce = (func, delay) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => func(...args), delay);
};
};
Combine with useCallback or useEffect for search or input-heavy UI.
- Extract reusable hooks If you reuse the same logic in multiple components — extract it!
// useLocalStorage.js
export const useLocalStorage = (key, initial) => {
const [value, setValue] = useState(() =>
JSON.parse(localStorage.getItem(key)) || initial
);
useEffect(() => {
localStorage.setItem(key, JSON.stringify(value));
}, [key, value]);
return [value, setValue];
};
- Lazy load components with React.lazy
const LazyComp = React.lazy(() => import('./LazyComp'));
<Suspense fallback={<div>Loading...</div>}>
<LazyComp />
</Suspense>
Improves performance by reducing initial bundle size.
- Use keyExtractor properly when rendering lists
{items.map(item => (
<ListItem key={item.id} item={item} />
))}
Avoid using index as key unless necessary to prevent bugs during reordering.
💡 Bonus: Use TypeScript with React for better DX and fewer bugs.
If you found this helpful, drop a ❤️ or comment your favorite tip. Let’s keep learning together!
Top comments (0)
Some comments may only be visible to logged-in visitors. Sign in to view all comments.