【发布时间】:2019-04-14 05:29:50
【问题描述】:
我对如何在 React 中使用 setState by hook 将数组数据添加到对象内的数组列表感到困惑。有人可以帮我解决这个问题吗? 下面是我的代码,每次我控制台日志它总是返回空数组。
import React, { useState } from 'react';
const Main = props => {
const [state, setState] = useState({
noteArray: [],
noteText: '',
});
const addNote = () => {
if(state.noteText){
var d = new Date();
let x = {
'date': d.getFullYear() +
"/" + (d.getMonth() + 1 ) +
"/" + d.getDate(),
'note': state.noteText
}
setState({...state, noteArray: x});
console.log(state.noteArray);
}
}
.......................the rest of code
}
【问题讨论】:
标签: arrays reactjs immutability react-hooks