
ReactJS
React provides many built-in Hooks beyond useState and useEffect. Each solves specific problems.
useContext lets you access context values without prop drilling. Consume context from providers up the tree.
import { createContext, useContext } from 'react';
const ThemeContext = createContext();
function ThemeProvider({ children }) {
const [theme, setTheme] = useContext('light');
return (
<ThemeContext.Provider value={{ theme, setTheme }}>
{children}
</ThemeContext.Provider>
);
}
function Button() {
const { theme } = useContext(ThemeContext);
return (
<button style={{ background: theme === 'dark' ? '#000' : '#fff' }}>
Click
</button>
);
}useReducer manages complex state with multiple actions. Similar to Redux but simpler.
import { useReducer } from 'react';
function reducer(state, action) {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
default:
return state;
}
}
function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div>
<p>Count: {state.count}</p>
<button onClick={() => dispatch({ type: 'INCREMENT' })}>+</button>
</div>
);
}| Hook | Purpose |
|---|---|
| useState | Component state |
| useEffect | Side effects |
| useContext | Access context |
| useReducer | Complex state |
Ready to practice? Challenges
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward