λ³μ΄λ μλ²μ λ°μ΄ν°λ₯Ό μ λ°μ΄νΈνλλ‘ μλ²μ λ€νΈμν¬ νΈμΆμ μ€μ ν©λλ€.
ex) λΈλ‘κ·Έ ν¬μ€νΈλ₯Ό μΆκ°νκ±°λ, μμ λλ λ³κ²½ λ±
β useMutation
νΉμ§
- μΌλΆ μμΈλ₯Ό μ μΈνκ³ useQuery μ μλΉν μ μ¬ ν©λλ€.
- mutate ν¨μ λ₯Ό λ°ννλλ° μ΄ ν¨μλ, μ¬μ©μμ λ³κ²½ μ¬νμ ν λλ‘ μλ²λ₯Ό νΈμΆν λ μ¬μ© ν©λλ€.
- λ°μ΄ν°λ₯Ό μ μ₯νμ§ μμΌλ―λ‘ μΏΌλ¦¬ ν€λ νμνμ§ μμ΅λλ€.
- isLoading μ μ‘΄μ¬ νμ§λ§, isFetching μ μμ΅λλ€.
- λ³μ΄μ κ΄λ ¨λ μΊμλ μ‘΄μ¬νμ§ μκ³ , μ¬μλ λν κΈ°λ³Έκ° μ λλ€.
곡μ λ¬Έμ : Mutations
μ μ©
λλ―Έ API λ₯Ό μ¬μ©νλ―λ‘ μ€μ λ‘ λ³μ΄λ₯Ό μ μ© ν μλ μμ§λ§, λ³μ΄κ° μ§νλλ λμ μ΄λ€ μΌμ΄ μΌμ΄λλμ§ λν΄
μ¬μ©μμκ² μ΄λ₯Ό μλ €μ£Όλ λ°©λ²μ λν΄ μ΄ν΄λ΄
λλ€.
β μ§νν μ 체 μ½λ
import { useQuery } from 'react-query';
async function fetchComments(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/comments?postId=${postId}`
);
return response.json();
}
async function deletePost(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/postId/${postId}`,
{ method: "DELETE" }
);
return response.json();
}
async function updatePost(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/postId/${postId}`,
{ method: "PATCH", data: { title: "REACT QUERY FOREVER!!!!" } }
);
return response.json();
}
export function PostDetail({ post }) {
console.log(post)
// replace with useQuery
// fetchComments ν¨μ - μ΅λͺ
ν¨μ μ¬μ©νκΈ° (λκΈμ λͺ¨λ λμΌν κ²°κ³Όκ° λμ¬ κ² μ΄λ€) μμ§ μλ°°μ κΈ° λλ¬Έ
// postId λ post μμ±μμ μ»μ μ μλ€.
const {data, isLoading, isError} = useQuery(['comments', post.id], ()=>fetchComments(post.id));
if(isLoading){
return <h3>Loading ...</h3>
}
if(isError){
return <h3>Sorry, error</h3>
}
return (
<>
<h3 style={{ color: "blue" }}>{post.title}</h3>
<button>Delete</button> <button>Update title</button>
<p>{post.body}</p>
<h4>Comments</h4>
{data.map((comment) => (
<li key={comment.id}>
{comment.email}: {comment.body}
</li>
))}
</>
);
}
β useMutation μ¬μ©
import { useQuery, useMutation } from 'react-query';
// μμ μ΄λ¦μ μμ
const deleteMutation = useMutation((μΈμ) => λΉλκΈ°ν¨μ(μΈμ))
// λΉκ΅λ₯Ό μν useQuery
const {data, isLoading} = useQuery(['쿼리 ν€', ''], () => function());
useQuery λ λ€λ₯Έ μ°¨μ΄μ μ
- useQuery μ μΈμλ‘μ λ°λ‘ μ λ¬νλ 쿼리 ν¨μμλ λ¬λ¦¬ ( () => function() μ΄λΆλΆ )
- μΈμλ‘ μ λ¬νλ λ³μ΄ ν¨μμ κ²½μ° κ·Έ μ체λ μΈμλ‘ λ°μ μ μμ΅λλ€.
- useMutation μμ κ°μ²΄λ λ³μ΄ ν¨μλ₯Ό λ°ννκ² λ©λλ€. (deleteMutation)
async function deletePost(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/postId/${postId}`,
{ method: "DELETE" }
);
return response.json();
}
const deleteMuation = useMutation(() => deletePost(postId));
μμ deleteMutation μ λκ΅°κ° μμ λ²νΌμ ν΄λ¦ν λ λ³μ΄ ν¨μλ₯Ό μ€ννλ €λ λͺ©μ μ λλ€.
- μ¬μ©ν λ μμ± ν¨μμΈ mutate λ₯Ό μ€ννκ² λ©λλ€.
- props μμ λ°μ postId κ° λ¬΄μμ΄λ μκ΄μμ΄ μ€ννκ² λ©λλ€.
- λ°λΌμ λ³μ΄ ν¨μλ₯Ό νΈμΆν λλ©΄ μΈμκ° mutate() μμ ν λΉ λ©λλ€.
return (
<>
<h3 style={{ color: "blue" }}>{post.title}</h3>
<button onClick={() => deleteMutation.mutate(post.id)}>Delete</button>
<button>Update title</button>
<p>{post.body}</p>
<h4>Comments</h4>
{data.map((comment) => (
<li key={comment.id}>
{comment.email}: {comment.body}
</li>
))}
</>
);
β useMutation λν μ¬λ¬κ°μ§ λ°ν κ°μ²΄κ° μμ΅λλ€
isError, isLoading, isSuccess μ¬μ©
- isSuccess μ μ¬μ©νμ¬ (νμ¬ API λ°©μ λλ¬Έμ μ¬μ©) νμ΄μ§μμ λ°μ΄ν°λ₯Ό λ³κ²½ν¨μ ν΅ν΄ μ±κ³΅μ μΌλ‘ λμλμ§ μ μ μμ΅λλ€.
return (
<>
<h3 style={{ color: "blue" }}>{post.title}</h3>
<button onClick={() => deleteMuation.mutate(post.id)}>Delete</button>
<button>Update title</button>
{deleteMuation.isError && <p style={{color : 'red'}}>Error post</p>}
{deleteMuation.isLoading && <p style={{color : 'blue'}}>Deleting the post</p>}
{deleteMuation.isSuccess && <p style={{color : 'green'}}>Success</p>}
<p>{post.body}</p>
<h4>Comments</h4>
{data.map((comment) => (
<li key={comment.id}>
{comment.email}: {comment.body}
</li>
))}
</p>
);
β μ 체 μ½λ
import { useQuery, useMutation } from 'react-query';
async function fetchComments(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/comments?postId=${postId}`
);
return response.json();
}
async function deletePost(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/postId/${postId}`,
{ method: "DELETE" }
);
return response.json();
}
async function updatePost(postId) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/postId/${postId}`,
{ method: "PATCH", data: { title: "REACT QUERY FOREVER!!!!" } }
);
return response.json();
}
export function PostDetail({ post }) {
const {data, isLoading, isError} = useQuery(['comments', post.id], ()=>fetchComments(post.id));
const deleteMuation = useMutation(() => deletePost(post.id));
if(isLoading){
return <h3>Loading ...</h3>
}
if(isError){
return <h3>Sorry, error</h3>
}
return (
<>
<h3 style={{ color: "blue" }}>{post.title}</h3>
<button onClick={() => deleteMuation.mutate(post.id)}>Delete</button>
<button>Update title</button>
{deleteMuation.isError && <p style={{color : 'red'}}>Error post</p>}
{deleteMuation.isLoading && <p style={{color : 'blue'}}>Deleting the post</p>}
{deleteMuation.isSuccess && <p style={{color : 'green'}}>Success</p>}
<p>{post.body}</p>
<h4>Comments</h4>
{data.map((comment) => (
<li key={comment.id}>
{comment.email}: {comment.body}
</li>
))}
</>
);
}
β κ²°κ³Ό
- μ¬λ¬κ°μ§ λ°ν κ°μ²΄λ₯Ό ν΅ν΄ 쿼리μμ μ§νν κ²κ³Ό μ μ¬ν λ°©μμΌλ‘ μ£ΌκΈ°λ₯Ό μ²λ¦¬ ν μ μμ΅λλ€.
β μ°Έκ³
'π Front-End > React.js' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
useInfiniteQuery νΈμΆ (0) | 2022.10.26 |
---|---|
React Query - 무ν μ€ν¬λ‘€(Infinite Scroll) (0) | 2022.10.26 |
React Query - isFetching VS. isLoading (0) | 2022.10.25 |
React Query - λ°μ΄ν° ν리ν¨μΉ(Pre-fetching) (0) | 2022.10.25 |
React Query - νμ΄μ§ 맀κΉ(Pagination) (0) | 2022.10.25 |