[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>
);
[React] Use the new React Context API的更多相关文章
- React 16.3来了:带着全新的Context API
文章概览 React在版本16.3-alpha里引入了新的Context API,社区一片期待之声.我们先通过简单的例子,看下新的Context API长啥样,然后再简单探讨下新的API的意义. 文中 ...
- [译]迁移到新的 React Context Api
随着 React 16.3.0 的发布,context api 也有了很大的更新.我已经从旧版的 api 更新到了新版.这里就分享一下我(作者)的心得体会. 回顾 下面是一个展示如何使用旧版 api ...
- React 新 Context API 在前端状态管理的实践
本文转载至:今日头条技术博客 众所周知,React的单向数据流模式导致状态只能一级一级的由父组件传递到子组件,在大中型应用中较为繁琐不好管理,通常我们需要使用Redux来帮助我们进行管理,然而随着Re ...
- 简单整理React的Context API
之前做项目时经常会遇到某个组件需要传递方法或者数据到其内部的某个子组件,中间跨越了甚至三四层组件,必须层层传递,一不小心哪层组件忘记传递下去了就不行.然而我们的项目其实并没有那么复杂,所以也没有使用r ...
- React Context API
使用React 开发程序的时候,组件中的数据共享是通过数据提升,变成父组件中的属性,然后再把属性向下传递给子组件来实现的.但当程序越来越复杂,需要共享的数据也越来越多,最后可能就把共享数据直接提升到最 ...
- 10分钟学会React Context API
Create-react-app来学习这个功能: 注意下面代码红色的即可,非常简单. 在小项目里Context API完全可以替换掉react-redux. 修改app.js import React ...
- React 全新的 Context API
Context API 可以说是 React 中最有趣的一个特性了.一方面很多流行的框架(例如react-redux.mobx-react.react-router等)都在使用它:另一方面官方文档中却 ...
- 手写一个React-Redux,玩转React的Context API
上一篇文章我们手写了一个Redux,但是单纯的Redux只是一个状态机,是没有UI呈现的,所以一般我们使用的时候都会配合一个UI库,比如在React中使用Redux就会用到React-Redux这个库 ...
- React Hooks & Context API
React Hooks & Context API responsive website https://reactjs.org/docs/hooks-reference.html https ...
随机推荐
- Linux下I/O复用 Select与Poll
Select #include <sys/time.h>#include <sys/types.h>#include <sys/unistd.h> int sele ...
- 自己定义View实现水平滚动控件
前几天项目中须要使用到一个水平可滚动的选择条,类似下图效果(图片是从简书上一位作者那儿找来的,本篇也是在这位作者的文章的基础上改动的,站在大神的肩膀上,哈哈,因为原文没有提供demo,并且实现的效果跟 ...
- struts2提交多个对象带图片
一:实体类 二:前台页面 三:Action处理
- 【Cocos2d-x】坐标系和图层
在Cocos2D-X中,存在四种坐标系: 1.OpenGL坐标系:该坐标系原点在屏幕左下角.x轴向右,y轴向上.这也就是cocos2dx中用到的坐标系所以没啥好说的. 2.屏幕坐标系(UIKit坐标) ...
- Android recovery UI实现分析
Android recovery模式为何物? 关于这个问题, baidu上已经有无数的答案.不理解的朋友先补习一下. 从纯技术角度来讲, recovery和android本质上是两个独立的rootfs ...
- WebService CXF学习:复杂对象传递(List,Map)
转自:https://blog.csdn.net/z69183787/article/details/35988335 第一步:创建存储复杂对象的类(因为WebServices的复杂对象的传递,一定要 ...
- bfs初学
BFS: ** 当知道初始和目标状态的,用双向BFS: 无权图最好用BFS 不用重复如队** 实现框架: 抄来的(来源:https://www.luogu.org/blog/stephen2333/s ...
- MyBatis数据持久化(十)与Spring4整合
前面几节介绍了mybatis的基本使用方法,本节主要介绍如何使用mybatis与主流的IoC容器Spring进行整合. 我们首先需要获取Spring框架的jar文件,在写本文时spring的最新Rel ...
- SQL学习——基础语句(3)
先上数据表 student表 grade 表 SQL Union 语句 合并两个或多个select查询结果集. select s_id from student union select s_id ...
- C++快速读取大文件
debug的时候需要等很长时间读模型,查资料发现了两种快速读取大文件的方法. test 1:每次读一个字符串 test 2.3一次读取整个文件 {//test 1 string buf; clock_ ...