Skip to content

Are React v16 props frozen or sealed ? #11520

Closed
@gusdaud-zz

Description

@gusdaud-zz

I've being trying to update my application to React 16 but I'm getting this error message in Chrome:

Uncaught TypeError: Cannot assign to read only property 'fontFamily' of object '#<Object>'

My pseudo code is currently like this:

const styles = {
  container: {
    fontSize: '14px',
    fontFamily: '"Helvetica Neue",
  },
};

class MyComp extends Component {
  render = () => {
    let theme = this.props.theme;
    styles.container.fontFamily = theme.font;

    return (<div style={styles}>Testing</div>);
  }
}

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 getting true 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 ?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions