根据Apollo Docs,我想mutate()
从 ApolloClient 获取函数到属于我的反应组件的道具中。这是正确/首选的方法吗?
class myComponent extends React.Component {
constructor(props) {
super(props);
this.mutate = props.client.mutate();
};
}
根据Apollo Docs,我想mutate()
从 ApolloClient 获取函数到属于我的反应组件的道具中。这是正确/首选的方法吗?
class myComponent extends React.Component {
constructor(props) {
super(props);
this.mutate = props.client.mutate();
};
}
如果您想使用 apollo 客户端动态调用突变,则可以像这样使用它:
import { withApollo } from 'react-apollo';
class myComponent extends React.Component {
constructor(props) {
super(props);
this.mutate = props.client.mutate;
}
onClick = () => {
this.mutate({
mutation: gql `${<define your graphql mutation>}`,
variables: { ... },
}).then(...);
}
...
}
export default withApollo(MyComponent);
否则我建议你静态定义你的突变,graphql
然后调用突变:
class MyComponent extends React.Component {
onClick = () => {
this.props.mutate({ variables: { ... } }).then(....);
}
...
}
const yourGraphqlMutation = gql `${<define your graphql mutation>}`;
export default graphql(yourGraphqlMutation)(MyComponent);