Skip to content

Commit

Permalink
DevTools bugfix for useState() with hasOwnProperty key
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed May 18, 2021
1 parent 343776f commit 3809db7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
32 changes: 32 additions & 0 deletions packages/react-devtools-shared/src/__tests__/profilerStore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,36 @@ describe('ProfilerStore', () => {
});
}).toThrow('Cannot modify filter preferences while profiling');
});

it('should not throw if state contains a property hasOwnProperty ', () => {
let setStateCallback;
const ControlledInput = () => {
const [state, setState] = React.useState({hasOwnProperty: true});
setStateCallback = setState;
return state.hasOwnProperty;
};

const container = document.createElement('div');

// This element has to be in the <body> for the event system to work.
document.body.appendChild(container);

// It's important that this test uses legacy sync mode.
// The root API does not trigger this particular failing case.
ReactDOM.render(<ControlledInput />, container);

utils.act(() => store.profilerStore.startProfiling());
utils.act(() =>
setStateCallback({
hasOwnProperty: false,
}),
);
utils.act(() => store.profilerStore.stopProfiling());

// Only one commit should have been recorded (in response to the "change" event).
const root = store.roots[0];
const data = store.profilerStore.getDataForRoot(root);
expect(data.commitData).toHaveLength(1);
expect(data.operations).toHaveLength(1);
});
});
19 changes: 11 additions & 8 deletions packages/react-devtools-shared/src/backend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1221,15 +1221,18 @@ export function attach(
}

function isEffect(memoizedState) {
if (memoizedState === null || typeof memoizedState !== 'object') {
return false;
}
const {deps} = memoizedState;
const hasOwnProperty = Object.prototype.hasOwnProperty.bind(memoizedState);
return (
memoizedState !== null &&
typeof memoizedState === 'object' &&
memoizedState.hasOwnProperty('tag') &&
memoizedState.hasOwnProperty('create') &&
memoizedState.hasOwnProperty('destroy') &&
memoizedState.hasOwnProperty('deps') &&
(memoizedState.deps === null || isArray(memoizedState.deps)) &&
memoizedState.hasOwnProperty('next')
hasOwnProperty('create') &&
hasOwnProperty('destroy') &&
hasOwnProperty('deps') &&
hasOwnProperty('next') &&
hasOwnProperty('tag') &&
(deps === null || isArray(deps))
);
}

Expand Down

0 comments on commit 3809db7

Please sign in to comment.