react-native-context-logger
TypeScript icon, indicating that this package has built-in type declarations

1.1.2 • Public • Published

react-native-context-logger

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

Usage

  1. Install with npm install react-native-context-logger --save-dev or yarn add react-native-context-logger -D
  2. Import logger with
import {useLogger} from 'react-native-context-logger';
  1. Wrap your reducer with logger before passing it to useReducer
const [state, dispatch] = useReducer(useLogger(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' ? useLogger(reducer) : reducer,
  initialState
);

Contributing

Contributions are welcome.

Package Sidebar

Install

npm i react-native-context-logger

Weekly Downloads

6

Version

1.1.2

License

ISC

Unpacked Size

10.4 kB

Total Files

8

Last publish

Collaborators

  • komalrana