GithubHelp home page GithubHelp logo

rainstormza / react-lifecycle-visualizer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from oblosys/react-lifecycle-visualizer

0.0 2.0 0.0 4.51 MB

Real-time visualizer for React lifecycle methods

Home Page: https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo?file=src/samples/New.js

License: MIT License

JavaScript 92.78% HTML 1.13% CSS 6.09%

react-lifecycle-visualizer's Introduction

React Lifecycle Visualizer npm version Build Status

An npm package (react-lifecycle-visualizer) for tracing & visualizing lifecycle methods of arbitrary React components.

To trace a component, apply the higher-order component traceLifecycle to it, and all its lifecycle-method calls will show up in a replayable log component. Additionally, traced components may include a <this.LifecyclePanel/> element in their rendering to show a panel with lifecycle methods that are highlighted when the corresponding log entry is selected.

Parent-child demo

Usage

The easiest way to get started is to open the StackBlitz project and edit the sample components in src/samples.

The panel shows the new v16.3 lifecycle methods, unless the component defines at least one legacy method and no new methods. On a component that has both legacy and new methods, React ignores the legacy methods, so the panel shows the new methods.

Though technically not lifecycle methods, setState & render are also traced. A single setState(update, [callback]) call may generate up to three log entries:

  1. 'setState' for the call itself.
  2. If update is a function instead of an object, 'setState:update fn' is logged when that function is evaluated.
  3. If a callback function is provided, 'setState:callback' is logged when it's called.

To save space, the lifecycle panel only contains setState, which gets highlighted for any of the three events above.

Run the demo locally

To run a local copy of the StackBlitz demo, simply clone the repo, and run npm install & npm start:

git clone [email protected]:Oblosys/react-lifecycle-visualizer.git
cd react-lifecycle-visualizer
npm install
npm start

The demo runs on http://localhost:8001/.

Using the npm package

$ npm i react-lifecycle-visualizer

Setup

The set up tracing, wrap the root or some other ancestor component in a <VisualizerProvider> and include the <Log/> component somewhere. For example:

import { Log, VisualizerProvider } from 'react-lifecycle-visualizer';

ReactDom.render(
  <VisualizerProvider>
    <div style={{display: 'flex'}}>
      <App/>
      <Log/>
    </div>
  </VisualizerProvider>,
  document.getElementById('root')
);

If you're using a WebPack dev-server with hot reloading, you can include a call to clearInstanceIdCounters in the module where you set up hot reloading:

import { clearInstanceIdCounters } from 'react-lifecycle-visualizer';
..
clearInstanceIdCounters(); // clear instance counters on hot reload
..

This isn't strictly necessary, but without it, instance counters will keep increasing on each hot reload, making the log less readable.

Trace components

To trace a component (e.g. ComponentToTrace,) apply the traceLifecycle HOC to it. This is most easily done with a decorator.

import { traceLifecycle } from 'react-lifecycle-visualizer';
..
@traceLifecycle
class ComponentToTrace extends React.Component {
  ..
  render() {
    return (
      ..
      <this.LifecyclePanel/>
      ..
    );
  }
}

Alternatively, apply traceLifeCycle directly to the class, like this:

const ComponentToTrace = traceLifeCycle(class ComponentToTrace extends React.Component {...});

or

class ComponentToTraceOrg extends React.Component {...}
const ComponentToTrace = traceLifeCycle(ComponentToTraceOrg);

A this.trace method gets added to component and can be used to log specific information:

componentDidUpdate(prevProps, prevState) {
  this.trace('prevProps: ' + JSON.stringify(prevProps));
}

Because we cannot use this to refer to the component instance in the static getDerivedStateFromProps, trace is passed as a third parameter to this method:

static getDerivedStateFromProps(nextProps, prevState, trace) {
    trace('nextProps: ' + JSON.stringify(nextProps));
    ..
}

react-lifecycle-visualizer's People

Contributors

oblosys avatar danielruf avatar

Watchers

James Cloos avatar Supakorn Thongtra avatar

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.