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. warning:deprecated conversion from string constant to &#39;char *&#39;

    warning:deprecated conversion from string constant to 'char *' 解决方式 #include <iostream> using ...

  2. ASPX和Razor

    ASPX ASPX文件是微软的在server端运行的动态网页文件,通过IIS解析运行后能够得到动态页面,是微软推出的一种新的网络编程方法,而不是ASP的简单升级,由于它的编程方法和ASP有非常大的不同 ...

  3. CRM实施中应避免的5大问题

    越来越多的人认识到,杂乱的客户信息应该统一管理.曾经人们用excel表格甚至是纸笔来记录客户信息,可是假设想知道这个客户我们销售接触过几次?邮件里都谈了什么?在线客服都和客户聊了什么?报价单发的啥价格 ...

  4. Spark SQL Catalyst源代码分析之Analyzer

    /** Spark SQL源代码分析系列文章*/ 前面几篇文章解说了Spark SQL的核心运行流程和Spark SQL的Catalyst框架的Sql Parser是如何接受用户输入sql,经过解析生 ...

  5. 插入排序和冒泡排序(Swift版本)

    插入排序(只记录代码不作详细分析) 插入排序将输入的元素一个个插入到已排序的队列里,对比是由后往前 代码: func insertSort<Elem: Comparable>(_ arr: ...

  6. poj--3169--Layout(简单差分约束)

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9098   Accepted: 4347 Descriptio ...

  7. Docker容器查看ip地址

    第一步:进入centos7容器:yum install net-tools -y     我这里已经加载过,所以没有继续加载 第二步:加载完成之后可以输入 ifconfig查看ip地址

  8. 1. Two Sum[E]两数之和

    题目 Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  9. iris中间件

    最近使用golang写的时候涉及到权限校验,用中间件(使用iris框架内的东西) 自己摸索出一种自己的方式 iris.UseFunc(MiddlewareFunc)使用这个方法,会在所有的请求之前执行 ...

  10. tomcat映射路径(应用程序基本目录)的配置方法

    tomcat映射路径的配置方法 一.默认配置 位置:/conf 文件夹里的server.xml文件 <Host appBase="webapps"> appBase:可 ...