Created
August 1, 2015 18:19
-
-
Save epferrari/793e3f9e30bb5aa20920 to your computer and use it in GitHub Desktop.
Using React parent-context across portals in 0.13.3 without getting console.warns. Should be React 0.14-ready without changing your code to use `renderSubtreeIntoContainer`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react/addons'; | |
import {AppContextWrapper,AppContextMixin} from './appContext.jsx'; | |
import ModalWithParentContext from './ModalWithParentContext.jsx'; | |
var App = React.createClass({ | |
mixins: [AppContextMixin], // provides context for all components in its tree | |
render(){ | |
return ( | |
<div> | |
<h1>Example</h1> | |
<p>This modal will render the context it is passed as a prop without console warnings.</p> | |
<ModalWithParentContext contextWrapper={AppContextWrapper} /> // provides context to components in Modal's tree | |
</div> | |
); | |
} | |
}); | |
React.render(<App />,document.body); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react/addons'; | |
/* for implementing parent-contexts across portals */ | |
class AppContextWrapper extends React.Component { | |
getChildContext(){ | |
return { | |
name: 'Kazooie', | |
instrument: 'banjo', | |
home: 'backpack' | |
}; | |
} | |
static childContextTypes: { | |
name: React.PropTypes.string, | |
instrument: React.PropTypes.string, | |
home: React.PropTypes.string | |
} | |
render(){ | |
return ( <span>{this.props.children}</span> ); | |
} | |
} | |
/* for implementing owner-context */ | |
const AppContextMixin = { | |
getChildContext(){ | |
return { | |
name: 'Kazooie', | |
instrument: 'banjo', | |
home: 'a backpack' | |
}; | |
}, | |
childContextTypes: { | |
name: React.PropTypes.string, | |
instrument: React.PropTypes.string, | |
home: React.PropTypes.string | |
} | |
}; | |
export {AppContextWrapper,AppContextMixin}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react/addons'; | |
class ContextualComponentA extends React.Component { | |
static contextTypes: { | |
name: React.PropTypes.string.isRequired | |
} | |
render(){ | |
return ( <h1>{this.context.name}</h1> ); | |
} | |
} | |
class ContextualComponentB extends React.Component { | |
static contextTypes: { | |
instrument: React.PropTypes.string.isRequired, | |
home: React.PropTypes.string.isRequired | |
} | |
render(){ | |
return ( | |
<p>Hi, I live in {this.context.home} and play the {this.context.instrument}. It is a good life</p> | |
); | |
} | |
} | |
export {ContextualComponentA,ContextualComponentB}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react/addons'; //0.13.3 | |
import {Modal,Button} from 'react-bootstrap'; | |
import {ContextualComponentA,ContextualComponentB} from './contextualComponents.jsx'; | |
export default class ModalWithChildContext extends React.Component { | |
static propTypes: { | |
contextWrapper: React.PropTypes.func.isRequired | |
} | |
constructor(props){ | |
super(props); | |
this.state = {modalOpen: false}; | |
} | |
openModal(){ | |
this.setState({modalOpen: true}); | |
} | |
closeModal(){ | |
this.setState({modalOpen: false}); | |
} | |
render(){ | |
let ContextWrapper = this.props.contextWrapper; | |
return( | |
<div> | |
<Button onClick={this.openModal}>Open Modal</Button> | |
<Modal animation enforceFocus backdrop {...this.props}> | |
<ContextWrapper> | |
<Modal.Header closebutton> | |
<Modal.Title> | |
<ContextualComponentA/> | |
<Modal.Title> | |
</Modal.Header> | |
<Modal.Body> | |
<ContextualComponentB/> | |
</Modal.Body> | |
</ContextWrapper> | |
</Modal> | |
</div> | |
); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment