【发布时间】:2019-10-02 03:10:33
【问题描述】:
我正在尝试使用来自react-apollo-hooks 的useMutation 钩子来执行删除突变,但我在将帖子的 ID 值传递给以下代码中的突变钩子时遇到了困难:
const Posts = () => {
const { data, error, loading } = useQuery(GET_POST)
const onDeleteHandler = useMutation(DELETE_POST, {
variables: { id }
})
if (loading) return <div>...loading</div>
if (error) return <div>Error</div>
return data.posts.map(({id, title, body, location, published, author}) => {
return (
<div className="card" key={id}>
<p>id: {id}</p>
<p>title: {title}</p>
<p>body: {body}</p>
<p>location: {location}</p>
<p>published: {published}</p>
<p>author: {author.name}</p>
<Link to={`/post/${id}/edit`}>
Edit
</Link>
<button
onClick={onDeleteHandler}>
Delete
</button>
</div>
)
})
}
我不能在onClick() 属性中包含useMutation,因为挂钩不能用作回调函数。我尝试使用const inputRef = useRef() 并传递inputRef.current.value,但一直未定义。
【问题讨论】:
-
阅读docs
标签: reactjs graphql react-apollo react-hooks react-apollo-hooks