【发布时间】:2022-07-08 21:54:10
【问题描述】:
useParam 返回字符串 |未定义,但该函数需要一个字符串 |突变选择。
我有错误An argument of type "string | undefined" cannot be assigned to a parameter of type "string | MutationSelection". The type "undefined" cannot be assigned to the type "string | MutationSelection".ts(2345)。
这是我的代码
const PinDetail: React.FC<PropsType> = ({ user }) => {
const { pinId } = useParams()
const addComment = () => {
client.patch(pinId) //here error
.setIfMissing({ comments: [] })
.insert('after', 'comments[-1]', [{
comment,
_key: uuidv4(),
postedBy: {
_type: 'postedBy',
_ref: user._id
}
}])
.commit()
.then(() => {
fetchPinDetail(), // and here
setComment('')
setAddingComment(false)
})
}
}
【问题讨论】:
-
client可能在此处未定义。试试client?.patch(pinId)
标签: reactjs typescript react-hooks react-router-dom