Skip to content

Commit 65bd052

Browse files
committed
Explain TypeScript setup + Minor fixes
1 parent 52ab1a1 commit 65bd052

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

README.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ An npm package ([`react-lifecycle-visualizer`](https://www.npmjs.com/package/rea
55
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 that are highlighted when the corresponding log entry is selected.
66

77
<p align="center">
8-
<a href="https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo">
8+
<a href="https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo?file=src/samples/New.js">
99
<img
1010
alt="Parent-child demo"
1111
src="https://raw.githubusercontent.com/Oblosys/react-lifecycle-visualizer/master/images/parent-child-demo.gif"
@@ -17,9 +17,9 @@ To trace a component, apply the higher-order component `traceLifecycle` to it, a
1717
## Usage
1818

1919
The easiest way to get started is to
20-
open the [StackBlitz project](https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo?file=src/samples/New.js) and edit the sample components in `src/samples`.
20+
open the [StackBlitz project](https://stackblitz.com/github/Oblosys/react-lifecycle-visualizer/tree/master/examples/parent-child-demo?file=src/samples/New.js) 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.)
2121

22-
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.
22+
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.
2323

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

@@ -153,6 +153,50 @@ static getDerivedStateFromProps(nextProps, prevState) {
153153
}
154154
```
155155

156+
## TypeScript
157+
158+
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
159+
160+
```typescript
161+
export interface TraceProps {
162+
trace: (msg: string) => void,
163+
LifecyclePanel : React.SFC
164+
}
165+
```
166+
167+
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:
168+
169+
<!-- GitHub doesn't recognize ```tsx -->
170+
```jsx
171+
import { traceLifecycle, TraceProps } from 'react-lifecycle-visualizer';
172+
..
173+
interface ComponentToTraceProps extends TraceProps {}; // add trace & LifecyclePanel props
174+
interface ComponentToTraceState {}
175+
176+
class ComponentToTrace extends React.Component<ComponentToTraceProps, ComponentToTraceState> {
177+
constructor(props: ComponentToTraceProps, context?: any) {
178+
props.trace('before super(props)');
179+
super(props, context);
180+
this.props.trace('after super(props)');
181+
}
182+
183+
static getDerivedStateFromProps(nextProps : ComponentToTraceProps, nextState: ComponentToTraceState) {
184+
nextProps.trace('deriving');
185+
return null;
186+
}
187+
188+
render() {
189+
return <this.props.LifecyclePanel/>;
190+
}
191+
}
192+
193+
```
194+
195+
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](https://github.com/DefinitelyTyped/DefinitelyTyped/issues/20796)). Instead, we simply apply it as a function:
196+
197+
```tsx
198+
const TracedComponent = traceLifecycle(ComponentToTrace);
199+
```
156200

157201
<!-- ## API
158202

0 commit comments

Comments
 (0)