A very very basic logger for the useReducer function in the React Hooks API. Inspired by redux-logger.
- Install with
npm install use-reducer-logger --save-devoryarn add use-reducer-logger -D - Import logger with
import logger from 'use-reducer-logger';- Wrap your reducer with logger before passing it to
useReducer
const [state, dispatch] = useReducer(logger(reducer), initialState);See Example
You should only use this in a dev environment. So you could do something like this to apply the logger based on the env.
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();
}
}
const [state, dispatch] = useReducer(
process.env.NODE_ENV === 'development' ? logger(reducer) : reducer,
initialState
);MIT see LICENSE.
Contributions are welcome.
This repo is now sponsored/maintained by Zaelot Inc.
