Skip to content

Instantly share code, notes, and snippets.

View Liroo's full-sized avatar
📟
Make The Internet Great Again.

Liroo Pierre ᵈᵉᵛ Liroo

📟
Make The Internet Great Again.
View GitHub Profile
@Liroo
Liroo / usePersistReducer.js
Last active January 22, 2020 09:47
Really simple react hook for persistant reducer across session.
import { useReducer } from 'react';
export const usePersistReducer = (reducer, initialState, key) => {
let persistKey = `__PERSIST_REDUCER_${key}__`
const persistReducer = (state, action) => {
let newState = reducer(state, action);
localStorage.setItem(persistKey, JSON.stringify(newState));
return newState;