Skip to content

Instantly share code, notes, and snippets.

@dignifiedquire
Created May 29, 2015 16:26
Show Gist options
  • Save dignifiedquire/ccb36538839b3b86c357 to your computer and use it in GitHub Desktop.
Save dignifiedquire/ccb36538839b3b86c357 to your computer and use it in GitHub Desktop.
childClick
const Parent = React.createClass({
getInitialState() {
selectedItem: null
},
_onChildSelect(i) {
this.setState({selectedItem: i})
},
render() {
const names = [
'First',
'Second',
'Third'
]
let selected
if (this.state.selectedItem) {
selected = <span>You have selected {names[this.state.selectedItem]}</span>
}
return (
<div className='parent'>
{selected}
{names.map((name, i) => {
return (
<Child name={name} onSelect={this._onChildSelect.bind(this, i)}
)
}}
</div>
)
}
})
const Child = React.createClass({
propTypes: {
name: React.PropTypes.string.isRequired
onSelect: React.PropTypes.func
},
getDefaultPropTypes() {
return {
onSelect() {}
}
},
getInitialState() {
return {
seletected: false
}
}
_onClick(e) {
e.preventDefault()
this.setState({selected: true})
this.onSelect(e)
},
render() {
const className = classNames('row', {
selected: this.state.selected
})
return (
<div className={classname}>
<a href='' onClick={this._onClick}
{this.props.name}
</a>
</div>
)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment