Skip to content

Commit afb73ab

Browse files
committed
Added sample react-query mutation
1 parent b46bb63 commit afb73ab

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/App.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ReactQueryDevtools } from 'react-query-devtools'
33
import { ReactQueryConfigProvider } from 'react-query'
44

55
import ReactTable from './components/ReactTable'
6+
import ReactMutation from './components/ReactMutation'
67

78
const queryConfig = {
89
shared: {
@@ -31,6 +32,7 @@ function App() {
3132
<div className = 'App'>
3233
<ReactTable />
3334
<ReactQueryDevtools />
35+
<ReactMutation />
3436
</div>
3537
</ReactQueryConfigProvider>
3638
)

src/components/ReactMutation.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react'
2+
import { useMutation } from 'react-query'
3+
import axios from 'axios'
4+
5+
const updateAsyncPost = async () => {
6+
const data = await axios('https://jsonplaceholder.typicode.com/posts', {
7+
body: JSON.stringify({
8+
title: 'foo',
9+
body: 'bar',
10+
userId: 1
11+
}),
12+
headers: {
13+
"Content-type": "application/json; charset=UTF-8"
14+
}
15+
})
16+
return data
17+
}
18+
19+
const ReactMutation = () => {
20+
21+
const [mutate, info] = useMutation(updateAsyncPost)
22+
23+
const updatePost = async () => {
24+
await mutate()
25+
}
26+
27+
// eslint-disable-next-line no-undef
28+
console.log({info})
29+
30+
return (
31+
<div onClick = { updatePost }>
32+
Click to create a Post - using JSON-PlaceHolder
33+
</div>
34+
)
35+
}
36+
37+
export default ReactMutation

0 commit comments

Comments
 (0)