I want to pass a template as props or as children and everytime I click 'add' i want to add one more item.
This is where I got so far:
App.js
<List template={
(<Item>
<span>Test</span>
</Item>)
} />
List.js
...
add = () => {
const list = this.state.list;
const i = list.length + 1;
const newItem = cloneElement(this.props.template, { key: i });
this.setState({ list: this.state.list.concat([newItem]) });
}
...
render() {
return (
<div className="list">
{this.state.list}
<button onClick={this.add}>Add</button>
</div>
);
}
But altough a new item is added, it has no children, so the 'Item' is there, but the 'span' is not. So the list is always empty.
Anyone knows how to do something like that?
The point is that the List mustn't be aware of what is passed to it. Whatever you pass, it should add.