React Cheatsheet
React Cheatsheet
CheatSheet
In this Cheatsheet, we will cover the basics of React. We will
provide examples to help you understand how React works and
how to use them in your own web development projects. Whether
you are a beginner or an experienced developer, this PDF can
serve as a useful reference guide.
REACT
React is a JavaScript library for building user interfaces. It was
developed and is maintained by Facebook and a community of
individual developers and companies. React allows developers to
build reusable UI components, manage the state of their
applications, and render dynamic updates efficiently.
2|Page
MANAGE STATE
useState
• Class way
const initialCount = 0;
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count : initialCount };
}
render() {
return (
<div>
<p>You clicked {this.state.count} times</p>
<button
onClick = {() => this.setState(({count}) => ({ count: count + 1 }))}
>
Click me
</button>
</div>
);
}
}
3|Page
• Hook Way
useReducer
4|Page
const initialState = {count: 0};
function reducer(state, action) {
switch (action.type) {
case 'increment':
return {count: state.count + 1};
case 'decrement':
return {count: state.count - 1};
default:
throw new Error();
}
}
function Counter({initialState}) {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<>
Count: {state.count}
<button onClick = {() => dispatch({type: 'increment'})}>+</button>
<button onClick = {() => dispatch({type: 'decrement'})}>+</button>
</>
);
}
useEffect(() => {
applyEffect(dependencies);
return () => cleanupEffect();
}, [dependencies]);
5|Page
• Class way
6|Page
• Hook Way
useLayoutEffect
useLayoutEffect(() => {
applyBlockingEffect(dependencies);
return cleanupEffect();
}, [dependencies]);
7|Page
useLayoutEffect is almost same as useEffect, but fires
synchronously after the render phase. Use this to safely read from
or write to the DOM
8|Page
USE THE CONTEXT API
useContext
• Class way
9|Page
• Hook Way
return ...
}
MEMOIZE EVERYTHING
useMemo
10 | P a g e
function RandomColoredLetter(props) {
const [color, setColor] = useState('#fff')
const [letter, setLetter] = useState('a')
const handleColorChange = useMemo(() => () =>
setColor(randomColor()), []);
const handleLetterChange = useMemo(() => () =>
setLetter(randomColor()), []);
return (
<div>
<ColorPicker handleChange={handleColorChange} color={color} />
<LetterPicker handleChange={handleLetterChange} letter={letter} />
<hr/>
<h1 style={{color}}>{letter}</h1>
</div>
)
}
useCallback
function RandomColoredLetter(props) {
const [color, setColor] = useState('#fff')
const [letter, setLetter] = useState('a')
const handleColorChange = useCallback(() => setColor(randomColor()), []);
const handleLetterChange = useCallback(() => setLetter(randomColor()), []);
return (
<div>
<ColorPicker handleChange={handleColorChange} color={color} />
<LetterPicker handleChange={handleLetterChange} letter={letter} />
<hr/>
<h1 style={{color}}>{letter}</h1>
</div>
)
}
11 | P a g e
USE REFS
useRef
But it also allows you to just hold a mutable value through any
render. Also, mutating the value of ref.current will not cause any
render.
useImperativeHandle
useImperativeHandle (
ref,
createHandle,
[dependencies]
)
12 | P a g e
useImperativeHandle allows you to customize the exposed interface
of a component when using a ref. The following component will
automatically focus the child input when mounted :
13 | P a g e
REUSABILITY
Extract reusable behaviour into custom hooks.
14 | P a g e