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的更多相关文章

  1. [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 ...

  2. [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’ ...

  3. React & update state with props & Object.assign

    React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...

  4. 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 ...

  5. React组件的state和props

    React组件的state和props React的数据是自顶向下单向流动的,即从父组件到子组件中,组件的数据存储在props和state中.实际上在任何应用中,数据都是必不可少的,我们需要直接的改变 ...

  6. 说说React组件的State

    说说React组件的State React的核心思想是组件化的思想,应用由组件搭建而成, 而组件中最重要的概念是State(状态). 正确定义State React把组件看成一个状态机.通过与用户的交 ...

  7. React组件的State

    React组件的State 1.正确定义State React把组件看成一个状态机.通过与用户的交互,实现不同状态,然后渲染UI,让用户界面和数据保持一致.组件的任何UI改变,都可以从State的变化 ...

  8. react native中state和ref的使用

    react native中state和ref的使用 因props是只读的,页面中需要交互的情况我们就需要用到state. 一.如何使用state 1:初始化state 第一种方式: construct ...

  9. react设置默认state和默认props

    1.默认状态设置 1.constructor (ES6) constructor(props) { this.state = { n: ... } } 2.getInitialState (ES5) ...

随机推荐

  1. Swift备忘录

    Swift 备忘录 2015-4 一.简介 1.Swift 语言由苹果公司在2010年7月开始设计,在 2014 年6月推出,在 2015 年 12 月 3 日开源 2.特点(官方): (1)苹果宣称 ...

  2. A. Jeff and Digits(cf)

    A. Jeff and Digits time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. 从0开始学习BFC

    为什么需要BFC? <style> .red { background: red; } .blue { background: #1890ff; } .green { background ...

  4. Django day24 cbv和APIView的源码分析 和 resful的规范

    一:cbv的源码分析 1.CBV和FBV的区别: - Class Base View   CBV(基于类的视图) - Function Base View   FBV(基于函数的视图) 2.as_vi ...

  5. Python可迭代序列排序总结

    列表排序 示例:lst = [12, 6, 1, 3, 10] 方法一:使用sort def list_sort(lst): lst.sort() # 就地排序,没有返回值 return lst 补充 ...

  6. 【NOI1999、LOJ#10019】生日蛋糕(搜索、最优化剪枝、可行性剪枝)

    主要是剪枝的问题,见代码,讲的很详细 #include<iostream> #include<cstdio> #include<cmath> #include< ...

  7. spirngMvc

    配置方式就略了 直接开始注解方式: 1.  新建项目 2.  导入jar包 3.  创建controller,用注解方式声明 4.  在web.xml配置核心分发器DispatcherServlet ...

  8. 【转】虚拟化(四):vsphere高可用功能前提-共享存储搭建

    vsphere高级功能HA.DRS.FT等,都需要有共享存储环境,即多台esxi主机同时连接一个共享存储,这样在新建虚拟机时,可以指定把虚拟磁盘保存在共享存储上,便于虚拟机在各个主机之间“飘移”. 常 ...

  9. count(*)实现原理+两阶段提交总结

    count(*)实现原理 不同引擎的实现: MyISAM引擎把表的总行数存在了磁盘上,执行COUNT(*)就会直接返回,效率很高: InnoDB在count(*)时,需要把数据一行一行的从引擎里面取出 ...

  10. PHP并发IO编程实践

    PHP相关扩展 Stream:PHP内核提供的socket封装 Sockets:对底层Socket API的封装 Libevent:对libevent库的封装 Event:基于Libevent更高级的 ...