Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools bugfix for useState() with hasOwnProperty key #21524

Merged
merged 1 commit into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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