0

enter image description here

export function Home() {
  const { data } = useSWR("/comments");
    const { data: editData, mutate } = useSWR("/comments", {
      revalidateOnFocus: false,
    });
    const onEdit = async () => {
      await axios.post("/comments", JSON.stringify(editData));
      mutate(editData);
    };
  return (
    <React.Fragment>
            {data?.map((row: any) => (
              <TableRow key={row.comment}>
                <TableCell>{row.comment}</TableCell>
                <TableCell align="right">
                  <Button
                    variant="contained"
                    color="secondary"
                    startIcon={<EditOutlined />}
                    onClick={onEdit}
                  >
                    Edit
                  </Button>
                </TableCell>
              </TableRow>
            ))}
    </React.Fragment>
  );
}
//db.json
{
  "comments": [
    {
      "comment": "hello World",
      "id": 5
    },
  ]
}

I'm working on edit comment using SWR and json-server,material ui

When I click edit button, not working properly..

enter image description here

axios.defaults.baseURL is "http://localhost:4001";

Is there something wrong with the onEdit function?

JeongHaSeung
  • 393
  • 6
  • 12
  • _"When I click edit button, not working properly"_ - Can you explain what were you expecting to happen when clicking on the "Edit" button? With your current code `editData` is the exact same as `data`, so nothing changes when you click that button. – juliomalves Oct 19 '22 at 20:34

0 Answers0