npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-log-hook

v1.2.2

Published

React hook for logging per component lifecycle

Downloads

548

Readme

🪵 react-log-hook

React hook for logging per component lifecycle

Features

  • 🪶 Lightweight — under 1.5 kB gzipped & minified
  • 🗂️ Typed — made with TypeScript, shipped with types
  • 🥰 Simple — don't worry about any changes in your props & state
  • 🔧 Customizable — able to change everything you see in the logs
  • 🔬 Tested — up to 💯% unit test coverage
  • 🏎️ Fast — native react hooks & optimized
  • 📭 No dependecies

Install

With npm

npm install -D react-log-hook

With yarn

yarn add -D react-log-hook

Usage

Basic usage

import { useLog } from 'react-log-hook'

const App = () => {
  // Add a logger
  const { log } = useLog()

  const [state, setState] = useState(null)

  // Log the changes via console in real time!
  log(state)

  return null
}

Configuration options

import { useLog } from 'react-log-hook'

const App = () => {
  // Any configuration properties are optional
  const { log } = useLog({
    environments: [
      /** Contains array of environments of `process.env.NODE_ENV` in which logging will be allowed  */
      'dev',
      'development',
    ],

    // Print Options

    styles: {
      /** Contains styles object with different CSS inline styles used in logging */
      componentCSS:
        'color: DodgerBlue' /** Inline css for rendering component name in the logs */,
      changeCSS:
        'color: green; font-weight: bold;' /** Inline css for rendering current value in the logs */,
      subValueCSS:
        'color: SlateGray; font-weight: thin;' /** Inline css for rendering any additional data like time or previous value in the logs */,
    },
    printer: console /** Contains custom implementation of console */,
    logLevel: 'log' /** Level of logging defined by console method */,
    /** Render object or array inline or via interactive browser renderer */
    inline: true,
    isGroupingEnabled: true /** Enable grouping for logs  */,
    isGroupCollapsed: false /** Render groups collapsed  */,
    groupLabelRenderer: (
      /** A function which will be used to render labels for the group  */
      type /** Current stage of component lifecycle: 'Mount' | 'Change' | 'Unmount'  */,
      componentName,
    ) => `${type}${componentName}`,

    // Custom Render Function

    render: function ({
      /** Custom function which will be used for rendering the result, provided with useful data */
      value,
      prevValue,
      type /** Current stage of component lifecycle: 'Mount' | 'Change' | 'Unmount' */,
      componentName,
      inline /** Render object or array inline or via interactive browser renderer */,
      flags: {
        isGrouped /** Enable grouping for logs  */,
        isCollapsed /** Render groups collapsed  */,
      },
    }) {
      console.log(value)
    },
  })

  const [state, setState] = useState(null)

  // It's possible to redefine any configuration option per log call!
  log(state, {
    inline: false,
    logLevel: 'warn',
  })

  return null
}

FAQ

Will it deep copy the value to make sure it will persist in the logs?

  • 🎉 Yes, 🪵 react-log-hook deep copies the value to make sure it will not be changed in the logs later

Do i need to install @types/react-log-hook as well?

  • 💪 No, 🪵 react-log-hook comes with prebundled types

Will it run in production evironment?

  • ✅ By default 🪵 react-log-hook will run only in dev or development node evironments defined by NODE_ENV

Roadmap

  • [x] Add previous state checking
  • [x] Use object copy to persist in time
  • [x] Use console groups to handle all the logs
  • [x] Add dev environment support by default
  • [x] Polish the looks with component names, function calls, time etc
  • [x] Add more customization options
  • [ ] Test with SSR & Server components

Contributing

  • 🌟 Stars & 📥 Pull Requests are welcome for sure! ❤️

Development

🪵 react-log-hook uses npm & npm scripts in development, the following scipts can be handy:

npm run start:demo

Starts a demo app with enabled hook to check it in real environment

npm run storybook

Starts storybook with example components to test against

npm run release:check

Combination of linting, type-checking & tests; runs as precommit hook

License

MIT License