์ฌ์ฉ์์
query์ ๋ค๋ฅด๊ฒ CUD์ ์ฌ์ฉ๋๋ค.
https://react-query.tanstack.com/guides/mutations
Mutations
Subscribe to our newsletter The latest TanStack news, articles, and resources, sent to your inbox.
react-query.tanstack.com
์ฌ์ฉ์์
const mutation = useMutation(newTodo => axios.post('/todos', newTodo))
return (
<div>
{mutation.isLoading ? (
'Adding todo...'
) : (
<>
{mutation.isError ? (
<div>An error occurred: {mutation.error.message}</div>
) : null}
{mutation.isSuccess ? <div>Todo added!</div> : null}
<button
onClick={() => {
mutation.mutate({ id: new Date(), title: 'Do Laundry' })
}}
>
Create Todo
</button>
</>
)}
</div>
)
์ํ๋ return data๋ useQuery์ ๊ฐ์
- mutation.mutate ํจ์๋ฅผ ์ด์ฉํด ๋ณ์๋ฅผ mutation์ ์ ๋ฌํ ์ ์๋ค.
// This will not work in React 16 and earlier
const CreateTodo = () => {
const mutation = useMutation(event => {
event.preventDefault()
return fetch('/api', new FormData(event.target))
})
return <form onSubmit={mutation.mutate}>...</form>
}
return๋ error๋ data๋ฅผ clear ํ ๋๋ mutation์ reset ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
๋ฐ์ํ
'์น (WEB) > ๊ณต๋ถ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
React-query :: api fetch ์ ์ฌ์ฉํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (0) | 2021.07.10 |
---|---|
React-query :: Query Invalidation (0) | 2021.07.10 |
React-query :: Query (0) | 2021.07.10 |
DeepLink๋? :: ์๋งํฌ, ๋ฅ๋งํฌ, ๋ํผ๋ ๋ฅ๋งํฌ (4) | 2021.07.10 |
JavaScript ๋์ ์๋ฆฌ :: ๋ฐํ์ ๊ตฌ์ฑ๊ณผ ๋น๋๊ธฐ ๋์ (feat. ๋น๋๊ธฐ ์ฝ๋ฐฑ) (0) | 2021.04.01 |
Comment