Skip to content

Instantly share code, notes, and snippets.

@rpominov
Last active October 5, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpominov/e7a5f9d20c80a9cb6a2b to your computer and use it in GitHub Desktop.
Save rpominov/e7a5f9d20c80a9cb6a2b to your computer and use it in GitHub Desktop.
React-teleports concept

React-teleports concept

<TeleportManager>
  <Foo>
    <TeleportExit id="abc" />
    <Bar>
      <TeleportEntrance id="abc">
        <Baz />
      </TeleportEntrance>
    </Bar>
  </Foo>
</TeleportManager>

<Baz /> renders in place of <TeleportExit />, and <TeleportEntrance /> renders null.

// Supposed we have <TeleportManager> up in the tree, and <TeleportExit id="modal" /> somewhere.
const Foo = React.createClass({
getInitialState() {
return {
opened: false
}
},
render() {
return <div>
<button onClick={() => {this.setState({opened: true})}}>Open</button>
{this.state.opened && <TeleportEntrance id="modal">
<Modal foo="foo" bar="bar" />
</TeleportEntrance>}
</div>
}
})
// Note: same can be done using flux. And probably this is the right way.
const Foo = React.createClass({
render() {
return <div>
<button onClick={() => {dispatch(openModal({foo: "foo", bar: "bar"}))}}>Open</button>
</div>
}
})
// This is renderend instead of <TeleportExit id="modal" />
const ModalHost = React.createClass({
render() {
const {isModalOpened, currentModalProps} = readFromFluxStoreSomehow()
return isModalOpened && <Modal {...currentModalProps} />
}
})
// The problem of Flux solution is that you may have to store React-elements in Flux stores,
// if you want the <Modal /> to also accept the children prop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment