React Hooks and Suspense
course
React Suspense has been released in React 16.6.0 (React.lazy support only) and React Hooks is stable in 16.8.0! Let's see how we can use these and more features of React together to write simpler React components.
Browse
Find courses, lessons, articles, talks, and podcasts.
238 resources
Showing 145–168 · Page 7 of 10
course
React Suspense has been released in React 16.6.0 (React.lazy support only) and React Hooks is stable in 16.8.0! Let's see how we can use these and more features of React together to write simpler React components.
course
The underpinnings of tools like Babel and ESLint, Abstract Syntax Trees (or ASTs) are not nearly as scary as you might think. This is a collection of all the egghead.io lessons I (Kent) have put out there.
lessonDuration 3m 17sFree
**NOTE:** The `react-cache` API is not stable and has changed. Check [this issue](https://github.com/facebook/react/issues/14780) for more details. Many components in your application will need data that has to be fetched over the network. When that data is loaded, however, you don't want to make a network request because you have the data stored in cache in the browser. React gives us an API to handle cache for us. We can use `createCache` and `createResource` to set data in the cache after fetching over the network.
lessonDuration 3m 28s
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.
lessonDuration 4m 7s
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 `useEffect` and allow us to just pass the inputs.
lessonDuration 2m 22s
Thanks to [`react-testing-library`](https://github.com/kentcdodds/react-testing-library) our tests are free of implementation details, so when we refactor components to hooks we generally don't need to make any changes to our tests. However, `useEffect` is slightly different from `componentDidMount` in that it's actually executed asynchronously _after_ the render has taken place. So all of our query tests which relied on the HTTP requests being sent immediately after render are failing. Let's use the `flushEffects` utility from `react-testing-library` to ensure that the pending effect callbacks are run before we make assertions.
lessonDuration 2m 1sFree
In this lesson, we’ll write a simple toggle component which will serve as the foundation for the rest of the course. Kent walks through how to create a simple toggle with React setState. He shows us how to extend the functionality of the toggle by safely adding an onToggle prop that gets called after state is updated. It is important to define default props when you are calling a method from props.
lessonDuration 4m 6sFree
Compound component gives more rendering control to the user. The functionality of the component stays intact while how it looks and the order of the children can be changed at will. We get this functionality by using the special `React.Children.map` function to map over the children given to our `<Toggle/>` component. We map over the children to pass the `on` state as a prop to its children. We move the visual pieces of the component out into function components and add them as static properties to `<Toggle/>`.
lessonDuration 4m 1sFree
Our current compound component implementation is great, but it's limited in that users cannot render the structure they need. Let's allow the user to have more flexibility by using React context to share the implicit state to our child `<Toggle/>` components. We will walk through safely defining `childContextTypes`, providing the value with `getChildContext`, and, on each of the components that need that context, we define `contextTypes` so that React will pass the context that is being asked for.
lessonDuration 3m 22sFree
The state shared between our toggle components is implicit because it lives inside context and is not easily accessible. In this lesson, we'll make a Higher Order Component (HOC) factory function which will allow the user to access that implicit state in their own components. Higher Order Components take a component and render a new component with some enhanced behaviors, in this case, the toggle context is added to the returned component. This factory component can get rid of duplicated code caused by needing to declare `contextTypes` on each component that needs it. Now we can wrap the component with `withToggle` and destructure `on` and `toggle` from the props.
lessonDuration 3m 47sFree
One problem Higher Order Components face is they share the props namespace with the other props the user wants to pass to that component. In this lesson, we’ll take a look at how we can resolve these naming conflicts.
lessonDuration 4m 43sFree
[React DevTools](https://chrome.google.com/webstore/detail/react-developer-tools/fmkadmapgofadopljbjfkapdkoienihi?hl=en) and React's built-in development warnings rely on the `displayName` and `name` property of your react components to give you a good developer experience. Unfortunately, Higher Order Components make it harder for React to give that good experience. In this lesson, we’ll learn about the `displayName` property and how that impacts React Developer Tools. Then we'll improve the `displayName` for our Higher Order Component by changing where we call the withToggle Higher Order Component.
lessonDuration 2m 54sFree
When you wrap a component with a Higher Order Component, it becomes impossible to forward the `ref` prop down the component tree due to the special nature of the `ref` prop in React. In this lesson, we’ll see that problem in action with our Higher Order Component. We will pass the ref as an `innerRef` prop so that the Higher Order Component can re-assign the ref to the component that it is wrapping.
lessonDuration 2m 45sFree
Let’s make it a little easier to test components which have been wrapped by our Higher Order Component by exposing the underlying component. Inside of our Higher Order Component, we will assign a `WrappedComponent` property to our Component that we passed in.
lessonDuration 3m 2sFree
One of the goals of reusable components is that the implementation details are unobservable. You want to make things work the way people think they should work without them having to consider the implementation. In this lesson we'll see a confusing observable behavior of using Higher Order Components and use the [`hoist-non-react-statics`](https://github.com/mridgway/hoist-non-react-statics) library to avoid the problem.
lessonDuration 5m 25sFree
In this lesson, we'll take a step back and reexamine the problem by doing a few tiny refactorings to arrive at a new pattern called `render props`. `render props` is just a function that you pass to a component that renders jsx. This pattern gives us the ability to use Reacts lifecycle methods where as with Higher Order Components, you could not. We'll see that it allows us to avoid using APIs like `context` and all of the issues we had to deal with when using Higher Order Components.
lessonDuration 2m 5sFree
Sometimes you have common use cases that require common props to be applied to certain elements. You can collect these props into an object for users to simply apply to their elements and we'll see how to do that in this lesson.
lessonDuration 6m 12sFree
When you're using prop collections, sometimes you can run into trouble with exposing implementation details of your prop collections. In this lesson, we'll see how we can abstract that away by simply creating a function called a prop getter that will compose the user's props with our prop collection.
lessonDuration 4m 6sFree
Sometimes users of your component want to have more control over what the internal state is. In this lesson we'll learn how to allow users to control some of our component’s state just like the `<input />`'s `value` prop.
lessonDuration 6m 47sFree
If you have state that needs to exist throughout your application, then you may find yourself passing props all over the application and even "drilling" the prop through components that don't really care about the prop at all. In this lesson, we'll see a sample of a small app that has the "prop drilling problem" and learn how to implement the "Provider pattern" to access context state anywhere in the component tree.
lessonDuration 1m 52sFree
When making a reusable component, you'll find that people often like to have the API they're most familiar with, so in this lesson we'll recreate the `withToggle` Higher Order Component using our new `ConnectedToggle` render prop component.
lessonDuration 3m 19sFree
One problem with using context is that any component in the hierarchy could return true from shouldComponentUpdate without knowing that state in context has changed. In this lesson, we’ll look at how the [`react-broadcast`](https://github.com/ReactTraining/react-broadcast) library can be used to avoid that problem.
lessonDuration 6m 49sFree
Now that we have everything wired up to share and update the state of the toggled boolean across our application, let's see how easy it would be to change from the state of a single boolean to state in a redux store! We’ll call it `rendex` (render props + redux). 😍
lessonDuration 1m 44sFree
Here's a little intro to the course material. I hope you enjoy the course! The goal of this course is to give you the concepts and ideas you need to make components as simple and flexible as possible so you and your team will have an easier time building an amazing UI. You will learn several patterns in this course (Comound and HoC components, render props, and the Provider pattern etc.) that are used broadly in the JavaScript (and specifically React) ecosystem. Some patterns are meant to be used in place of others while many compliment when used in tandem with each other. None of these patterns have caused performance issues of note so give each pattern a try and see how you like it. Links: - [React, Inline Functions, and Performance](https://cdb.reacttraining.com/react-inline-functions-and-performance-bdff784f5578)