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

Added failing tests for mutable source mutate during render #20199

Closed
wants to merge 1 commit into from

Conversation

bvaughn
Copy link
Contributor

@bvaughn bvaughn commented Nov 9, 2020

Reported in #19948.

This test case highlights that mutating a source while rendering a component that reads from the source causes mutable source to throw an error:

Cannot read from mutable source during the current render without tearing. This is a bug in React. Please file an issue.

Mutating a mutable source (or any external variable) during render isn't supported and is expected not to work. The problem here is that the error message misattributes it as a React bug.

Should this be supported?

This seems very similar lazily initializing a ref so it might be tempting to think it's okay. Mutable sources are external though, so modifying them during render is a side effect which makes it unsafe and unsupported. So while we could "support" this case in a very limited way, we shouldn't.

Could we warn better?

Maybe there's a way we could detect this case and give a better error message though? Conceptually, checking the mutable source version an additional time before returning the result of the render would be sufficient to detect and warn about this. I think this would require us to track all of the mutable sources that are read during render and loop through them at the end of renderWithHooks to check one more time. This seems like something we would not want to do in production and throwing a different error in DEV seems weird. We could log an error and then throw but that also seems weird.

Maybe an alternate approach would be to store the version number of the source somewhere (where we store other DEV-only metadata, maybe the source itself) the first time a Fiber reads it and then check the version if the Fiber renders again to make sure it's the same. I'll look into this.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Nov 9, 2020
@codesandbox-ci
Copy link

codesandbox-ci bot commented Nov 9, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1a8ee88:

Sandbox Source
React Configuration

@sizebot
Copy link

sizebot commented Nov 9, 2020

No significant bundle size changes to report.

Size changes (experimental)

Generated by 🚫 dangerJS against 1a8ee88

@sizebot
Copy link

sizebot commented Nov 9, 2020

No significant bundle size changes to report.

Size changes (stable)

Generated by 🚫 dangerJS against 1a8ee88

@bvaughn bvaughn closed this Jan 26, 2021
@bvaughn bvaughn deleted the useMutableSource-bug-report branch January 26, 2021 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants