This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useQuery } from 'react-query'; | |
export const useFetchFirstTodo = () => { | |
const { data, error, isLoading } = useQuery("fetchFirstTodo", fetchFirstTodo); | |
// -> dispatch and action | |
// -> manipulate the response before sending back to component | |
// -> call another api | |
// -> whatever makes sense for you to put here... | |
return { data, error, isLoading } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useQuery } from 'react-query'; | |
const fetchFirstTodo = async () => { | |
try { | |
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1"); | |
const json = await res.json(); | |
return json; | |
} catch (err) { | |
throw err; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const useFetch = (service) => { | |
const [loading, setLoading] = useState(true); | |
const [error, setError] = useState(); | |
const [data, setData] = useState(); | |
const fetchAPI = useCallback(async () => { | |
try { | |
const res = await fetch(service); | |
const json = await res.json(); | |
setData(json); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Todo = () => { | |
const [loading, setLoading] = useState(true); | |
const [error, setError] = useState(); | |
const [data, setData] = useState(); | |
const fetchTodo = useCallback(async () => { | |
try { | |
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1"); | |
const json = await res.json(); | |
setData(json); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"gelaterie": { | |
"icon": "🍦", | |
"data": [ | |
{ | |
"name": "Gelateria Fior Fiore", | |
"tel": [ | |
"051 760522" | |
] | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function areEqual(){ | |
// your logic here | |
} | |
React.memo(MyWelcomeMessage, areEqual) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shouldComponentUpdate(nextProps, nextState){ | |
// your logic here | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, Fragment } from "react"; | |
const WelcomeMessage = React.memo(({ name }) => { | |
console.log("render WelcomeMessage"); | |
return <label>Hello {name}</label>; | |
}); | |
const Page = () => { | |
const [user, setUser] = useState({}); | |
console.log("render Page"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Fragment } from "react"; | |
class WelcomeMessage extends React.Component { | |
render() { | |
console.log("render WelcomeMessage"); | |
return <label>Hello {this.props.user}</label>; | |
} | |
} | |
class Page extends React.PureComponent { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Fragment } from "react"; | |
class WelcomeMessage extends React.Component { | |
render() { | |
console.log("render WelcomeMessage"); | |
return <label>Hello {this.props.user}</label>; | |
} | |
} | |
class Page extends React.Component { |
NewerOlder