Skip to content

Instantly share code, notes, and snippets.

@hansott
Last active April 12, 2019 14:20
Show Gist options
  • Save hansott/91421362756cbae4ccdf1c28a8fca846 to your computer and use it in GitHub Desktop.
Save hansott/91421362756cbae4ccdf1c28a8fca846 to your computer and use it in GitHub Desktop.
redux-logger for useReducer
import { logger } from "./logger";
import React, { useReducer } from "react";
const reducer = (state, action) => state;
const initialState = {};
export const Component = () => {
const [state, dispatch] = useReducer(logger(reducer), initialState);
return <div></div>;
}
export const logger = (reducer) => {
return (state, action) => {
console.log("%cPrevious State:", "color: #9E9E9E; font-weight: 700;", state);
console.log("%cAction:", "color: #00A7F7; font-weight: 700;", action);
console.log("%cNext State:", "color: #47B04B; font-weight: 700;", reducer(state, action));
return reducer(state, action);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment