【发布时间】:2021-07-20 10:20:55
【问题描述】:
const [ post, setPost] = useState([])
function addMemo() {
let memo = [
{ id:1, name:'shyam', post:'developer' },
{ id:2, name:'ram', post:'porter' },
{ id:3, name:'prakash', post:'cobler' },
{ id:4, name:'sanj', post:'designer' },
{ id:5, name:'anil', post:'pilot' },
{ id:6, name:'gopal', post:'laptain' }
]
setPost( [...post, memo] )
}
return (
<button type="button" onClick={() => addMemo()}>Click Me</button>
)
这是我的代码,但我无法将对象数组推送到现有数组 将对象数组推送到现有数组,
【问题讨论】:
-
@william 的分析是正确的,但仅供参考,您应该参考您的 addMemo,而不是在每次调用时重新绑定它,例如 onClick={addMemo}
-
我也遇到过这种问题。我尝试了以下方法,但现在可以工作了。
标签: arrays reactjs react-hooks