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>
);

[React] Use the new React Context API的更多相关文章

  1. React 16.3来了:带着全新的Context API

    文章概览 React在版本16.3-alpha里引入了新的Context API,社区一片期待之声.我们先通过简单的例子,看下新的Context API长啥样,然后再简单探讨下新的API的意义. 文中 ...

  2. [译]迁移到新的 React Context Api

    随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...

  3. React 新 Context API 在前端状态管理的实践

    本文转载至:今日头条技术博客 众所周知,React的单向数据流模式导致状态只能一级一级的由父组件传递到子组件,在大中型应用中较为繁琐不好管理,通常我们需要使用Redux来帮助我们进行管理,然而随着Re ...

  4. 简单整理React的Context API

    之前做项目时经常会遇到某个组件需要传递方法或者数据到其内部的某个子组件,中间跨越了甚至三四层组件,必须层层传递,一不小心哪层组件忘记传递下去了就不行.然而我们的项目其实并没有那么复杂,所以也没有使用r ...

  5. React Context API

    使用React 开发程序的时候,组件中的数据共享是通过数据提升,变成父组件中的属性,然后再把属性向下传递给子组件来实现的.但当程序越来越复杂,需要共享的数据也越来越多,最后可能就把共享数据直接提升到最 ...

  6. 10分钟学会React Context API

    Create-react-app来学习这个功能: 注意下面代码红色的即可,非常简单. 在小项目里Context API完全可以替换掉react-redux. 修改app.js import React ...

  7. React 全新的 Context API

    Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...

  8. 手写一个React-Redux,玩转React的Context API

    上一篇文章我们手写了一个Redux,但是单纯的Redux只是一个状态机,是没有UI呈现的,所以一般我们使用的时候都会配合一个UI库,比如在React中使用Redux就会用到React-Redux这个库 ...

  9. React Hooks & Context API

    React Hooks & Context API responsive website https://reactjs.org/docs/hooks-reference.html https ...

随机推荐

  1. HDU 2371

    知道了怎么置换之后,就可以用矩阵来置换了,但这道题一直关于置换的地方读不明白. #include <iostream> #include <cstdio> #include & ...

  2. [Angular] Getting to Know the @Attribute Decorator in Angular

    So when you using input binding in Angular, it will always check for update. If you want to improve ...

  3. Cocos2D-x设计模式发掘之中的一个:单例模式

    http://www.tuicool.com/articles/NBRn2murl=pVtZACoQFKXC3u3uGwMLnTy4YDWihcVg0ata5gy506pmPpQEc0PO9hm6wG ...

  4. insmod: error inserting &#39;hello.ko&#39;: -1 Invalid module format

    在学习编写linux驱动程序的时候,一般都是从写一个helloworld的模块開始. 可是在编译完毕后,进行模块载入的时候,有时会出现例如以下错误: insmod: error inserting ' ...

  5. C++中 pair 的使用方法

    #include<iostream> #include<string> #include<map> using namespace std; // pair简单讲就 ...

  6. Android之弹出菜单框【注冊上下文菜单】

    注冊上下文菜单:(长按弹出一个菜单) 第一种创建方法(与长按事件结合): public class MainActivity extends Activity { private TextView u ...

  7. WebBrowser网页操作之提取获取元素和标签(完整篇)

    最近使用WebBrower做了几个Hook小程序,收集积累如下: using System; using System.Collections.Generic; using System.Linq; ...

  8. 解决 Eclipse 导入项目后 Maven Dependencies missing jar 问题

    转自:https://yq.aliyun.com/ziliao/314086 话不多说直接上图 上图是我通过git导入项目后, Maven Dependencies Library中很多包出现miss ...

  9. ubuntu下谷歌浏览器字体模糊解决方案

    一般来说,这种问题应该是缺少字体造成的,因此重新安装字体就可以解决问题. 1.双系统下,查找Windows C盘下:Windows/font/???? ???可在3中查找相应字体 2.分别双击安装那两 ...

  10. POJ 3020 Hungary

    一道建图题-- // by SiriusRen #include <cstdio> #include <cstring> using namespace std; #defin ...