Created
July 29, 2016 07:17
-
-
Save LiuuY/db6b389fb15db8ec9f507441e4107ffb to your computer and use it in GitHub Desktop.
Array in jsx
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
// {[...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