Current Size:
Chakra BreakPoints
base0px
sm480px
md768px
lg992px
xl1280px
2xl1536px
Current Height:Width
widthpx
heightpx

Subjects

React18: Async Data Fetching Patterns

Date Created: 2023/09/12

Last Update: 2023/09/12

#async #react #cheatsheet #reference #notes
import { useCallback, useState, useEffect } from "react";
import "./styles.css";
export default function App() {
const [fooData, setFooData] = useState<null | any>(null);
const fetchData = useCallback(async () => {
const res = await fetch("https://jsonplaceholder.typicode.com/posts/1");
const data = await res.json();
console.log(data);
setFooData(data);
});
useEffect(() => {
fetchData();
}, []);
return (
<div className="App">
{fooData ? (
<div>
<div>{fooData.userId}</div>
<div>{fooData.title}</div>
<div>{fooData.body}</div>
</div>
) : (
<div></div>
)}
</div>
);
}

Living Pattern Validations

More Notes

All Notes
HomeProjects

Links

Home Articles Notes Projects About Style Guide Site Credits

Contact

 [email protected]

Location

🌎 Earth