[React] Update Application State with React Apollo ApolloConsumer Component
In this lesson I refactor some code that utilizes the Mutation component to update client-side cache to use the new ApolloConsumer component baked into react-apollo 2.1.
Additional Resources: https://dev-blog.apollodata.com/introducing-react-apollo-2-1-c837cc23d926
If just working on local state, we can use ApolloConsumer:
import React from "react";
import { render } from "react-dom"; import ApolloClient, { gql } from "apollo-boost";
import {
ApolloProvider,
Query,
Mutation,
compose,
graphql,
ApolloConsumer
} from "react-apollo"; const defaults = {
items: ["Apple", "Orange"]
}; const GET_ITEMS = gql`
query {
items @client
}
`; const client = new ApolloClient({
clientState: {
defaults
}
}); const Items = () => (
<Query query={GET_ITEMS}>
{({ loading, error, data }) => {
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>; return data.items.map(item => <div key={item}>{item}</div>);
}}
</Query>
); const AddItem = ({ addItem }) => {
let input;
return (
<ApolloConsumer>
{cache => (
<div>
<form
onSubmit={e => {
e.preventDefault();
let { items } = cache.readQuery({ query: GET_ITEMS });
items = [...items, input.value];
cache.writeData({ data: { items } });
input.value = "";
input.focus();
}}
>
<input ref={node => (input = node)} />
<button type="submit">Add Item</button>
</form>
</div>
)}
</ApolloConsumer>
);
}; const App = () => (
<div>
<div>
<Items />
<AddItem />
</div>
</div>
); const ApolloApp = () => (
<ApolloProvider client={client}>
<App />
</ApolloProvider>
); render(<ApolloApp />, document.getElementById("root"));
[React] Update Application State with React Apollo ApolloConsumer Component的更多相关文章
- [React] Keep Application State in Sync with Browser History
Using pushState and passing route data via context allows our application to respond to route change ...
- [React] Use React Context to Manage Application State Through Routes
We’ll create a Router component that will wrap our application and manage all URL related state. We’ ...
- React & update state with props & Object.assign
React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...
- Wait… What Happens When my React Native Application Starts? — An In-depth Look Inside React Native
Discover how React Native functions internally, and what it does for you without you knowing it. Dis ...
- React组件的state和props
React组件的state和props React的数据是自顶向下单向流动的,即从父组件到子组件中,组件的数据存储在props和state中.实际上在任何应用中,数据都是必不可少的,我们需要直接的改变 ...
- 说说React组件的State
说说React组件的State React的核心思想是组件化的思想,应用由组件搭建而成, 而组件中最重要的概念是State(状态). 正确定义State React把组件看成一个状态机.通过与用户的交 ...
- React组件的State
React组件的State 1.正确定义State React把组件看成一个状态机.通过与用户的交互,实现不同状态,然后渲染UI,让用户界面和数据保持一致.组件的任何UI改变,都可以从State的变化 ...
- react native中state和ref的使用
react native中state和ref的使用 因props是只读的,页面中需要交互的情况我们就需要用到state. 一.如何使用state 1:初始化state 第一种方式: construct ...
- react设置默认state和默认props
1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...
随机推荐
- sublime的ctags安装
首先,是ctags的下载.在这里:http://pan.baidu.com/s/1gdAMFab 我们用sublime几乎都会首先安装这个插件,这个插件是管理插件的功能,先安装它,再安装其他插件就方便 ...
- JavaScript学习杂记
1.DOM层级:document(document) --> doctype,documentElement(html) --> head,body(body). 2.offset, cl ...
- Django day08 多表操作 (二) 添加表记录
一: 一对多 1. 一对多新增 两种方式: publish = 对象 publish_id = id 1. publish_id 和 publish 的区别就是: 1)publish_id 可 ...
- Java 网络IO编程(BIO、NIO、AIO)
本概念 BIO编程 传统的BIO编程 代码示例: public class Server { final static int PROT = 8765; public static void main ...
- 如何通过免费开源ERP Odoo实现企业数字化转型深度分析(一)
本文来自<开源智造企业数字化转型报告白皮书>的精选内容章节.请勿转载.欢迎您反馈阅读意见. 引言 在由消费者驱动的数字经济时代,创新之势锐不可挡.变革步伐从未如此迅速,并且还会越来越快.对 ...
- Ubuntu1804安装Postgresql【转】
转载自:https://huur.cn/course/yw/1591.html 关系数据库管理系统是许多网站和应用程序的关键组成部分.它们提供了一种结构化的方式来存储,组织和访问信息. Postgre ...
- Md2All,让公众号完美显示Latex数学公式
当公众号遇上Latex 大家都知到,公众号连代码块都不支持,更不要说功能强大的Latex公式了.那在Md2All之前,如果想在公众号上显示Latex公式应该怎么办呢? 最通常的做法就是在某个支持Lat ...
- 获取XML里指定的节点内容信息
HttpContent bw = new StringContent(StrXml, Encoding.UTF8, "application/Xml"); var Msg = aw ...
- 安卓JNI使用OpenCV
OpenCV也有Java数据结构的包,不过计算速度还是很慢,非不得已不使用此种方式调用OpenCV.使用NDK编写底层OpenCv的调用代码,使用JNI对代码进行封装,可以稍微提高一点效率. 参考链接 ...
- 【技术累积】【点】【git】【10】.gitignore和.gitattributes
.gitignore 告诉git忽略一些文件,git status会显示不到这些文件的状态. 一般放在项目根目录,以对全局控制,当然可以放在module下: 具体规则主要是: 以行为单位定义忽略文件类 ...