Add a new object to a list (array) in React
Sat Jul 02 2022 06:18:12 GMT+0000 (Coordinated Universal Time)
Saved by
@jen_magpantay
#javascript
#react.js
const [inputValue, setInputValue] = useState('');
const [list, setList]= useState([]);
handleAddIntemOnList(){
// create a new item as object to be added to the list array
const newItem = {
name: inputValue,
addedAt: new Date().toLocaleTimeString("pt-br", {
hour: '2-digit',
minute: '2-digit'
})
}
// save into the list state
setList(prevState => [...prevState, newItem])
}
content_copyCOPY
Comments