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

context does not update using unstable_renderSubtreeIntoContainer #6599

Closed
kalaxiya opened this issue Apr 24, 2016 · 7 comments · Fixed by #7125
Closed

context does not update using unstable_renderSubtreeIntoContainer #6599

kalaxiya opened this issue Apr 24, 2016 · 7 comments · Fixed by #7125

Comments

@kalaxiya
Copy link

I use ReactDOM.unstable_renderSubtreeIntoContainer to render a portal.

class Modal extends React.Component {

componentDidMount() {
    this.node = document.createElement('div');
    document.body.appendChild(this.node);
    this.renderPortal(this.props);
}

componentWillReceiveProps(nextProps) {
    this.renderPortal(nextProps);
}

componentWillUnmount() {
    ReactDOM.unmountComponentAtNode(this.node);
    document.body.removeChild(this.node);
}

renderPortal = (props) => {
    renderSubtreeIntoContainer(this, <ModalPortal {...props} />, this.node);
}

render() {
    return null;
  }
}

In my root component:

class App extends React.Component {
    static childContextTypes = {
        highlightColor: React.PropTypes.object
    }

    getChildContext() {
        return {
            highlightColor: this.props.highlightColor
        }
    }

    ...
}

The context of root component will be updated and passed to children if this.props.highlightColor is changed. This is right.
I can access the correct context in the <ModalPortal/> component in the first render.
And after that the context passed to <ModalPortal/> does not change any more when the context of root component changed.
But I can access the correct context in the <Modal/> every render.

what's wrong with my code ?

@gaearon
Copy link
Collaborator

gaearon commented Apr 24, 2016

Is it possible that you have a shouldComponentUpdate there? (Or something that implements it, e.g. connect() from React Redux.) There is a known issue about it in #2517.

Alternatively this may be a new issue. Can you provide a JSFiddle reproducing it?

@danielberndt
Copy link

Here's a minimal fiddle illustrating the issue:
http://codepen.io/anon/pen/LZPxER?editors=1010

no shouldComponentUpdate here, just plain context passing. The context is not updated when unstable_renderSubtreeIntoContainer is involved.

@gaearon
Copy link
Collaborator

gaearon commented May 29, 2016

Can you submit a PR with a failing test case please?

@danielberndt
Copy link

You can find the PR here: #6924

It's my first PR to react. So I hope, I didn't miss anything obvious! 😰

@iotch
Copy link

iotch commented Jun 8, 2016

Is it safe to use unstable_renderSubtreeIntoContainer in production? It seems like it may be changed or removed at any time. Is there any production alternatives to render a component into a DOM node manually?

@mikekidder
Copy link

@iotch -- a little insight from this tweet https://twitter.com/soprano/status/737316379712331776

@iclanzan
Copy link

I can confirm that context does not update when using unstable_renderSubtreeIntoContainer.

gaearon added a commit that referenced this issue Jun 27, 2016
* create failing test case

* Fix renderSubtreeIntoContainer to update context

Fixes #6599

* Also test re-rendering due to prop update

* Address review feedback
zpao pushed a commit that referenced this issue Jul 8, 2016
* create failing test case

* Fix renderSubtreeIntoContainer to update context

Fixes #6599

* Also test re-rendering due to prop update

* Address review feedback

(cherry picked from commit 25f9f45)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants