我正在做树结构。我需要添加数据并想要删除、编辑和搜索当我在该数据存储新变量之前将新数据推入数组时,我遇到了一个错误TypeError: treeData.push is not a function如何将该数据设置到新的变量中以及如何推送数组......添加.js 文件:import React, { useState} from 'react';import { TextField} from '@fluentui/react/lib/TextField';import { DefaultButton, PrimaryButton, Stack, IStackTokens} from '@fluentui/react';import './styles.css'const TextFieldBasicExample = (props) => { const [newItemValue, setNewItemValue] = useState({ title: '' }); console.log('onchange', newItemValue); const handleChange = (e) => { setNewItemValue({ [e.target.name]: e.target.value, }); } const _insert = (event) => { console.log('onclick', newItemValue); props.callback(newItemValue); // setNewItem({ // [event.target.name]:'' // }) } return ( < Stack horizontal > < Stack className = "add-inp" > < TextField label = "Add Item" name = "title" onChange = { handleChange } /> < span id = "error_name" > < /span> < PrimaryButton text = "Add" className = "add-btn" onClick = { _insert } /> < /Stack> < /Stack> );};export default TextFieldBasicExample
2 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
我假设您遇到此代码片段的问题:
if (test.title != '') {
var m = treeData.push(test)
// setTreeData(m);
}
所以你可以这样做:
if (test.title != '') {
setTreeData([...treeData, test]);
}
添加回答
举报
0/150
提交
取消