Skip to content

Commit 15b3e6e

Browse files
author
Brian Vaughn
committed
Changed overrideHook to support useReducer overrides; added isEditable bool to hooks metadata
1 parent fe5271d commit 15b3e6e

File tree

4 files changed

+180
-38
lines changed

4 files changed

+180
-38
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ const Dispatcher: DispatcherType = {
233233

234234
type HooksNode = {
235235
index: number,
236+
isEditable: boolean,
236237
name: string,
237238
value: mixed,
238239
subHooks: Array<HooksNode>,
@@ -406,6 +407,7 @@ function buildTree(rootStack, readHookLog): HooksTree {
406407
let children = [];
407408
levelChildren.push({
408409
index: -1,
410+
isEditable: false,
409411
name: parseCustomHookName(stack[j - 1].functionName),
410412
value: undefined,
411413
subHooks: children,
@@ -415,9 +417,11 @@ function buildTree(rootStack, readHookLog): HooksTree {
415417
}
416418
prevStack = stack;
417419
}
420+
const {primitive} = hook;
418421
levelChildren.push({
419-
index: hook.primitive === 'DebugValue' ? -1 : index++,
420-
name: hook.primitive,
422+
index: primitive === 'DebugValue' ? -1 : index++,
423+
isEditable: primitive === 'Reducer' || primitive === 'State',
424+
name: primitive,
421425
value: hook.value,
422426
subHooks: [],
423427
});

packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('ReactHooksInspection', () => {
2828
let tree = ReactDebugTools.inspectHooks(Foo, {});
2929
expect(tree).toEqual([
3030
{
31+
isEditable: true,
3132
index: 0,
3233
name: 'State',
3334
value: 'hello world',
@@ -49,11 +50,13 @@ describe('ReactHooksInspection', () => {
4950
let tree = ReactDebugTools.inspectHooks(Foo, {});
5051
expect(tree).toEqual([
5152
{
53+
isEditable: false,
5254
index: -1,
5355
name: 'Custom',
5456
value: __DEV__ ? 'custom hook label' : undefined,
5557
subHooks: [
5658
{
59+
isEditable: true,
5760
index: 0,
5861
name: 'State',
5962
value: 'hello world',
@@ -83,17 +86,20 @@ describe('ReactHooksInspection', () => {
8386
let tree = ReactDebugTools.inspectHooks(Foo, {});
8487
expect(tree).toEqual([
8588
{
89+
isEditable: false,
8690
index: -1,
8791
name: 'Custom',
8892
value: undefined,
8993
subHooks: [
9094
{
95+
isEditable: true,
9196
index: 0,
9297
name: 'State',
9398
subHooks: [],
9499
value: 'hello',
95100
},
96101
{
102+
isEditable: false,
97103
index: 1,
98104
name: 'Effect',
99105
subHooks: [],
@@ -102,17 +108,20 @@ describe('ReactHooksInspection', () => {
102108
],
103109
},
104110
{
111+
isEditable: false,
105112
index: -1,
106113
name: 'Custom',
107114
value: undefined,
108115
subHooks: [
109116
{
117+
isEditable: true,
110118
index: 2,
111119
name: 'State',
112120
value: 'world',
113121
subHooks: [],
114122
},
115123
{
124+
isEditable: false,
116125
index: 3,
117126
name: 'Effect',
118127
value: effect,
@@ -152,22 +161,26 @@ describe('ReactHooksInspection', () => {
152161
let tree = ReactDebugTools.inspectHooks(Foo, {});
153162
expect(tree).toEqual([
154163
{
164+
isEditable: false,
155165
index: -1,
156166
name: 'Bar',
157167
value: undefined,
158168
subHooks: [
159169
{
170+
isEditable: false,
160171
index: -1,
161172
name: 'Custom',
162173
value: undefined,
163174
subHooks: [
164175
{
176+
isEditable: true,
165177
index: 0,
166178
name: 'Reducer',
167179
value: 'hello',
168180
subHooks: [],
169181
},
170182
{
183+
isEditable: false,
171184
index: 1,
172185
name: 'Effect',
173186
value: effect,
@@ -176,6 +189,7 @@ describe('ReactHooksInspection', () => {
176189
],
177190
},
178191
{
192+
isEditable: false,
179193
index: 2,
180194
name: 'LayoutEffect',
181195
value: effect,
@@ -184,27 +198,32 @@ describe('ReactHooksInspection', () => {
184198
],
185199
},
186200
{
201+
isEditable: false,
187202
index: -1,
188203
name: 'Baz',
189204
value: undefined,
190205
subHooks: [
191206
{
207+
isEditable: false,
192208
index: 3,
193209
name: 'LayoutEffect',
194210
value: effect,
195211
subHooks: [],
196212
},
197213
{
214+
isEditable: false,
198215
index: -1,
199216
name: 'Custom',
200217
subHooks: [
201218
{
219+
isEditable: true,
202220
index: 4,
203221
name: 'Reducer',
204222
subHooks: [],
205223
value: 'world',
206224
},
207225
{
226+
isEditable: false,
208227
index: 5,
209228
name: 'Effect',
210229
subHooks: [],
@@ -227,6 +246,7 @@ describe('ReactHooksInspection', () => {
227246
let tree = ReactDebugTools.inspectHooks(Foo, {});
228247
expect(tree).toEqual([
229248
{
249+
isEditable: false,
230250
index: 0,
231251
name: 'Context',
232252
value: 'default',
@@ -290,10 +310,13 @@ describe('ReactHooksInspection', () => {
290310
let tree = ReactDebugTools.inspectHooks(Foo, {});
291311
expect(tree).toEqual([
292312
{
313+
isEditable: false,
293314
index: -1,
294315
name: 'Custom',
295316
value: __DEV__ ? 'bar:123' : undefined,
296-
subHooks: [{index: 0, name: 'State', subHooks: [], value: 0}],
317+
subHooks: [
318+
{isEditable: true, index: 0, name: 'State', subHooks: [], value: 0},
319+
],
297320
},
298321
]);
299322
});

0 commit comments

Comments
 (0)