Skip to content

Instantly share code, notes, and snippets.

@LiuuY
Created July 29, 2016 07:17
Show Gist options
  • Save LiuuY/db6b389fb15db8ec9f507441e4107ffb to your computer and use it in GitHub Desktop.
Save LiuuY/db6b389fb15db8ec9f507441e4107ffb to your computer and use it in GitHub Desktop.
Array in jsx
// {[...Array(10)].map((x, i) =>
// <div></div>
// )}
var Hello = React.createClass({
getInitialState() {
return {curr: 0}
},
shouldComponentUpdate(nextProps, nextState) {
return !(nextState.curr === this.state.curr)
},
handleClick(i) {
this.setState({curr: i})
},
render: function() {
// 批量生成
return (
<div>
{[...Array(this.props.total)].map((x, i) =>
<div
className={i === this.state.curr ? 'curr' : ''}
onClick={this.handleClick.bind(null, i)}
key={i}
>
{i === this.state.curr ? 'curr' : 'not curr'}
</div>
)}
</div>
)
}
})
ReactDOM.render(
<Hello total={5} />,
document.getElementById('container')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment