我正在尝试使用useMutation
来自 s 的钩子react-apollo-hook
来执行删除突变,但是我很难将帖子的 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>
)
})
}
我不能包含属性useMutation
内部,onClick()
因为钩子不能用作回调函数。我尝试使用const inputRef = useRef()
和传递inputRef.current.value
,但一直不确定。