All Projects → Oblosys → React Lifecycle Visualizer

Oblosys / React Lifecycle Visualizer

Licence: mit
Real-time visualizer for React lifecycle methods

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Lifecycle Visualizer

Traceshark
This is a tool for Linux kernel ftrace and perf events visualization
Stars: ✭ 63 (-94.89%)
Mutual labels:  trace, visualizer
Logbert
Logbert is an advanced log message viewer for log4net, log4j and others.
Stars: ✭ 70 (-94.32%)
Mutual labels:  trace
Androidequalizer
Stars: ✭ 18 (-98.54%)
Mutual labels:  visualizer
Glmviz
A high framerate, fully configurable OpenGL music visualizer
Stars: ✭ 56 (-95.45%)
Mutual labels:  visualizer
Sst Elements
SST Architectural Simulation Components and Libraries
Stars: ✭ 36 (-97.08%)
Mutual labels:  trace
Sudoku Gui
Simple Sudoku Game built on Pygame as a backtracking algorithm visualizer. Credits to TechWithTim for the idea.
Stars: ✭ 29 (-97.65%)
Mutual labels:  visualizer
Molten
php probe for zipkin and opentracing
Stars: ✭ 740 (-39.94%)
Mutual labels:  trace
Corewar
School 42 project // Implementation of programming game “Core War” [Champions + Assembler + Disassembler + Virtual Machine + Visualizer]
Stars: ✭ 76 (-93.83%)
Mutual labels:  visualizer
Visualixir
A process/message visualizer for BEAM nodes.💪👁
Stars: ✭ 1,169 (-5.11%)
Mutual labels:  visualizer
Tracer
A powerful and customizable logging library for node.js
Stars: ✭ 1,081 (-12.26%)
Mutual labels:  trace
Cloud Trace Java
Stars: ✭ 51 (-95.86%)
Mutual labels:  trace
Fliplog
fluent logging with verbose insight, colors, tables, emoji, filtering, spinners, progress bars, timestamps, capturing, stack traces, tracking, presets, & more...
Stars: ✭ 41 (-96.67%)
Mutual labels:  trace
Fountain Of Colors
Music visualizer for Rainmeter
Stars: ✭ 65 (-94.72%)
Mutual labels:  visualizer
Dubbo Trace
基于Dubbo的分布式系统调用跟踪Demo
Stars: ✭ 72 (-94.16%)
Mutual labels:  trace
Html5 audio visualizer
An audio spectrum visualizer built with HTML5 Audio API
Stars: ✭ 1,025 (-16.8%)
Mutual labels:  visualizer
Candle
GRBL controller application with G-Code visualizer written in Qt.
Stars: ✭ 796 (-35.39%)
Mutual labels:  visualizer
Jaeger Client Ruby
OpenTracing Tracer implementation for Jaeger in Ruby
Stars: ✭ 59 (-95.21%)
Mutual labels:  trace
Go Trace
Implementing a path tracer in Go
Stars: ✭ 77 (-93.75%)
Mutual labels:  trace
Kubectl Trace
Schedule bpftrace programs on your kubernetes cluster using the kubectl
Stars: ✭ 1,194 (-3.08%)
Mutual labels:  trace
Unimic
A wrapper for Unity's Microphone class.
Stars: ✭ 65 (-94.72%)
Mutual labels:  visualizer

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.props.LifecyclePanel/> element in their rendering to show a panel with lifecycle methods, which 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. (For a better view of the log, press the 'Open in New Window' button in the top-right corner.)

The panel shows the new React 16.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 on 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:8000/.

Using the npm package

$ npm i react-lifecycle-visualizer

Setup

To 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 resetInstanceIdCounters in the module where you set up hot reloading:

import { resetInstanceIdCounters } from 'react-lifecycle-visualizer';
..
resetInstanceIdCounters(); // reset 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.

Tracing 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.props.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);

Traced component props: LifecyclePanel and trace

The traced component receives two additional props: LifecyclePanel and trace. The LifecyclePanel prop is a component that can be included in the rendering with <this.props.LifecyclePanel/> to display the lifecycle methods of the traced component.

render() {
  return (
    ..
    <this.props.LifecyclePanel/>
    ..
  );
}

The trace prop is a function of type (msg: string) => void that can be used to log custom messages:

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

In the constructor we can use this.props.trace after the call to super, or access trace on the props parameter:

constructor(props) {
  props.trace('before super(props)');
  super(props);
  this.props.trace('after super(props)');
}

In the static getDerivedStateFromProps we cannot use this to refer to the component instance, but we can access trace on the nextProps parameter:

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

TypeScript

There's no need to install additional TypeScript typings, as these are already included in the package. The interface TraceProps declares the trace and LifecyclePanel props. Its definition is

export interface TraceProps {
  trace: (msg: string) => void,
  LifecyclePanel : React.SFC
}

With the exception of tracing a component, the TypeScript setup is the same as the JavaScript setup above. Here's an example of a traced component in TypeScript:

import { traceLifecycle, TraceProps } from 'react-lifecycle-visualizer';
..
interface ComponentToTraceProps extends TraceProps {}; // add trace & LifecyclePanel props
interface ComponentToTraceState {}

class ComponentToTrace extends React.Component<ComponentToTraceProps, ComponentToTraceState> {
  constructor(props: ComponentToTraceProps, context?: any) {
    props.trace('before super(props)');
    super(props, context);
    this.props.trace('after super(props)');
  }

  static getDerivedStateFromProps(nextProps : ComponentToTraceProps, nextState: ComponentToTraceState) {
    nextProps.trace('deriving');
    return null;
  }

  render() {
    return <this.props.LifecyclePanel/>;
  }
}

The only difference is that we cannot use traceLifecycle as a decorator in TypeScript, because it changes the signature of the parameter class (see this issue). Instead, we simply apply it as a function:

const TracedComponent = traceLifecycle(ComponentToTrace);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].