WhoBuilt

State Management & More

Jul 3, 2020· 26 min· 50K views·Watch on YouTube

Learn Next.js presents a Mastering Next.js module on Redux, React Context, TypeScript, and dynamic imports.

Choose Redux only when you need it

Redux provides a shared state store, but adding it can make an app more complex. Learn Next.js recommends checking whether component state or a top-level component can meet the need first. For Next.js apps that do use Redux, the module shows wrapping only the pages that need it so other pages can still be statically rendered.

Use React Context to share state

React Context lets components access shared values without passing them through every layer as props. Learn Next.js compares Redux’s developer tools, middleware, and performance options with Context and React’s useReducer hook. In a Day Drink example, a search context grows to hold the search term, selected day, location, and deal filters, which components read through a useSearch hook.

Add TypeScript to a Next.js app

Learn Next.js describes TypeScript as a way to gain confidence when refactoring, while noting that teams need to account for its learning curve. The setup shown starts with a tsconfig file and installing TypeScript and React types; Next.js then fills in the configuration and creates next-env.d.ts. The module also covers typing props and using Next.js types for pages, components, and API routes, plus generating types from a GraphQL schema.

Load code dynamically when needed

Dynamic imports can defer code until runtime, which can help keep it out of the initial page load. Learn Next.js also describes using one to load React Select without server-side rendering when the library caused styling problems. The feature is presented as useful for specific cases rather than a routine requirement.

Takeaways

  • Start with component state and add global state only when the app needs it.
  • In Next.js, apply Redux at the page level where needed to preserve static rendering elsewhere.
  • Use React Context and useReducer for shared state when their simpler setup fits the application.
  • Use TypeScript to make refactoring safer, and consider generating types from the GraphQL schema.
  • Use dynamic imports to defer code or work around a library that does not render correctly on the server.

Mentioned in this talk

Search inside this talk

What was claimed 8

  • feature
    Redux: Provides a predictable state container for JavaScript applications.
    “It's a predictable state container for JavaScript applications”
    YouTube
    Unverified1 source· Talk
  • feature
    Context Engine: Shares state across components without passing props through intermediate components.
    “Context allows you to do that without having to pass props.”
    YouTube
    Unverified1 source· Talk
  • feature
    Redux: Can provide developer tools for tracing state updates and time-travel debugging, middleware, and performance optimizations.
    “You can trace your state updates. You can get a bunch of pre-baked middleware”
    YouTube
    Unverified1 source· Talk
  • integrates with
    “The Apollo Client will be able to make those requests for you, cache that data”
    YouTube
    Verified2 sources· Talk
  • feature
    TypeScript: Compiles to JavaScript and adds type information that can increase confidence during refactoring.
    “It's a type superset of JavaScript that compiles down to JavaScript”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: Recognizes a tsconfig file and can populate it when the application runs.
    “Next.js will automatically populate this tsconfig file.”
    YouTube
    Unverified1 source· Talk
  • feature
    GraphQL: Its schema can be used to automatically generate TypeScript types.
    “automatically generate all your types based on your GraphQL schema”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: Supports dynamic imports, including disabling server-side rendering for an imported library.
    “I had to import it this way, turn off server-side rendering”
    YouTube
    Unverified1 source· Talk

Related talks