





























Learn about the massive improvements coming to function components in React via a fresh new course showing you how to refactor an existing app to these new and upcoming APIs.
Ok, before I get into things, can I just say that this course artwork by Maggie Appleton and Maxime Bourgeois is just the absolute best. It's just so good. 😍
I'm super excited to share this course with you. I've been using React full time for almost three years now and I've never been more excited (!!) about writing components than when I started playing around with Hooks and Suspense. Let's get a quick rundown of what you can expect from the course:
With the massive improvements to function components in React via hooks and suspense, you may be interested in seeing how to refactor a typical class component to a simpler class component that uses React Suspense and Hooks features. In this course, Kent will take a modern React codebase that uses classes and refactor the entire thing to use function components as much as possible. We'll look at state, side effects, async code, caching, and more!
Want a primer on hooks and suspense? Watch my React Hooks and Suspense Playlist!
note: React Hooks is alpha and subject to change. The React team has the 16.x roadmap here.
Let's get a quick overview of what this course is all about and how it's been structured to make sure you're as productive as possible with these new features.
We have a render prop based class component that allows us to make a GraphQL request with a given query string and variables and uses a GitHub graphql client that is in React context to make the request. Let's refactor this to a function component that uses the hooks useReducer, useContext, and useEffect.
The second argument to React's useEffect hook is an array of dependencies for
your useEffect callback. When any value in that array changes, the effect
callback is re-run. But the variables object we're passing to that array is
created during render, so our effect will be re-run every render even if the
shape of the object is the same. So let's solve this by doing our own equality
check from within the effect callback.
In the class version of this component, we had a method called safeSetState
which would check whether the component was still mounted before trying to call
setState. This is because our graphql client library is unable to cancel
in-flight requests. Let's make that same kind of thing work by tracking the
mounted state of our component using the useRef and useEffect hooks.
Because hooks code is regular JavaScript, extracting it to its own function is
trivial and enables code sharing in a really nice way. It also allows us to
encapsulate and separate concerns really cleanly. Custom hooks also compose
really nicely together to build more complex hooks out of more primitive ones.
Let's do this by creating a useSetState and useSafeSetStatecustom hook.
If you would like a more comprehensive useSetState hook, give
use-legacy-state a try.
Our hook to track the previous values looks pretty useful, so let's extract that
into it's own custom React Hook called usePrevious.
It would be nice if useEffect did the deep value comparison for us. Why don't
we make our own custom hook that does that for us? In this lesson we'll create a
useDeepCompareEffect which will allow us to use it just like a useEffectand
allow us to just pass the inputs.
We've got a pretty simple User class component that manages a bit of state and
uses some context. Let's refactor this over to a function component that uses
the useContext and useState hooks.
Our <Query /> component is a render prop based component that the <User />
component uses. But because it doesn't render anything, we can actually just
change it to a custom hook. Let's create a useQuery hook that returns the
state from the hooks the Query component uses and use that instead. But we'll
preserve the component so we don't have to refactor everywhere that uses the
Query render prop based component as well and we can keep our tests passing as
they are.
Let's refactor our GitHubClientProvider class component to a function
component that uses hooks. This one's pretty interesting because we can use
useEffect to encapsulate everything we need for a single effect, truly
separating that concern within our component.
With React 16.6.0, React Suspense was officially released as a stable feature
(with limited support for React.lazy). Let's refactor our lazily-loaded
components that are using
react-loadable to components
that use the built-in React.lazyfeature.
While users are filling out the form on our home page, it would be a good idea
to pre-load the next page they will be going to so they don't have to wait for
it to load once they've finished filling out the form. React's useEffect hook
makes this really easy.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。