Skip to content
A very basic logger for the useReducer function in the React Hooks API.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md
package-lock.json
package.json
screenshot.png
srhfl.js

README.md

use-reducer-logger

A very very basic logger for the useReducer function in the React Hooks API. Inspired by redux-logger.

screenshot of logger

Usage

  1. Install with npm install use-reducer-logger --save-dev or yarn add use-reducer-logger -D
  2. Import logger with
import logger from 'use-reducer-logger';
  1. Wrap your reducer with logger before passing it to useReducer
const [state, dispatch] = useReducer(logger(reducer), initialState);

See Example

In a Dev Environment

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] = (
  process.env.NODE_ENV === 'development' ? logger(reducer) : reducer,
  initialState
);

License

MIT see LICENSE.

Contributing

Contributions are welcome.

You can’t perform that action at this time.