⚛️ Here's a fun React tip: `useReducer` is a better `useState`, and it's easier to adopt than you may think.
Group related values together and spread them in the reducer. Then, updating is just:
updateThing({ prop: newValue })
And there's even more benefits to `useReducer`...
ALT Screenshot of React code showing two approaches to updating state:
Separate useState hooks:
const [message, setMessage] = useState('');
const [tags, setTags] = useState([]);
const [status, setStatus] = useState('active');
Single useReducer hook:
const [todo, updateTodo] = useReducer(
(data, partialData) => ({
...data,
...partialData,
}),
{ message: '', tags: [], status: 'active' }
);
ALT Screenshot of React code showing the difference between updating a value:
Updating with useState:
onChange={(e) => {
setMessage(e.target.value);
}}
Updating with useReducer:
onChange={(e) => {
updateTodo({
message: e.target.value,
});
}}