DEV Community

Fetch data with React Hooks and Typescript

Camilo Mejía on March 26, 2019

Reusing logic in React has been complex, and patterns like HOCs and Render Props tried to solve that problem. With the recent addition of Hooks, re...
Collapse
 
tksilicon profile image
ThankGod Ukachukwu • Edited

"Inside that function, I'm using the Hook useState to manage the web service states. Note that I need to define the exact type of data that will be managed by the result state passing the generic Starships not Starship. I'm initializing the Hook with the interface ServiceInit of the union type Service, so the only property allowed is status with the string 'loading'."

You initialized with ServiceLoading not ServiceInit and the generic Starships not Starship. I am following your tutorial to solve a challenge so I am following the tutorial closely.

Collapse
 
vmandal profile image
Vikram

Nice work

Collapse
 
amsteffensen profile image
Andreas Steffensen

Thank you for this, this really saved me. Very well written, and link too a GitHub repo also makes this worth vile.

Collapse
 
dance2die profile image
Sung M. Kim

Thank you, Camilo.
It was a fun & educational article as you provided best practices as well.

Collapse
 
josemunoz profile image
José Muñoz

Beware of this pattern, once Suspense for data fetching is out (soon) this pattern will be obsolete.

Collapse
 
camilomejia profile image
Camilo Mejía

You're right, but if you need to start a project just now and can't wait until suspense for data fetching is out, this is still valid.