diff --git a/README.md b/README.md index 5c0615084..c8651f01d 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ import ReactDOM from 'react-dom'; * [`useClickInside`](#useclickinside) * [`useClickOutside`](#useclickoutside) +* [`useFetch`](#usefetch) * [`useInterval`](#useinterval) * [`useTimeout`](#usetimeout) @@ -380,6 +381,61 @@ ReactDOM.render(
[⬆ Back to top](#contents) +### useFetch + +A hook that implements `fetch` in a declarative manner. + +- Create a custom hook that takes a `url` and `options`. +- Use the `React.useState()` hook to initialize the `response` and `error` state variables. +- Use the `React.useEffect()` hook to anychronously call `fetch()` and update the state varaibles accordingly. +- Return an object containting the `response` and `error` state variables. + +```jsx +const useFetch = (url, options) => { + const [response, setResponse] = React.useState(null); + const [error, setError] = React.useState(null); + + React.useEffect(() => { + const fetchData = async () => { + try { + const res = await fetch(url, options); + const json = await res.json(); + setResponse(json); + } catch (error) { + setError(error); + } + }; + fetchData(); + }, []); + + return { response, error }; +}; +``` + +
+Examples + +```jsx +const ImageFetch = props => { + const res = useFetch('https://dog.ceo/api/breeds/image/random', {}); + if (!res.response) { + return
Loading...
; + } + const dogName = res.response.status; + const imageUrl = res.response.message; + return ( +
+ avatar +
+ ); +}; + +ReactDOM.render(, document.getElementById('root')); +``` +
+ +
[⬆ Back to top](#contents) + ### useInterval A hook that implements `setInterval` in a declarative manner. diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index cf1ea49cb..553ad2723 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -463,6 +463,23 @@ "hash": "e8f3e64cd0cf616dd42843328234e37b1b7a77ed51da8956204fdb02e088ce33" } }, + { + "id": "useFetch", + "type": "snippetListing", + "title": "useFetch", + "attributes": { + "text": "A hook that implements `fetch` in a declarative manner.\n\n- Create a custom hook that takes a `url` and `options`.\n- Use the `React.useState()` hook to initialize the `response` and `error` state variables.\n- Use the `React.useEffect()` hook to anychronously call `fetch()` and update the state varaibles accordingly.\n- Return an object containting the `response` and `error` state variables.\n\n", + "tags": [ + "hooks", + "effect", + "state", + "intermediate" + ] + }, + "meta": { + "hash": "848cb6ba4f8bd5dad012a38e6bd0e7c829a79b3215a23939c30a3f652627da4f" + } + }, { "id": "useInterval", "type": "snippetListing", diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 1c7f12e93..c939364f3 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -631,6 +631,29 @@ "hash": "e8f3e64cd0cf616dd42843328234e37b1b7a77ed51da8956204fdb02e088ce33" } }, + { + "id": "useFetch", + "title": "useFetch", + "type": "snippet", + "attributes": { + "fileName": "useFetch.md", + "text": "A hook that implements `fetch` in a declarative manner.\n\n- Create a custom hook that takes a `url` and `options`.\n- Use the `React.useState()` hook to initialize the `response` and `error` state variables.\n- Use the `React.useEffect()` hook to anychronously call `fetch()` and update the state varaibles accordingly.\n- Return an object containting the `response` and `error` state variables.\n\n", + "codeBlocks": { + "style": "", + "code": "const useFetch = (url, options) => {\r\n const [response, setResponse] = React.useState(null);\r\n const [error, setError] = React.useState(null);\r\n\r\n React.useEffect(() => {\r\n const fetchData = async () => {\r\n try {\r\n const res = await fetch(url, options);\r\n const json = await res.json();\r\n setResponse(json);\r\n } catch (error) {\r\n setError(error);\r\n }\r\n };\r\n fetchData();\r\n }, []);\r\n\r\n return { response, error };\r\n};", + "example": "const ImageFetch = props => {\r\n const res = useFetch('https://dog.ceo/api/breeds/image/random', {});\r\n if (!res.response) {\r\n return
Loading...
;\r\n }\r\n const dogName = res.response.status;\r\n const imageUrl = res.response.message;\r\n return (\r\n
\r\n \"avatar\"\r\n
\r\n );\r\n};\r\n\r\nReactDOM.render(, document.getElementById('root'));" + }, + "tags": [ + "hooks", + "effect", + "state", + "intermediate" + ] + }, + "meta": { + "hash": "848cb6ba4f8bd5dad012a38e6bd0e7c829a79b3215a23939c30a3f652627da4f" + } + }, { "id": "useInterval", "title": "useInterval", diff --git a/snippets/useFetch.md b/snippets/useFetch.md new file mode 100644 index 000000000..9d7379663 --- /dev/null +++ b/snippets/useFetch.md @@ -0,0 +1,51 @@ +--- +title: useFetch +tags: hooks,effect,state,intermediate +--- + +A hook that implements `fetch` in a declarative manner. + +- Create a custom hook that takes a `url` and `options`. +- Use the `React.useState()` hook to initialize the `response` and `error` state variables. +- Use the `React.useEffect()` hook to anychronously call `fetch()` and update the state varaibles accordingly. +- Return an object containting the `response` and `error` state variables. + +```jsx +const useFetch = (url, options) => { + const [response, setResponse] = React.useState(null); + const [error, setError] = React.useState(null); + + React.useEffect(() => { + const fetchData = async () => { + try { + const res = await fetch(url, options); + const json = await res.json(); + setResponse(json); + } catch (error) { + setError(error); + } + }; + fetchData(); + }, []); + + return { response, error }; +}; +``` + +```jsx +const ImageFetch = props => { + const res = useFetch('https://dog.ceo/api/breeds/image/random', {}); + if (!res.response) { + return
Loading...
; + } + const dogName = res.response.status; + const imageUrl = res.response.message; + return ( +
+ avatar +
+ ); +}; + +ReactDOM.render(, document.getElementById('root')); +```