[React] Fetch data in React (2)๐ฆ
์์์๋ javascript์ promises ์ then๊ณผ catch ๋ธ๋ก์ ์ฌ์ฉํ์.
ํ์ง๋ง, then() ๋์ ์ async/await statement๋ฅผ react์์ data๋ฅผ fetchํ ๋ ์ฌ์ฉํ ์ ์๋ค.
import React, { Component } from 'react';
import axios from 'axios';
const API = 'https://hn.algolia.com/api/v1/search?query=';
const DEFAULT_QUERY = 'redux';
class App extends Component {
...
async componentDidMount() {
this.setState({ isLoading: true });
try {
const result = await axios.get(API + DEFAULT_QUERY);
this.setState({
hits: result.data.hits,
isLoading: false
});
} catch (error) {
this.setState({
error,
isLoading: false
});
}
}
...
}
export default App;
async (๋น๋๊ธฐ)
await ๋ฌธ์ ๋ฌด์ธ๊ฐ๊ฐ ๋น๋๊ธฐ ์ ์ผ๋ก ์คํ๋ ๋๋ง๋ค ๋น๋๊ธฐ ํจ์ ๋ด์์ ์ฌ์ฉ๋ฉ๋๋ค. ๋ฐ๋ผ์ ๋๊ธฐ ํ ์์ฒญ์ด ํด๊ฒฐ๋๊ธฐ ์ ์ ๋ค์ ์ค์ด ์คํ๋์ง ์์ต๋๋ค. ๋ํ ์์ฒญ์ด ์คํจ ํ ๊ฒฝ์ฐ try ๋ฐ catch ๋ธ๋ก์ ์ฌ์ฉํ์ฌ ์ค๋ฅ๋ฅผ ํฌ์ฐฉ ํ ์ ์์ต๋๋ค.
๋ฐ๋ณต๋๋ fetch ์์ ์ ์ค์ด์.(์ปดํฌ๋ํธ๊ฐ ๋ง์์ง์๋ก ๋ง์์ง)
component๊ฐ mount๋ ๋, data๋ฅผ fetchํ๊ณ loading์ค, ์ด๋ error ๋ฅผ ํ์ํ๋ ๊ฑฐ๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์ถ์.
2๊ฐ์ง ์ฑ ์์ผ๋ก componet๊ฐ ์๋ผ์ง ์ ์๋๋ฐ,
showing the fetched data with conditional renderings and fetching the remote data with storing it in local state afterward. Whereas the former is only there for rendering purposes, the latter could be made reusable by a higher-order component.
> ๋ฆฌํฉํ ๋งํ ๋ !
https://www.robinwieruch.de/react-higher-order-components/
const API = 'https://hn.algolia.com/api/v1/search?query=';
const DEFAULT_QUERY = 'redux';
...
const RenderPropApproach = () =>
<Fetcher url={API + DEFAULT_QUERY}>
{({ data, isLoading, error }) => {
if (!data) {
return <p>No data yet ...</p>;
}
if (error) {
return <p>{error.message}</p>;
}
if (isLoading) {
return <p>Loading ...</p>;
}
return (
<ul>
{data.hits.map(hit =>
<li key={hit.objectID}>
<a href={hit.url}>{hit.title}</a>
</li>
)}
</ul>
);
}}
</Fetcher>
https://www.robinwieruch.de/react-hooks-fetch-data
import React, { useState, useEffect } from 'react';
import axios from 'axios';
function App() {
const [data, setData] = useState({ hits: [] });
useEffect(() => {
const fetchData = async () => {
const result = await axios(
'https://hn.algolia.com/api/v1/search?query=redux',
);
setData(result.data);
};
fetchData();
}, []); //๋ ๋๋ง ์ต์ด์๋ง
return (
<ul>
{data.hits.map(item => (
<li key={item.objectID}>
<a href={item.url}>{item.title}</a>
</li>
))}
</ul>
);
}
export default App;
- query๊ฐ ๋ฐ๋ ๋๋ง, ๋ค์ fetch
default๋ก query๊ฐ ์ ์ฅ.
loading์ค, error์ฒ๋ฆฌ
import React, { Fragment, useState, useEffect } from 'react';
import axios from 'axios';
function App() {
const [data, setData] = useState({ hits: [] });
const [query, setQuery] = useState('redux');
const [url, setUrl] = useState(
'https://hn.algolia.com/api/v1/search?query=redux',
);
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
useEffect(() => {
const fetchData = async () => {
setIsError(false);
setIsLoading(true);
try {
const result = await axios(url);
setData(result.data);
} catch (error) {
setIsError(true);
}
setIsLoading(false);
};
fetchData();
}, [url]);
return (
<Fragment>
<input
type="text"
value={query}
onChange={event => setQuery(event.target.value)}
/>
<button
type="button"
onClick={() =>
setUrl(`http://hn.algolia.com/api/v1/search?query=${query}`)
}
>
Search
</button>
{isError && <div>Something went wrong ...</div>}
{isLoading ? (
<div>Loading ...</div>
) : (
<ul>
{data.hits.map(item => (
<li key={item.objectID}>
<a href={item.url}>{item.title}</a>
</li>
))}
</ul>
)}
</Fragment>
);
}
export default App;
custom data fetching in hook ๋ถํฐ~ ์์!
| [React] Virtual-DOM, Reconciliation, fiber (0) | 2021.06.21 |
|---|---|
| [MongoDB] Model Relationships Between Documents (3) (0) | 2021.06.10 |
| [MongoDB] Model Relationships Between Documents (2) (0) | 2021.06.10 |
| [MongoDB] Model Relationships Between Documents (1) (0) | 2021.06.10 |
| [React] Fetch data in React (1) (0) | 2021.05.27 |