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

Upgrading to 0.9.0 prevents extensions, what is the recommended fix? #2378

Closed
pickhardt opened this issue Aug 18, 2015 · 4 comments
Closed
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@pickhardt
Copy link

After upgrading to 0.9.0, I'm getting an error "Cannot add property "hideModal", object is not extensible." I also see the warning: Cannot update during an existing state transition (such as within render). Render methods should be a pure function of props and state.

I've been previously added some properties to the children props inside render:

render: function() {
  var childrenWithModalProps = [].concat(this.props.children);
  for (var i = 0; i < childrenWithModalProps.length; i++) {
    childrenWithModalProps[i].props["hideModal"] = this.hideModal;
    childrenWithModalProps[i].props["showModal"] = this.showModal;
  }
  ...

I understand the problem is you're using preventExtensions or whatever to prevent extensions of props inside render. Where am I supposed to extend the props then? Is there a beforeRender or something I can use to accomplish this?

@ide
Copy link
Contributor

ide commented Aug 18, 2015

You should use cloneElement:

React.cloneElement(child, {
  hideModal: this.hideModal,
  showModal: this.showModal,
})

The principle is that an element's props should be set only by its owner, which created the element.

(FYI questions about React are better suited for the main repo at https://github.com/facebook/react/)

@pickhardt
Copy link
Author

OK, yeah I didn't realize it was a React issue cause it happened when I upgraded RN.

Do you know why that is? It seems wasteful to potentially clone a ton of elements.

@ide
Copy link
Contributor

ide commented Aug 19, 2015

There's some discussion here about freezing props: facebook/react#1753. I think it boils down to avoiding shared mutable state.

@pickhardt
Copy link
Author

I see. Thanks.

@facebook facebook locked as resolved and limited conversation to collaborators Jul 21, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants