I am currently trying to create a component that onclick of a button gets appended to a parent component of DOM element. However I am having a problem getting the initial loop working. Here is what I am doing,
class GenerateInvoice extends Component {
constructor(props) {
super(props);
this.state = {
'invoice': {
'items' : {}
}
};
this.onAddChild = this.onAddChild.bind(this);
}
render() {
const children = [];
for (var i = 0; i < Object.keys(this.state.invoice.items); i += 1) {
children.push(<InvoiceItemForm key={i} number={i} />);
};
return(
<div>
<a href="" onClick={this.onAddChild}>Add New Item</a>
{children}
</div>
)
}
onAddChild = (e) => {
e.preventDefault();
let invoice = this.state.invoice.items;
this.setState({ invoice : {'id' : 'INV001'} });
}
}
export default GenerateInvoice ;
However when I client the button with onAddChild callback on it, I get
Cannot convert undefined or null to object
why would this be?
Here is a link to my test project,
[<>]toolbar button). Stack Snippets support React, including JSX; here's how to do one.iin that loop) for...well...for just about anything. I suspectthis.state.invoice.itemsshould be an array, not an object. Also note that the block attached to a control statement (if,for, etc.) doesn't take a;at the end. (The parser will ignore it because it allows EmptyStatement, but it doesn't belong there.)