-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Are React v16 props frozen or sealed ? #11520
Comments
Can you please look in to this Syntax |
i finally tracked down my issue of trying to upgrade from v16.0.0 to v16.1.0 to a similar issue. in my case, i have a unit test that is trying to stub Object.freeze({
renderToString: renderToString,
renderToStaticMarkup: renderToStaticMarkup,
renderToNodeStream: renderToNodeStream,
renderToStaticNodeStream: renderToStaticNodeStream,
version: version
}); which results in the following error being thrown in my test:
is this frozen intentionally? if so, is there a suggested work around for testing? |
@vamshi2000 this is a typo while I was writing the pseudo code but the actual syntax is correct. @travi I took a while to figure too and the deal is that it spreads! even strings are copied as reference and frozen so it prevents future modifications in the destination object. Overall, inspite it may give performance gains I think freezing takes more than it adds. React team, what’s your thoughts ? |
it appears to not be frozen in the source, so i assume that it gets added by the transpilation? |
I don't necessarily think the issue described by OP and the issue in #11520 (comment) are related. Could each of you please create a minimal example reproducing it? #11520 (comment) is definitely caused by Rollup transpilation. We can fix it by changing the server bundle entry points like The issue in the original post is less clear to me. We'll need a reproducing example for that one. I'm actually not convinced it is a bug. React does freeze the props and style objects but that wouldn't freeze individual props (only the object containing them). That is, unless you're somehow passing them as a prop or style object to something. Need to see the whole code to say more. |
thanks for the quick response. i can try to get a repro together tonight and see if i follow your suggestion well enough to put a quick PR together too. i'm pretty sure i follow, but will look a little deeper once i have a little more time to confirm. since you think these are unrelated to each other, would you rather i create a separate issue to simplify tracking each one? |
Yes please. |
i split out #11526 |
I couldn't reproduce the issue on a fiddle that I've created: https://jsfiddle.net/wro6hcrd/ Thanks so far, |
I think what's happening is that you're passing <div style={styles.container} /> In this case of course it is not allowed to mutate it later: styles.container.fontFamily = something; You are not allowed to mutate objects you pass as styles to React because then the style will be different between re-renders. This is very hard to debug and was a common source of bugs before we started freezing styles. Mutating styles was deprecated in React 15. If you run this sort of code, React 15 will warn you that you're mutating a style and it won't work in a future version of React. React 16 tightened this up with I hope this helps! |
Note that the above comment is about freezing style objects (new in 16 but mutating has been deprecated earlier). It is not about freezing props. Props have been frozen since React 0.14 (two years ago). Freezing doesn't work recursively. Only the In strict mode, JS engine throws on mutation of a frozen object (as opposed to silently discarding it). Normally, you opt into strict mode with |
I've being trying to update my application to React 16 but I'm getting this error message in Chrome:
My pseudo code is currently like this:
I've noticed that the error happens only the second time render is run.
Also if I test in the debugger
Object.isFrozen(this.props)
inside the render method I'm gettingtrue
as return.Which means that
this.props.theme.font
is frozen by react and this state is being copied to my styles object. And on the second pass it cannot be replaced because its frozen.This didn't happen in previous versions of React.
So, is React freezing properties now ?
The text was updated successfully, but these errors were encountered: