mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-24 20:53:08 +00:00
Recently learned that components passed into `React.renderComponent` may not be the ones actually mounted. Also learned that it returns the mounted component. Added some documentation describing this.
1.0 KiB
1.0 KiB
id, title, layout, permalink, prev
| id | title | layout | permalink | prev |
|---|---|---|---|---|
| references-to-components | References to Components | tips | references-to-components.html | expose-component-functions.html |
If you're using React components in a larger non-React application or transitioning your code to React, you may need to keep references to components. React.renderComponent returns a reference to the mounted component:
/** @jsx React.DOM */
var myComponent = React.renderComponent(<MyComponent />, myContainer);
If you pass a variable to 'React.renderComponent`, it's not guaranteed that the component passed in will be the one that's mounted. In cases where you construct a component before mounting it, be sure to reassign your variable:
/** @jsx React.DOM */
var myComponent = <MyComponent />;
// Some code here...
myComponent = React.renderComponent(myComponent, myContainer);
Note:
This should only ever be used at the top level. Inside components, let your
propsandstatehandle communication with child components, and only reference components viarefs.