Skip to content

Commit 35e17fd

Browse files
committed
Don't pass trace as extra argument
Using `props.trace` (in the constructor) or `nextProps.trace` (in `getDerivedStateFromProps`) is easy enough and more consistent.
1 parent 0535e90 commit 35e17fd

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ resetInstanceIdCounters(); // clear instance counters on hot reload
7979

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

82-
#### Trace components
82+
#### Tracing components
8383

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

@@ -112,19 +112,43 @@ class ComponentToTraceOrg extends React.Component {...}
112112
const ComponentToTrace = traceLifeCycle(ComponentToTraceOrg);
113113
```
114114

115-
A `this.props.trace` method gets added to component and can be used to log specific information:
115+
#### Traced component props: `LifecyclePanel` and `trace`
116+
117+
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.
118+
119+
```jsx
120+
render() {
121+
return (
122+
..
123+
<this.props.LifecyclePanel/>
124+
..
125+
);
126+
}
127+
```
128+
129+
The `trace` prop is a function of type `(msg: string) => void` that can be used to log custom messages:
116130

117131
```jsx
118132
componentDidUpdate(prevProps, prevState) {
119133
this.props.trace('prevProps: ' + JSON.stringify(prevProps));
120134
}
121135
```
122136

123-
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:
137+
In the constructor we can use `this.props.trace` after the call to `super`, or access `trace` on the `props` parameter:
138+
139+
```jsx
140+
constructor(props) {
141+
props.trace('before super(props)');
142+
super(props);
143+
this.props.trace('after super(props)');
144+
}
145+
```
146+
147+
In the static `getDerivedStateFromProps` we cannot use `this` to refer to the component instance, but we can access `trace` on the `nextProps` parameter:
124148

125149
```jsx
126-
static getDerivedStateFromProps(nextProps, prevState, trace) {
127-
trace('nextProps: ' + JSON.stringify(nextProps));
150+
static getDerivedStateFromProps(nextProps, prevState) {
151+
nextProps.trace('nextProps: ' + JSON.stringify(nextProps));
128152
..
129153
}
130154
```

src/traceLifecycle.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function traceLifecycle(ComponentToTrace) {
4545
class TracedComponent extends ComponentToTrace {
4646
constructor(props, context) {
4747
props.trace(MConstructor);
48-
super(props, context, props.trace);
48+
super(props, context);
4949
this.LifecyclePanel = withDeprecationWarning(
5050
constants.DEPRECATED_THIS_LIFECYCLE_PANEL,
5151
props.LifecyclePanel
@@ -70,7 +70,7 @@ export default function traceLifecycle(ComponentToTrace) {
7070
static getDerivedStateFromProps(nextProps, prevState) {
7171
nextProps.trace(MGetDerivedState);
7272
return ComponentToTrace.getDerivedStateFromProps
73-
? ComponentToTrace.getDerivedStateFromProps(nextProps, prevState, nextProps.trace)
73+
? ComponentToTrace.getDerivedStateFromProps(nextProps, prevState)
7474
: null;
7575
}
7676

test/TracedChild.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class Child extends Component {
66

77
static staticProperty = 'a static property'
88

9-
constructor(props, context, trace) {
10-
super(props, context, trace);
11-
trace('custom:constructor');
9+
constructor(props, context) {
10+
super(props, context);
11+
props.trace('custom:constructor');
1212
}
1313

1414
// eslint-disable-next-line no-unused-vars

test/TracedLegacyChild.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { traceLifecycle } from '../src';
44
class LegacyChild extends Component {
55
state = {}
66

7-
constructor(props, context, trace) {
8-
super(props, context, trace);
9-
trace('custom:constructor');
7+
constructor(props, context) {
8+
super(props, context);
9+
props.trace('custom:constructor');
1010
}
1111

1212
componentWillMount() {

0 commit comments

Comments
 (0)