Write a Custom React Effect Hook

Share this video with your friends

Send Tweet

Similar to writing a custom State Hook, we’ll write our own Effect Hook called useStarWarsQuote, which returns a random quote and a loading state.

Remember, hooks can only be used inside function components.

Michael
Michael
~ 5 years ago

Hi there! Closer to the end of this particular video, your example around 1:57, you have just extracted the useStarWarsQuote useEffect into it's own function, At this time you create a new useEffect inside of the functional component that then sets data again in useText.

Im curious why this is preferable, since the useStarWarsQuote already returns an object with quote and loading. I suspect it is just to illustrate how one would pass variables around to other state hooks. However this isn't clear from the example if this is a pattern that should be followed or not.

Joe Previte
Joe Previte(instructor)
~ 5 years ago

Hi Michael! Great question.

You don't necessary need to follow this pattern. As you suspect, the purpose of doing that was to show how one would pass variables around to other state hooks. I hope that helps and thanks for asking me to clarify (I'm sure others are wondering the same).