我想使用 react-query useMutation 将用户添加到数据库,但我在库中遇到此错误。请任何人都可以帮助我。这是我的代码。
类型错误:对象不是函数:
import { addStaff } from "./async";
function Navbar() {
const inputRef = useRef();
const [mutateAdd] = useMutation(addStaff, {
onSuccess: () => console.log("User successfully added"),
});
const onAdd = () => {
mutateAdd({ text: inputRef.current.value });
inputRef.current.value = "";
};
return (
<div style={{ display: "flex" }}>
<div></div>
<div style={{ display: "flex", marginLeft: 40 }}>
<Input placeholder="start typing..." ref={inputRef} />
<Button onClick={onAdd}>Invite Staff</Button>
</div>
</div>
);
}```
答案 0 :(得分:0)
从 v3 开始,useMutation
返回的不是数组而是对象。请参阅迁移指南:https://react-query.tanstack.com/guides/migrating-to-react-query-3#usemutation-now-returns-an-object-instead-of-an-array
所以你可能想要:
const { mutate } = useMutation(addStaff, {
onSuccess: () => console.log("User successfully added"),
});