Giter Club home page Giter Club logo

react-use-debug-hooks's Introduction

NPM JavaScript Style Guide Badges Badges Badges Badges


react-use-debug-hooks


Type-safe React hooks for debugging React component's changed props (and states), or a useEffect hook wrapper, which returns the dependencies that changed on each iteration of the effect within the console.


npm i -D react-use-debug-hooks

Important!

Do NOT use in production environment!

This package is intended to be a debugging tool only!
Therefore, it should be installed within the devDependancies and all usage removed from the codebase before pushing to a production environment.

useDebugInfo Reference

Parameter Explanation

  • componentName: This changes the debug label outputted with the changed deps in the console.
  • props: The props you want to observe for changes.
  • logOnlyWhenPropsChange: If you want to display in console only when any prop changes.

Parameter Types

  • effectConsoleName: string
  • props: object
  • logOnlyWhenPropsChange: boolean

useEffectDebugInfo Reference

Parameter Explanation

  • effectConsoleName: This changes the debug label outputted with the changed deps in the console.
  • effect: A callback function that contains imperative, possibly effectful code.
  • deps: The effect will only activate if the values in the list change.

Parameter Types

  • effectConsoleName: string
  • effect: React.EffectCallback
  • deps: React.DependencyList

Extra Details

  • useDebugInfo just requires a componentName to be displayed in console and an object with all the props needed to be observed for changes.
  • useDebugInfo has a final (optional) parameter logOnlyWhenPropsChange if you want displayed in the console only when anything changes and not every render.
  • For useDebugInfo, besides which props have changed, the renderCount and renderTime is also displayed.
  • useEffectDebugInfo also requires a effectConsoleName to be displayed in the console.
  • useEffectDebugInfo's effect callback an deps list is no different from useEffect ones.

Example Usage

// ExampleComponent.tsx
import { useDebugInfo, useEffectDebugInfo } from 'react-use-debug-hooks';

const ExampleComponent = ({ title: string, initialCount = 0 }: ExampleComponentProps) => {
  const [ string, setString ] = useState('0');
  const count = useCount(initialCount);

  const debugInfo = useDebugInfo('ExampleComponent', { // provide all props that we want to observe for changes
    title,
    initialCount,
    string,
    count,
  }, true); // optional param 'logOnlyWhenPropsChange' to not be spammed in the console
            // when component renders too much without any of the observed props changing

  useEffectDebugInfo('reset_string_effect', () => {
    setString('0');
  }, [ initialCount ]); // only fires when useEffect hook's dependencies changes

  return (
    <>
      <h5>Title: {title}</h5>
      <p>String: {string}</p>
      <p>Render count: {count}</p>
      <button onClick={() => setString((prevS) => String(Number(prevS) + 1))}>Increment String</button>
    </>
  );
}

react-use-debug-hooks's People

Contributors

darkang3l avatar fum4 avatar mihai1imob avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

fum4

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.