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..
axios.defaults.baseURL is "http://localhost:4001";
Is there something wrong with the onEdit
function?