r/reactjs • u/dance2die • Dec 01 '19
Beginner's Thread / Easy Questions (December 2019)
Previous threads can be found in the Wiki.
Got questions about React or anything else in its ecosystem? Stuck making progress on your app?
Ask away! Weβre a friendly bunch.
No question is too simple. π
π Want Help with your Code? π
Improve your chances by putting a minimal example to either JSFiddle, Code Sandbox or StackBlitz.
- Describe what you want it to do, and things you've tried. Don't just post big blocks of code!
- Formatting Code wiki shows how to format code in this thread.
- Pay it forward! Answer questions even if there is already an answer - multiple perspectives can be very helpful to beginners. Also there's no quicker way to learn than being wrong on the Internet.
New to React?
Check out the sub's sidebar!
π Here are great, free resources! π
- Create React App
- Read the official Getting Started page on the docs.
- Get started with Redux by /u/acemarke (Redux Maintainer).
- Kent Dodd's Egghead.io course
- Tyler McGinnis' 2018 Guide
- Codecademy's React courses
- Scrimba's React Course
- Robin Wieruch's Road to React
Any ideas/suggestions to improve this thread - feel free to comment here!
Finally, thank you to all who post questions and those who answer them. We're a growing community and helping each other only strengthens it!
View all comments
Show parent comment
1
u/dance2die Dec 28 '19 edited Dec 29 '19
I'd use a 3rd party library for async code w/ Redux, but to demo how it'd work, I created a sample Sandbox to demo how it can be done.
UPDATE:
Nice article using react-tracked.
https://www.reddit.com/r/reactjs/comments/egxfrq/how_to_handle_async_actions_for_global_state_with/
https://codesandbox.io/s/async-code-using-hooks-and-context-o30sc
Table of Contents.
1. Declare contexts (one's fine, but I like to separate state from actions).
2. Create a container for providers, and add state and actions to pass down to children.
3. Wrap the component to use those state & actions.
In this simple demo, I wrapped it around the
App.Now, the
App& child components underneath the tree will be able to access the context.4. Implementing async method,
fetchPostsin the provider.As there is no
middlewareas Redux does, you'd need to manually resolve the promise (await response.json()below) and dispatch the posts to the reducer.Check out the steps in the comments below.
5. Implementing reducer
If you've worked with Redux, the code below will look familiar.
Just a pure function with logic to set state and flags.
(Let me know if you aren't sure how the code below works.)
6. Using the context state & calling async method.
We've set up the context states and actions above.
Now we can use the state & call side-effect,
fetchPostsinuseEffecton component load.7. Additional Information
Normally one wouldn't declare a context in the same file. I'd personally expose state and actions via hooks following Kent C. Dodds's Context Pattern mentioned in How to use React Context effectively though.