【发布时间】:2019-03-17 12:32:30
【问题描述】:
我的后端处理 AddLike 突变。这需要一个 UserId 和一个 CommentId 或 PitchId。看你喜欢哪个。我想在整个应用程序中重用我的 LikeButton 组件这样我需要在我的 Apollo React Mutation 中定义我的变量以获取 CommentId 或 PitchID。现在我似乎无法做任何事情。有什么建议? :-)
这是组件的代码。 API GrahQL 有效。所以没有什么可以改变的。
import * as React from 'react';
import { Mutation } from 'react-apollo';
import { ADD_LIKE } from '../../GraphQLStatements/GraphQLStatements';
interface ILikeButtonProps {
currentUser : number;
type : any;
index: number;
}
const LikeButton: React.SFC<ILikeButtonProps> = (props) => {
let inputType : string = "pitchId"
const DefineType = () => {
if (props.type === "comment") {
inputType = "commentId"
}
else {
inputType = "pitchId"
}
}
return (
<Mutation mutation={ADD_LIKE}>
{(addLike) => (
<i className="far fa-thumbs-up icon like__button"
onClick={e => {
e.preventDefault();
e.stopPropagation();
DefineType()
addLike({ variables : { like :
{ "userId": props.currentUser,
inputType : props.index}
}
}).then ((res : any) => {
console.log(res.data.addLike.statusMessage)
});
}}>
</i>
)}
</Mutation>
);
};
export default LikeButton;
现在这不起作用。提前致谢!
【问题讨论】:
标签: reactjs graphql apollo react-apollo graphql-js