19
loading...
This website collects cookies to deliver better user experience
useReducer
hook allows you to manage complex state logic in React Applications. The useReducer
hook is an alternative for the useState
hook and, combined with other interesting React’s feature called Context
, helps manage state efficiently.const [ state, dispatch ] = useReducer(reducerFucntion, initialState);
useReducer hook
receives two params:useReducer
returns array that contains:Current state - initially, this will be the initial state that you passed to a reducer, and after each action dispatch, the state is changed and returned here.
Dispatch function - a function that receives action as an argument and dispatches this action in a reducer.
const ADD_MESSAGE = ‘ADD_MESSAGE’;
const REMOVE_MESSAGE = ‘REMOVE MESSAGE’;
export function MessagesReducer(state, action) {
switch(action.type) {
case ADD_MESSAGE: {
return {
messages: [
...state.messages,
action.message
]
};
}
case REMOVE_MESSAGE: {
const indexToToRemove = state.messages.indexOf(action.message);
if (indexToToRemove >= 0) {
return {
messages: [
...state.messages.splice(indexToToRemove, indexToToRemove)
]
}
} else {
return state;
}
}
default: {
return state;
}
}
}
state
and action
. The state is data that we are manipulating, and action determines what we want to do with that data.ADD_MESSAGE
REMOVE_MESSAGE
{type: ADD_MESSAGE, message}; // adding message
{type: REMOVE_MESSAGE, message}; // removing message
action.message
.default: {
return state;
}
ADD_MESSAGE
:case ADD_MESSAGE: {
return {
messages: [
...state.messages,
action.message
]
};
}
state
messages array (state.messages
) and a new message received in action (action.message
).REMOVE_MESSAGE
:case REMOVE_MESSAGE: {
const indexToToRemove = state.messages.indexOf(action.message);
if (indexToToRemove >= 0) {
return {
messages: [
...state.messages.splice(indexToToRemove, indexToToRemove)
]
}
} else {
return state;
}
}
indexToRemove
const is equal or greater than zero, then the reducer function returns a new state containing messages without a message that should be removed. useReducer
hook returns the dispatch function, and then you can use it in a component to mutate a state. Take a look at the example below:<button onClick={() => dispatch({type: ADD_MESSAGE, message: ‘React is cool!’’})}> Add message </button>
export const addMessage = message => {
return {type: ADD_MESSAGE, message};
}
export const removeMessage = message => {
return {type: REMOVE_MESSAGE, message};
}
import {removeMessage as removeMessageAction from ‘./messagesReducer’;
dispatch(removeMessageAction(message))
<ChildComponent addMessage={message => dispatch(removeMessageAction(message))}/>
const ADD_MESSAGE = 'ADD_MESSAGE';
const REMOVE_MESSAGE = 'REMOVE_MESSAGE';
export function MessagesReducer(state, action) {
switch(action.type) {
case ADD_MESSAGE: {
return {
messages: [
...state.messages,
action.message
]
};
}
case REMOVE_MESSAGE: {
const indexToToRemove = state.messages.indexOf(action.message);
if (indexToToRemove >= 0) {
return {
messages: [
...state.messages.splice(indexToToRemove, indexToToRemove)
]
}
} else {
return state;
}
}
default: {
return state;
}
}
}
export const messagesInitialState = { messages: [] }
export const addMessage = message => {
return {type: ADD_MESSAGE, message};
}
export const removeMessage = message => {
return {type: REMOVE_MESSAGE, message};
}
import React, { createContext, useReducer } from 'react';
import {
MessagesReducer,
messagesInitialState,
addMessage as addMessageAction,
removeMessage as removeMessageAction
} from '../../reducers/Messages';
export const MessagesContext = createContext();
export const MessagesProvider = ({ children }) => {
const [{ messages }, dispatch ] = useReducer(MessagesReducer, messagesInitialState);
const removeMessage = message => dispatch(removeMessageAction(message));
const addMessage = message => dispatch(addMessageAction(message));
return <MessagesContext.Provider value={{
messages,
addMessage,
removeMessage
}}>
{children}
</MessagesContext.Provider>
};
useState
hook for managing primitive states like strings, numbers, and booleans. useReducer
hook.useReducer
hook with other React’s mechanism called Context
allows you to manage state in your app efficiently.