将 Mutate() 从 ApolloClient 移动到 React 组件的正确方法?
Correct Way to Move Mutate() from ApolloClient to React Component?
根据 Apollo Docs,我想从 ApolloClient 获取 mutate()
函数到属于我的反应组件的道具中。这是 correct/preferred 的方法吗?
class myComponent extends React.Component {
constructor(props) {
super(props);
this.mutate = props.client.mutate();
};
}
如果你想使用 apollo 客户端动态调用一个 mutation,你可以像这样使用它:
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);
根据 Apollo Docs,我想从 ApolloClient 获取 mutate()
函数到属于我的反应组件的道具中。这是 correct/preferred 的方法吗?
class myComponent extends React.Component {
constructor(props) {
super(props);
this.mutate = props.client.mutate();
};
}
如果你想使用 apollo 客户端动态调用一个 mutation,你可以像这样使用它:
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);