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

Forbid readContext() during render-phase class setState() reducer form #14670

Closed

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Jan 23, 2019

Depends on #14672

Addresses #14653 (comment) (I think?)

Not sure we want to actually do it but figured I'd send a PR.

expect(ReactNoop.flush).toThrow(
'Context can only be read while React is rendering',
),
).toWarnDev('Cannot update during an existing state transition', {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We warn anyway so maybe it isn't worth it?

@acdlite
Copy link
Collaborator

acdlite commented Jan 23, 2019

The reason to do this is because the semantics are currently undefined — what happens when context changes? Re-rendering will have no effect because the update already happened — and we want to preserve option value to possibly support it properly in the future. Like for code splitting reducers. Same reason we warn about reading context inside Hook reducer.

getDerivedStateFromProps is fine because we call it every time.

@sizebot
Copy link

sizebot commented Jan 23, 2019

ReactDOM: size: 0.0%, gzip: 🔺+0.2%

Details of bundled changes.

Comparing: f0befae...d0fabf1

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.1% 738.95 KB 739.44 KB 170.53 KB 170.78 KB UMD_DEV
react-dom.production.min.js 0.0% 🔺+0.2% 98.88 KB 98.9 KB 32.17 KB 32.23 KB UMD_PROD
react-dom.profiling.min.js 0.0% +0.1% 101.86 KB 101.89 KB 32.82 KB 32.86 KB UMD_PROFILING
react-dom.development.js +0.1% +0.2% 734 KB 734.5 KB 169.1 KB 169.35 KB NODE_DEV
react-dom.production.min.js 0.0% 🔺+0.1% 98.88 KB 98.91 KB 31.67 KB 31.7 KB NODE_PROD
react-dom.profiling.min.js 0.0% +0.1% 101.98 KB 102.01 KB 32.28 KB 32.33 KB NODE_PROFILING
ReactDOM-dev.js +0.1% +0.1% 756.57 KB 757.13 KB 170.34 KB 170.58 KB FB_WWW_DEV
ReactDOM-prod.js -0.3% -0.1% 312.5 KB 311.58 KB 57.56 KB 57.5 KB FB_WWW_PROD
ReactDOM-profiling.js -0.3% -0.1% 319.66 KB 318.74 KB 58.88 KB 58.79 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 739.29 KB 739.79 KB 170.67 KB 170.92 KB UMD_DEV
react-dom-unstable-fire.production.min.js 0.0% 🔺+0.2% 98.89 KB 98.92 KB 32.18 KB 32.24 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js 0.0% +0.1% 101.88 KB 101.91 KB 32.83 KB 32.87 KB UMD_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.2% 734.35 KB 734.84 KB 169.24 KB 169.5 KB NODE_DEV
react-dom-unstable-fire.production.min.js 0.0% 🔺+0.1% 98.9 KB 98.92 KB 31.68 KB 31.7 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js 0.0% +0.1% 102 KB 102.03 KB 32.29 KB 32.33 KB NODE_PROFILING
ReactFire-dev.js +0.1% +0.1% 755.78 KB 756.34 KB 170.29 KB 170.53 KB FB_WWW_DEV
ReactFire-prod.js -0.3% -0.2% 301.09 KB 300.17 KB 55.26 KB 55.17 KB FB_WWW_PROD
ReactFire-profiling.js -0.3% -0.1% 308.18 KB 307.26 KB 56.55 KB 56.49 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% -0.0% 44.87 KB 44.87 KB 12.3 KB 12.3 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 9.74 KB 9.74 KB 3.65 KB 3.64 KB NODE_PROD
ReactDOMServer-prod.js 0.0% 0.0% 45.18 KB 45.18 KB 10.39 KB 10.39 KB FB_WWW_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.2% 1.1 KB 1.1 KB 665 B 666 B NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% 0.0% 517.8 KB 518.29 KB 114.37 KB 114.38 KB UMD_DEV
react-art.production.min.js 0.0% 🔺+0.1% 90.93 KB 90.96 KB 27.96 KB 27.98 KB UMD_PROD
react-art.development.js +0.1% 0.0% 449.31 KB 449.81 KB 97.25 KB 97.28 KB NODE_DEV
react-art.production.min.js 🔺+0.1% 🔺+0.2% 55.91 KB 55.94 KB 17.22 KB 17.25 KB NODE_PROD
ReactART-dev.js +0.1% +0.2% 458.29 KB 458.85 KB 96.4 KB 96.61 KB FB_WWW_DEV
ReactART-prod.js -0.5% -0.2% 188.4 KB 187.48 KB 32.23 KB 32.16 KB FB_WWW_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +0.1% 0.0% 582.79 KB 583.34 KB 126.96 KB 126.96 KB RN_FB_DEV
ReactNativeRenderer-prod.js -0.4% -0.2% 244.14 KB 243.22 KB 42.9 KB 42.81 KB RN_FB_PROD
ReactNativeRenderer-profiling.js -0.4% -0.2% 250.29 KB 249.37 KB 44.23 KB 44.16 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +0.1% -0.0% 582.7 KB 583.26 KB 126.92 KB 126.92 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.2% 227.18 KB 227.37 KB 39.53 KB 39.62 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js +0.1% 0.0% 233.19 KB 233.38 KB 40.91 KB 40.93 KB RN_OSS_PROFILING
ReactFabric-dev.js +0.1% 0.0% 573.64 KB 574.2 KB 124.69 KB 124.69 KB RN_FB_DEV
ReactFabric-prod.js -0.4% -0.2% 237.34 KB 236.42 KB 41.44 KB 41.38 KB RN_FB_PROD
ReactFabric-profiling.js -0.4% -0.2% 242.64 KB 241.72 KB 42.76 KB 42.67 KB RN_FB_PROFILING
ReactFabric-dev.js +0.1% 0.0% 573.55 KB 574.1 KB 124.64 KB 124.65 KB RN_OSS_DEV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 220.41 KB 220.6 KB 38.07 KB 38.1 KB RN_OSS_PROD
ReactFabric-profiling.js +0.1% 0.0% 225.5 KB 225.69 KB 39.45 KB 39.46 KB RN_OSS_PROFILING

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.1% +0.2% 462.53 KB 463.02 KB 100.01 KB 100.21 KB UMD_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.2% 57.31 KB 57.34 KB 17.6 KB 17.63 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.2% 457.49 KB 457.98 KB 98.78 KB 98.98 KB NODE_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.2% 56.98 KB 57.01 KB 17.45 KB 17.47 KB NODE_PROD
ReactTestRenderer-dev.js +0.1% +0.2% 466.68 KB 467.23 KB 98.27 KB 98.48 KB FB_WWW_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.2 KB 11.2 KB 3.49 KB 3.49 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.1% 0.0% 447.14 KB 447.64 KB 95.76 KB 95.78 KB NODE_DEV
react-reconciler.production.min.js 🔺+0.1% 🔺+0.2% 57.07 KB 57.1 KB 17.07 KB 17.1 KB NODE_PROD
react-reconciler-persistent.development.js +0.1% 0.0% 445.52 KB 446.02 KB 95.11 KB 95.13 KB NODE_DEV
react-reconciler-persistent.production.min.js 🔺+0.1% 🔺+0.2% 57.08 KB 57.11 KB 17.07 KB 17.11 KB NODE_PROD

Generated by 🚫 dangerJS

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 23, 2019

That's true but we already unconditionally warn for render phase setState anyway. You can see it in the test. Usually if there's a warning, historically we didn't mind undefined behavior. It's meant to not officially be supported.

I guess we probably mute that warning at FB though?

@sebmarkbage
Copy link
Collaborator

Added a comment that I don’t think we should do this in prod.

#14653 (comment)

@acdlite
Copy link
Collaborator

acdlite commented Jan 23, 2019

Yeah warning is sufficient

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 23, 2019

Do we need an extra warning for this specifically? This test already warns with "Cannot update during an existing state transition".

@gaearon gaearon force-pushed the forbid-ren-phase-setstate-cls branch from b555c33 to d0fabf1 Compare January 23, 2019 16:56
@gaearon
Copy link
Collaborator Author

gaearon commented Jan 23, 2019

I made this depend on #14672 and added a similar DEV-only warning on top.

@gaearon
Copy link
Collaborator Author

gaearon commented Jan 23, 2019

It's annoying to have so many small ones. I'll send a combined one.

@gaearon gaearon closed this Jan 23, 2019
@gaearon
Copy link
Collaborator Author

gaearon commented Jan 23, 2019

Superseded by #14677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants