从道具反应阿波罗突变变量值

时间:2020-09-21 12:28:14

标签: reactjs apollo

我想通过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>

1 个答案:

答案 0 :(得分:0)

您应该使用:

materialId: material.id