Menu

Post image 1
Post image 2
Post image 3
Post image 4
Post image 5
1 / 5
0

How React Works (Part 4)? The Idea That Makes Suspense Possible

DEV Community·Sam Abaasi·about 1 month ago
#Xsy7Ubdl
Reading 0:00
15s threshold

The Idea That Makes Suspense Possible Series: How React Works Under the Hood Part 1: Motivation Behind React Fiber: Time Slicing & Suspense Part 2: Why React Had to Build Its Own Execution Engine Part 3: How React Finds What Actually Changed Prerequisites: Read Parts 1–3 first. A Question Before We Start Think about how you fetch data in a React component today. You probably do something like this: function UserProfile ({ id }) { const [ user , setUser ] = useState ( null ); const [ loading , setLoading ] = useState ( true ); useEffect (() => { fetchUser ( id ). then ( data => { setUser ( data ); setLoading ( false ); }); }, [ id ]); if ( loading ) return < Spinner />; return < div > { user . name } </ div >; } Enter fullscreen mode Exit fullscreen mode It works. But look at what you had to do: manage two pieces of state, write an effect, handle the loading condition manually, repeat this pattern in every component that fetches data.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More