-
Notifications
You must be signed in to change notification settings - Fork 47.4k
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
[Fiber] fix ensure class components refs get re-assigned to emptyObject on mount #9045
[Fiber] fix ensure class components refs get re-assigned to emptyObject on mount #9045
Conversation
…mismatch Due to how Jest and other mocking libraries work in userland, modules that are required might not be referentially equal at various stages of the application lifecycle. This isnt a perfect fix, but by having the require calls inline, it has a better liklihood of ensuring emptyObject is the same reference
Why doesn't the same problem happen with Stack? Regardless seems like we should be fixing this in jest since the runtime code doesn't this indirection. Inlining doesn't help us switch to all ES modules and rollup. |
Stack assigns I guess we should do the same in |
That sounds like a better fix. Is that ideal in the long term though? |
In long term we'd get away from string refs anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the action item is to fix the test environment.
refs-test
test-suite so mocked modules match up referentially
@sebmarkbage @gaearon changes have been made to the test suite and the other changes have been reverted. Thanks for the feedback. |
src/renderers/__tests__/refs-test.js
Outdated
* has a ref of the form "clickLogN". | ||
* Render a TestRefsComponent and ensure that the main refs are wired up. | ||
* we also define the classes in this function to ensure that they are | ||
* wired up properly in accordance to how Jest might auto mock modules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, could you explain how this is related to automocking?
If you mean resetting modules, it's a different feature from automocking.
I don't think we have automocking enabled in React repo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message was wrong, I've changed it. I didn't mean to state automocking.
Why are we fixing the test environment rather than applying the same strategy as in Stack? Fixing the test might work for us, but I'm not sure it would work for consumers who also use Jest with |
refs-test
test-suite so mocked modules match up referentiallyrefs-test
test-suite so modules match up referentially
Even regardless of the test environment, the issue is likely to come up when you have different versions of React and ReactDOM, and thus potentially duplicated Also, don't we need to reassign them for factory components anyway? I assume they support string refs in Stack although I haven't checked. |
@gaearon I don't think defining the classes at the top scope and using |
@gaearon Ideally, ReactDOM would pull |
So, here's the case I'm concerned about: fit('wat', () => {
function Comp() {
return {
render() {
return <div ref="haha" />
}
}
}
var container = document.createElement('div');
var inst = ReactDOM.render(<Comp />, container);
expect(inst.refs.haha.tagName).toBe('DIV');
}) This works in Stack because it reassigns If we fix Fiber to also reassign |
(It wouldn't be a permanent solution but we don't depend on |
@gaearon I agree with you. As a side note: the whole emptyObject optimization should ideally be used in as many places as we can (props, state etc) where nothing has been defined. I'll update the PR to include a re-assignment for |
refs-test
test-suite so modules match up referentially
I've added a regression test for refs in factory components. @gaearon |
seems like we needed to fix another bug
Due to how Jest and other mocking libraries work in user-land, modules that are required might not be referentially equal at various stages of the application lifecycle. Previously, Stack would re-assign
emptyObject
upon mounting a class component, ensuring the reference toemptyObject
remained consistent. This PR re-applies this change to class components in Fiber.