A closure is the combination of a function bundled together (enclosed) with
references to its surrounding state (the lexical environment).
In other words, a closure gives you access to an outer function's scope from an
inner function.
React Error Boundary
Error boundaries only catch errors that occur in a lifecycle method, render
method, and inside Hooks like useEffect.
Error boundaries are React components that catch JavaScript errors anywhere
in their child component tree, log those errors, and display a fallback UI instead
of the component tree that crashed.
Error boundaries catch errors during rendering, in lifecycle methods, and in
constructors of the whole tree below them.
A class component becomes an error boundary if it defines either (or both) of
the lifecycle methods static getDerivedStateFromError() or componentDidCatch().
Use static getDerivedStateFromError() to render a fallback UI after an error
has been thrown.
Use componentDidCatch() to log error information.
Pass data from child componenet to Parent componenet
While there is no direct way to pass data from the child to the parent
component, there are workarounds.
The most common one is to pass a handler function from the parent to the
child component that accepts an argument which is the data from the child
component.
React Query
React query is a react library that simplifies the way we fetch cache and
synchronize data from the server.
For example when a user leaves and then comes back to the same window
refetchOnWindowFocus:true handles that for you
If you implement infinite scroll feature you can use useInfiniteQuery hook
If you are writing data to the server you can make changes appear instantly
in the UI with optimistic updates
How to use React query
1. Intall react query library
2. Instantiate the query client and place it somewhere in your component tree
3. Now every child component can fetch data like a pro using react query
4. Now define a function in your component where you would like to fetch data
from server
5. The componenet that consumes that data and the useQuery hook it takes a
key that allows react query to manage this data efficiently.
6. Then the data fetching function as second argument it will make request
and return the data and status of the request