Created
July 29, 2016 04:23
-
-
Save theonlyrao/d4338d1ec223d7e8431771e7a4385972 to your computer and use it in GitHub Desktop.
Using 'componentDidUpdate` to render component in separate div
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'; | |
import ReactDOM from 'react-dom'; | |
const Letter = React.createClass({ | |
render() { | |
return ( | |
// same html stuff ... | |
) | |
} | |
}); | |
var CapitalAButton = React.createClass({ | |
getInitialState: function(){ | |
return { showCapitalA: false }; | |
}, | |
componentDidUpdate: function(prevProps, prevState){ | |
prevState.showCapitalA? ReactDOM.unmountComponentAtNode(document.querySelector("#letter")) : ReactDOM.render(<Letter />, document.querySelector('#letter')); | |
}, | |
toggleCapitalA: function(){ | |
this.state.showCapitalA? this.setState({showCapitalA: false}) : this.setState({showCapitalA: true}) | |
}, | |
render: function() { | |
return ( | |
<div id="capital-button" onClick={this.toggleCapitalA}> | |
A | |
</div> | |
) | |
} | |
}); | |
var ButtonChoices = React.createClass({ | |
render: function() { | |
return ( | |
<CapitalAButton /> | |
) | |
} | |
}); | |
ReactDOM.render( | |
<div id="a"> | |
<ButtonChoices /> | |
</div>, | |
document.querySelector('#button-group') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment