IMY220 L7 AsyncReact
IMY220 L7 AsyncReact
React Docs:
https://legacy.reactjs.org/docs/react-comp
onent.html
Class Component Lifecycle Methods
https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
Class Component Lifecycle Methods
https://projects.wojtekmaj.pl/r
eact-lifecycle-methods-diagra
m/
https://legacy.reactjs.or
g/docs/react-componen
t.html
https://react.dev/learn/r
ender-and-commit
- useEffect()
- useEffect(callback, [dependency])
- Covers componentDidMount,
componentDidUpdate and componentWillUnmount
by using different variations of useEffect
- Used in combination with useState() and
setState() to manage state updates
- More Hooks, but these are most commonly used.
Whole list of
Lifecycle Hooks and
Explanations in the
React Docs:
https://react.dev/lea
rn/synchronizing-wit
h-effects
https://github.com/ProgrammingHero1/react-functional-component-lifecycle
Functional Component Lifecycle Hooks
Code Demo
fetch() API
https://developer.mozilla.org/en-US/docs/Web/A
PI/Fetch_API
To handle this, we can use the response returned from fetch to check status codes, etc.
- If we return a response object from fetch, e.g., const res = await fetch(...);
- res.ok
- Returns true if response status code is between 200-299
- False otherwise
- res.status
- Returns the status code (e.g., 404, 500, 200, etc.)
fetch() API: Handling Responses
Example:
Component State and Asynchrony
- Let’s put state updates and fetch() together the wrong way
- Code Demo
React Docs:
Component State https://react.dev/lear
n/render-and-commit
Components can have state
Why?
- If you accidentally ‘dirty’ (change) the component state during the ‘render’
phase, you cause unwanted side effects, which makes React throw an Error.
- More often than not this comes up in Functional Components.
- Since it’s pretty easy to tell when Class lifecycle methods are being called.
- useEffect() requires you understand a lot more about how state is updated.
- Functional components are also beginning to disallow asynchronous behaviour in
components using Hooks (in favour of Server-Side Rendering).
Duel-Wielding Asynchrony and Component State
So how do we avoid this?
Class Components:
Functional Components:
https://www.youtube.com/watch?v=RAJD4KpX8LA
https://www.youtube.com/watch?v=Huq9LWPXr34
https://www.youtube.com/watch?v=-yIsQPp31L0
https://legacy.reactjs.org/docs/react-component.html
https://react.dev/learn/render-and-commit
https://react.dev/learn/synchronizing-with-effects
https://react.dev/learn/lifecycle-of-reactive-effects
References
Fetch:
https://dev.to/dionarodrigues/fetch-api-is-new-old-version-of-ajax-1m14
https://dev.to/dionarodrigues/fetch-api-do-you-really-know-how-to-handle-errors-2gj0
https://www.geeksforgeeks.org/difference-between-ajax-and-fetch-api/
https://dev.to/dionarodrigues/fetch-api-is-new-old-version-of-ajax-1m14
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
References
Async:
https://www.youtube.com/watch?v=-yIsQPp31L0
https://www.youtube.com/watch?v=Huq9LWPXr34
https://www.youtube.com/watch?v=RAJD4KpX8LA
https://www.youtube.com/watch?v=zvM_FUVcB-0
Practise!