【问题标题】:React Apollo mutation varaiables value from props从道具中反应阿波罗突变变量值
【发布时间】:2020-09-21 20:43:50
【问题描述】:

我想通过 materialId 和 supplyId 更新我在 mongoDB 文档中的记录。 我从 url 路径获取了 supplyId 到突变。我使用“+”按钮渲染所有列出的材料。如何将道具传递给突变变量?我试过这是,但它很糟糕。

materialId: {material.id},

Scren from React Developers Tools

        <Query
      query={gql`
        {
          materials {
            id
            name
          }
        }
      `}
    >
      {({ loading, error, data }) => {
        if (loading) return <div>Fetching</div>;
        if (error) return <div>Error</div>;
        else
          return (
            <div>
              Materials to add:
              <ul>
                {data.materials.map(material => (
                  <li id={material.id} key={material.id}>
                    {material.name} |
                    <Mutation
                      mutation={SUPPLY_MUTATION}
                      propsId={material.id}
                      variables={{
                        materialId: "get material.id Here",
                        supplyId: this.props.match.params.id
                      }}
                    >
                      {supplyMutation => (
                        <button onClick={supplyMutation}>+</button>
                      )}
                    </Mutation>
                  </li>
                ))}
              </ul>
            </div>
          );
      }}
    </Query>

【问题讨论】:

  • materialId:material.id

标签: reactjs apollo


【解决方案1】:

你应该使用:

materialId: material.id

【讨论】:

    猜你喜欢
    • 2017-09-04
    • 2019-02-08
    • 2019-04-02
    • 2017-03-23
    • 2019-12-06
    • 2018-12-19
    • 2019-02-20
    • 2017-07-21
    • 2021-01-24
    相关资源
    最近更新 更多