[React] Use the new React Context API

The React documentation has been warning us for a long time now that context shouldn't be used and that the API is unstable. Well, with the release of React 16.3, we're finally getting a stable context API and what's even better is that it has received a makeover and the dev experience is fantastic! In this lesson, we'll look at an example app that passes props down several levels deep into the component tree and replace all that prop drilling with the new context API. We'll see how to create a Provider and a Consumer, how to use them in the code and we'll take a look at how the default properties that get passed into createContext come into play.

 

A condition that we need 'context' API is to avoid pass Props all the way down to the component tree.

For example:

 

 

So porps are passed all the way down from 

  PageWrapper

          -- ProfileWrapper

      -- ProfileDetails

 

New context API looks like this:

1. Create an Context service:

复制代码
import { createContext } from "react";

const ProfileContext = createContext({
  firstName: "Sally",
  lastName: "Anderson"
});

export const ProfileProvider = ProfileContext.Provider;

export const ProfileConsumer = ProfileContext.Consumer;
复制代码

 'createContext' takes an default value. 

 

2. Wrap the top level component into Provider:

复制代码
import { ProfileProvider } from "./ProfileContext";

  render() {
    return (
      <ProfileProvider value={this.state.profile}>
        <PageWrapper />
      </ProfileProvider>
    );
  }
复制代码

 

If you want to just use default value, don't provide the Provider... this approach is a little bit strange.

  render() {
    return (
      <PageWrapper /> // may change in release
    );
  }

 

3. Remove all the props passed down.

4. In the component which do need context, use Consumer, Cunsumer takes a function as child.

复制代码
import React from "react";
import { ProfileConsumer } from "./ProfileContext";

export const ProfileDetails = props => (
  <ProfileConsumer>
    {context => (
      <div>
        <div>
          <strong>First name:</strong> {context.firstName}
        </div>
        <div>
          <strong>Last name:</strong> {context.lastName}
        </div>
      </div>
    )}
  </ProfileConsumer>
);
复制代码

 

posted @   Zhentiw  阅读(313)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-02-20 [Angular] @ViewChild and template #refs to get Element Ref
2017-02-20 [Angular] @ViewChildren and QueryLists (ngAfterViewInit)
2017-02-20 [Angular] Difference between Providers and ViewProviders
2016-02-20 [Protractor] Use protractor to catch errors in the console
2016-02-20 [Cycle.js] Hyperscript as our alternative to template languages
点击右上角即可分享
微信分享提示