[英]Error: Cannot convert undefined or null to object
I make request to the server using fetch
and get response - category list.我使用
fetch
向服务器发出请求并获取响应 - 类别列表。 This list I display in view- table.这个列表我显示在视图表中。 Near each category on the right I have button Delete .
在右侧的每个类别附近,我都有按钮Delete 。 When I click this button appears modal window with two buttons
Delete
and Cancel
.当我单击此按钮时,出现模态 window ,带有两个按钮
Delete
和Cancel
。 I used API method DELETE
that delete some category.我使用 API 方法
DELETE
删除某些类别。
When I click button Delete
in modal window当我单击模态 window 中的按钮
Delete
时
But in my page appears error:但是在我的页面中出现错误:
Why is this happening?为什么会这样? And how to fix it?
以及如何解决?
deleteCategory
deleteCategory
const Home = () => {
const [value, setValue] = useState({
/.....
listCategory: [],
numberIdDelete: "",
isOpenedDelete: false
});
const deleteCategory = (argDeleteCategory) => { // in this method id some category set as value in field - numberIdDelete
setValue({ // that is, I'll know on which category the user has clicked delete button
...value,
numberIdDelete: argDeleteCategory, // this id I put in path in component DeleteCategory
isOpenedDelete: true
});
};
const cancel = () => { // close modal window
setValue({
...value,
isOpenedDelete: false
});
};
return (
<div>
<Table dataAttribute={value.listCategory}
deleteCategory={deleteCategory}/> // in component Table located button Delete
{value.isOpenedDelete && <DeleteCategory value={value.numberIdDelete} cancel={cancel} />}
</div>
);
};
const DeleteCategory = (props) => {
const {handleSubmit} = useFormik({
onSubmit: async () => {
const response = await fetch(`pathToServer/${props.value}`, { // put value from state (id some category)
method:'DELETE',
headers: {/.....}
});
const data = await response.json();
}});
return (
<div >
<form onSubmit={handleSubmit}>
<button type="submit" onClick={() => props.delCategory}>Delete</button>
<button type="submit" onClick={() => props.cancel}>Cancel</button>
</form>
</div>
);
};
export default (props) => (
<table>
<thead>
<tr>
<th>ID</th>
<th>TITLE</th>
</tr>
</thead>
<tbody>
{props.dataAttribute.map(item => (
<tr key={item.id}>
<td>{item.id} </td>
<td>{item.title} </td>
<td><button onClick={() => props.deleteCategory(item.id)}>Delete</button></td> // Button Delete
</tr>
))}
</tbody>
</table>
);
The problem is in the way you are calling useFormik
.问题在于您调用
useFormik
的方式。 The way you are configuring it, there are no input fields associated with it.您配置它的方式,没有与之关联的输入字段。
This causes that when you click the delete button a submit is performed and useFormik
tries to build an object with the input values to pass it to onSubmit
prop.这导致当您单击删除按钮时执行提交,并且
useFormik
尝试使用输入值构建一个 object 以将其传递给onSubmit
属性。 But there are no inputs.但是没有输入。
Examining your DeleteCategory
component code, you don't need to use useFormik
.检查您的
DeleteCategory
组件代码,您不需要使用useFormik
。 You are only showing a confirmation modal after all.毕竟,您只是显示确认模式。 Instead, you could do something like:
相反,您可以执行以下操作:
const DeleteCategory = (props) => {
const delCategory = async () => {
const response = await fetch(`pathToServer/${props.value}`, { // put value from state (id some category)
method:'DELETE',
headers: {/.....}
});
const data = await response.json();
// Do whatever when your request has been performed.
};
return (
<div >
<button type="button" onClick={() => delCategory()}>Delete</button>
<button type="button" onClick={props.cancel}>Cancel</button>
</div>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.