Step 4 - Convert Todos component - #8
Open
danieltott wants to merge 22 commits into
Open
Conversation
add reset and current check
add reset and current check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
To integrate
useLoadinto<Todos>, we needed to add some features touseLoad. Aresetfunction allows us to reset the state back to its initial state. We also needed a way to prevent out-of-orderdispatches- if you switched theuserprop while the fetch was still loading, you would see a flash of the initial user's data and then the correct data.To prevent this, we can use
useRef, which gives us a mutable variable that we can leverage to keep track of whether we should throw away an update. TheuseRefvalue persists across renders, and will always have the current value regardless of which render is being run. It is very similar to a class property. Since ourloadfunction creates a closure, we can set a local variable to the useRef current value, and then compare them after running our async function.The integration into
<Todos>is similar to the integration into<App>. One difference here is that since our API function uses props as parameters (Api.fetchTodosByUser(user.id, showCompleted)), we need to wrap it inuseCallbackso that it isn't being re-created every render.To get management of our interval that calls
loadevery few seconds, we can make a pretty simpleuseEffect: