Skip to content

Instantly share code, notes, and snippets.

@siakaramalegos
Created October 6, 2016 20:48
Show Gist options
  • Save siakaramalegos/eafd1b114ddcbe8fac923edbc9f8a553 to your computer and use it in GitHub Desktop.
Save siakaramalegos/eafd1b114ddcbe8fac923edbc9f8a553 to your computer and use it in GitHub Desktop.
Updating style based on button click in React
import React, {Component, PropTypes} from 'react'
import View from './View'
import Button from '../common/Button'
export default class Document extends Component {
static propTypes = {
program: PropTypes.object.isRequired,
}
constructor() {
super()
this.state = {
hideCosts: false,
}
}
onButtonClick = (e) => {
e.preventDefault()
this.setState({ hideCosts: true })
}
render() {
const {program} = this.props
return (
<div className="Document">
<HiddenStyle hide={this.state.hideCosts} />
<button
className="btn btn-primary preview-action"
onClick={this.onButtonClick}
>
<i className="fa fa-eye-slash" aria-hidden="true"></i>
Hide Costs
</button>
<View program={program} />
</div>
)
}
}
const HiddenStyle = ({ hide }) => {
if (hide) {
return (
<style type="text/css">
{ `.hideable-item { display: none; }` }
</style>
)
} else {
return <noscript />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment