const ListItem = React.createClass({
render: function() {
return <li > {
this.props.item
} < /li>;
}
});
const ToDoList = React.createClass({
render: function() {
const todoItems = this.props.items.map(item => {
return <ListItem item = {
item
} > < /ListItem>
})
return ( <
ul > {
todoItems
} < /ul>
);
}
});
//creating a basic component with no data, just a render function
const MyApp = React.createClass({
render: function() {
return ( <
div className = "well" >
<
h1 > Hello < /h1> <
ToDoList > < /ToDoList> <
/div>
);
}
});
//insert the component into the DOM
ReactDOM.render( < MyApp / > , document.getElementById('container'));
<
div id = "container" > < /div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="container"></div>
ReactJs 教程说:
如果我们想让它成为一个真正可扩展的列表,我们可以创建一个项目数组,然后通过 ToDoList 组件将它们传递给 props,然后渲染出每个项目。让我们现在就这样做。
如何在上面的代码中传递项目数组?