DP's CODE NOTES

React Custom Hook

2019-02-27

React Hooks is a cool feauture that lets you use state and other React features without writing a class. And can also be used for reusing logic between components. Lets see how we can recreate some of the functionality we did in an earlier post A React HOC for resolving promises by using a React Hook

The component rendering a quote:

The container that uses our Hook:

The Hook code:

If we want to abstract rendering logic for loading state and errors we could put that logic inside the hook, also lets have the fetch logic inside the hook as well. The Hook would then look like this:

The calling code would look like this: