-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Update docs for React 16 #10846
Update docs for React 16 #10846
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,7 +46,7 @@ A typical use case for portals is when a parent component has an `overflow: hidd | |
> | ||
> It is important to remember, when working with portals, you'll need to make sure to follow the proper accessibility guidelines. | ||
|
||
[Try out an example on CodePen.](https://codepen.io/acdlite/pen/JrKgmz) | ||
[Try it on CodePen.](https://codepen.io/gaearon/pen/yzMaBd) | ||
|
||
## Portals and event bubbling | ||
|
||
|
@@ -63,12 +63,35 @@ This includes event bubbling. An event fired from inside a portal will propagate | |
</html> | ||
``` | ||
|
||
A Parent component in `#app-root` would be able to catch an uncaught, bubbling event from the sibling node #modal-root. | ||
A `Parent` component in `#app-root` would be able to catch an uncaught, bubbling event from the sibling node `#modal-root`. | ||
|
||
```js | ||
```js{20-23,34-41,45,53-55,62-63,66} | ||
// These two containers are siblings in the DOM | ||
const appRoot = document.getElementById('app-root'); | ||
const modalRoot = document.getElementById('modal-root'); | ||
|
||
class Modal extends React.Component { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be worth adding an inline comment about accessibility, in case people copy + paste without reading the other note. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would be the best place to add it? I'll leave that to you for now, running home! |
||
constructor(props) { | ||
super(props); | ||
this.el = document.createElement('div'); | ||
} | ||
|
||
componentDidMount() { | ||
modalRoot.appendChild(this.el); | ||
} | ||
|
||
componentWillUnmount() { | ||
modalRoot.removeChild(this.el); | ||
} | ||
|
||
render() { | ||
return ReactDOM.createPortal( | ||
this.props.children, | ||
this.el, | ||
); | ||
} | ||
} | ||
|
||
class Parent extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
@@ -77,19 +100,27 @@ class Parent extends React.Component { | |
} | ||
|
||
handleClick() { | ||
// This will fire when the button in Child is clicked, updating Parent's state, | ||
// even though Child is not a direct descendant in the DOM. | ||
// This will fire when the button in Child is clicked, | ||
// updating Parent's state, even though button | ||
// is not direct descendant in the DOM. | ||
this.setState(prevState => ({ | ||
clicks: prevState.clicks + 1 | ||
})); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div onClick={this.onClick}> | ||
<div onClick={this.handleClick}> | ||
<p>Number of clicks: {this.state.clicks}</p> | ||
<p>Open up the browser DevTools to observe that the button is not a child the div with onClick handler.</p> | ||
{ReactDOM.createPortal(<Child />, modalRoot)} | ||
<p> | ||
Open up the browser DevTools | ||
to observe that the button | ||
is not a child the div | ||
with onClick handler. | ||
</p> | ||
<Modal> | ||
<Child /> | ||
</Modal> | ||
</div> | ||
); | ||
} | ||
|
@@ -105,10 +136,9 @@ function Child() { | |
); | ||
} | ||
|
||
|
||
ReactDOM.render(<Parent />, appRoot); | ||
``` | ||
|
||
[Try this example on CodePen](https://codepen.io/gaearon/pen/jGBWpE). | ||
[Try it on CodePen.](https://codepen.io/gaearon/pen/jGBWpE). | ||
|
||
Catching an event bubbling up from a portal in a parent component allows the development of more flexible abstractions that are not inherently reliant on portals. For example, if you render a `<Modal />` component, the parent can capture its events regardless of whether it's implemented using portals. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,22 +15,6 @@ import ReactDOMServer from 'react-dom/server'; | |
var ReactDOMServer = require('react-dom/server'); | ||
``` | ||
|
||
We also provide a separate entry-point for use in the browser: | ||
|
||
```js | ||
// ES modules | ||
import ReactDOMServer from 'react-dom/server.browser'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed this since it's not meant to be used directly. You'll get the right thing on the client even if you try to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh cool, my bad! |
||
// CommonJS | ||
var ReactDOMServer = require('react-dom/server.browser'); | ||
``` | ||
|
||
Or, using UMD: | ||
|
||
```html | ||
<script crossorigin src="https://unpkg.com/react-dom/umd/react-dom-server.browser.production.min.js" /> | ||
<!-- Accessible as window.ReactDOMServer --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured I'd remove this one as well since it's neither common nor recommended, and people who need it probably can figure it out. |
||
``` | ||
|
||
## Overview | ||
|
||
The following methods can be used in both the server and browser environments: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call switching these all over to your account. Maybe we could create a shared React Codepen account so that anyone can make changes without updating the link? Unless it's too much hassle to switch between accounts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be great.