reactjs - 如何使用react-adopt动态设置mutation中的变量?

标签 reactjs graphql react-apollo

在我的组件中,我使用react-adopt来编写graphql查询和突变,这样我的渲染 Prop 就不会变得太困惑。我有以下代码:

这是我的突变,它需要一个参数 - planID。

const CREATE_ORDER_MUTATION = gql`
  mutation CREATE_ORDER_MUTATION($planID: String!) {
    createOrder(planID: $planID) {
      planID
      name
      description
      subscriptionType
      images
      subscriptionType
      pricePerMonth
}
}

这是采用函数,它需要几个突变,其中之一是 createOrder。 Apollo 的工作方式是我需要将变量 prop 传递给此处的 createOrder 组件。问题是,我目前没有 planID。 planID 仅在实际组件内部可用。

const Composed = adopt({
  toggleCart: <Mutation mutation={TOGGLE_CART_MUTATION} />,
  createOrder: <Mutation mutation={CREATE_ORDER_MUTATION} />,
});

我的组件看起来像这样。我这里有 planID,但我如何将它作为突变的参数传递?!

render() {
const { plan } = this.props;
return (
  <Composed>
    {({ toggleCart, createOrder }) => {
      const handleAdd = () => {
        toggleCart();
        Router.push('/waschingmachine');
        createOrder();
      };
      return (
        <StyledPlan>
          <h1>{plan.name}</h1>
          <p>{plan.description}</p>
          <img src={`static${plan.images[0]}`} alt="blue1" />
          <h2>{plan.pricePerMonth / 100} EUR</h2>
          <div className="buttons">
            <Link
              href={{
                pathname: '/plan',
                query: { id: plan.id },
              }}
            >
              <button type="button">info</button>
            </Link>
            <button onClick={handleAdd} type="button">
              Select Plan
            </button>
          </div>
        </StyledPlan>
      );
    }}
  </Composed>
);
}

如果无法通过这种方式解决问题,您会如何采取不同的处理方法?

最佳答案

以下是如何通过 react-adopt 方式将参数传递到内部 mutations 映射器中:

// In a nutshell, `react-adopt` allows you to pass props to your `Composed`
// component, so your inner `mapper` can access those props to pass them to 
// your <Query> or <Mutation>

// Here's your initial `Composed` component from above
const Composed = adopt({
  // `planId` is passed from below via props (see `<ContainerComponent />) 
  toggleCart: ({ planId, render }) => (
    <Mutation mutation={TOGGLE_CART_MUTATION} variables={{ planId }}>{render}</Mutation>,
    ),
  // `planId` is passed from below via props (see `<ContainerComponent />)
  createOrder: ({ planId, render })=> (
    <Mutation mutation={CREATE_ORDER_MUTATION} variables={{ planId }}>{render}</Mutation>                 
  )
});

// `<ContainerComponent />` will take a plan as its props and passed `planId` to 
// the `<Composed />` component
const ContainerComponent = ({ plan }) => (
  <Composed planId={plan.id}>
    {({ toggleCart, createOrder }) => {      
      const handleAdd = e => {
        e.preventDefault();
        toggleCart();
        // ...
        createOrder();
        // ...
      };

      // Whatever your UI needs you can pass them here via here
      return <YourPresentationComponent onClick={handleAdd} />;
    }}
  </Composed>
)

// Now all you need to do is to pass `plan` from your render() to the 
// <ContainerComponent /> (This is the render() where you return your 
render() {
  const { plan } = this.props;
  return <ContainerComponent plan={plan} />
}

希望这可以帮助解决您的问题!顺便说一句,您还可以获取先前的 mapper 值并将它们作为参数的一部分传递给下一个映射器 fn,请参见下文:

const Composed = adopt({ 
  // Here you can retrieve the actual `plan` by using `userId` and pass the result 
  // into your next children mapper fn to consume
  plan: ({ userId, render }) => (
   <Query query={GET_PLAN_GRQPHQL} variables={{ userId }}>{render}</Query>
  ),
  // `plan` is actually coming from above <Query /> component 
  toggleCart: ({ plan, render }) => { //... },
  createOrder: ({ plan, render }) => { //...}
});

关于reactjs - 如何使用react-adopt动态设置mutation中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53903546/

相关文章:

reactjs - Material UI - 自动完成样式

node.js - 如何动态生成cube.js的模式?

javascript - 为什么在 api 工作正常时使用片段上的 apollo 客户端查询会给我一个空对象?

graphql - 使用 Hasura 作为数据访问层的最佳和正确方法是什么

meteor - Apollo/GraphQl - 单个实体突变不更新缓存(数据库更新)

javascript - React onClick 函数在挂载时触发自身

reactjs - img 未显示在 reactstrap 轮播中

GraphQL:是否可以搜索嵌套字段?

reactjs - 如何使用 React-Hooks 和 Apollo Client 在 React-GraphQL 中实现搜索功能?

javascript - 如何解决无限 Javascript promise ?