【问题标题】:Problem with Hooks: useEffect, useMutation and useState working togetherHooks 的问题:useEffect、useMutation 和 useState 一起工作
【发布时间】:2019-10-01 03:50:51
【问题描述】:

我有一个使用react-table 作为数据网格的函数。它最初是通过本地状态从父组件中的 Apollo 填充的,网格中的每一行都是数组中的对象。

当网格中的一个单元格发生变化时,整个行对象被写入状态。

我正在尝试使用 useEffect 触发将这些状态更改写回数据库的突变,但我正在努力解决两个主要问题:

  • 突变没有写回数据库(虽然突变在 graphql 操场上确实有效)
  • 了解如何仅将更改的行发送回突变。

主要功能(部分)

function Table2({ columns, data }) {
  const [lines, setLines] = useState(data);
  const [updateLine, {loading, error }] = useMutation(UPDATE_ITEM_MUTATION, {
  variables:{ ...lines}
  });

  useEffect(() => {
    updateLine
  },[lines]);

  const updateMyData = (rowIndex, columnID, value) => {
    setLines(getLines =>
      getLines.map((row, index) => {
        if (index === rowIndex) {
          console.log(row)
          return {
            ...lines[rowIndex],
            [columnID]: value
          };
        }
        return row;

      })
    );
  };

还有变异……

const UPDATE_ITEM_MUTATION = gql`
mutation UPDATE_LINE_MUTATION(
  $id: ID!, 
  $description: String, 
  $project:Int
  $category:Int
  $account:Int
  $amt:Int
  $units:String
  $multiple:Int
  $rate:Int
  ){
  updateLine(
    where:{id: $id},
    data: {
    description: $description
    project: $project
    category: $category
    account: $account
    amt: $amt
    units: $units
    multiple: $multiple
    rate: $rate
    }) {
    id
    description
    amt
  }
}
`

如果您能给我一些建议,我将不胜感激。 谢谢

【问题讨论】:

  • line 是什么样的?
  • {id: "ck0pnyjp7fbox0b1728oqqnr5", amt: 1, description: "test4b", category: 3, __typename: "Line"}

标签: reactjs apollo react-apollo apollo-client


【解决方案1】:

我认为你不需要使用useEffect,你可以在你的更新中触发突变:

function Table2 ({ columns, data }) {
  const [lines, setLines] = useState(data)
  const [updateLine, { loading, error }] = useMutation(UPDATE_ITEM_MUTATION)

  const updateMyData = (rowIndex, columnID, value) => {
    const updatedLine = { ...lines[rowIndex], [columnID]: value }
    updateLine({ variables: { ...updatedLine } })
    setLines(getLines => getLines.map((row, index) => (index === rowIndex ? updatedLine : row)))
  }
}

如果你确实想使用 useEffect,你可以例如将最后更改的行保留在状态变量中,然后使用它来触发更新:

function Table2 ({ columns, data }) {
  const [lines, setLines] = useState(data)
  const [updateLine, { loading, error }] = useMutation(UPDATE_ITEM_MUTATION)
  const [updatedLine, setUpdatedLine] = useEffect(null);
  useEffect(()=>{
     // call your mutation
  }, [updatedLine]);
  const updateMyData = (rowIndex, columnID, value) => {
    const updatedLine = { ...lines[rowIndex], [columnID]: value }
    setUpdatedLine(updatedLine);
    updateLine({ variables: { ...updatedLine } })
    setLines(getLines => getLines.map((row, index) => (index === rowIndex ? updatedLine : row)))
  }
}

【讨论】:

  • 非常感谢您,这将是完美的。我的 graphql 查询存在一些问题。我现在将修复以供将来参考
  • 刚刚注意到 const updatedLine = { ...lines[rowIndex], [columnID]: value } 行的一个奇怪的副作用 - values 是 Ints 被转换为 Strings。有什么想法吗?
  • 其实我修好了,应该是const updatedLine = { ...lines[rowIndex], [columnID]: +value ? +value :value }
猜你喜欢
  • 2020-03-23
  • 2021-03-30
  • 1970-01-01
  • 2021-05-17
  • 2021-06-29
  • 2020-01-25
  • 2021-09-06
  • 2020-06-22
  • 2021-10-22
相关资源
最近更新 更多