Nick Wild
Nick Wild

Reputation: 575

Problem with Hooks: useEffect, useMutation and useState working together

I have a function that is using react-table as a datagrid. It is being initially populated from Apollo in a parent component via local state with each line in the grid an object in an array.

When changes occur in a cell in the grid the whole line object is written to state.

I am trying to use useEffect to trigger a mutation that writes these changes in state back to the database, but I am struggling with two main things:

The Main Function (part of)

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;

      })
    );
  };

and the mutation...

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
  }
}
`

I'd be really grateful for some advice. Thanks

Upvotes: 0

Views: 8353

Answers (1)

Will Jenkins
Will Jenkins

Reputation: 9787

I don't think you need to use useEffect, you can trigger the mutation in your update:

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)))
  }
}

If you did want to use useEffect, you could e.g. keep the last changed line in a state variable and then use that to trigger the update:

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)))
  }
}

Upvotes: 5

Related Questions