Hey, this is my first app in React. I know this is small, but can you give me some advice and corrections. It is good code?
Hey VanCleft,
great work so far.
I re-wrote some stuff:
Old:
addItem = event => {
let items = this.state.items;
let newText = this.state.value;
let newObject = { text: newText, checked: false };
items.push(newObject);
this.setState({ items: items });
this.setState({ value: "" });
event.preventDefault();
};
New:
addItem = event => {
const { items, value } = this.state;
this.setState({
items: [...items, { text: value, checked: false }],
value: ""
});
event.preventDefault();
};
And this.setState({ items: items });
can get changed to this.setState({ items });
when the properties have the same name.
And I would add a check for an empty input field.
1 Like