[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) ...
随机推荐
- 没调出来 P2023
#include<iostream> #include<cstdio> #include<cstring> #define ll long long #define ...
- 2015 多校赛 第四场 1010 (hdu 5336)
Problem Description XYZ is playing an interesting game called "drops". It is played on a r ...
- nodejs 中使用 mysql 实现 crud
首先要使用 mysql 就必须要安装 npm install mysql 然后封装 sql 函数 const mySql = require('mysql'); let connection ; le ...
- python--1、入门
python的创始人为吉多·范罗苏姆(Guido van Rossum). python在2017年统计的所有语言排名中处于第四名,稳步上升状态. python应用领域: WEB开发(Django框架 ...
- jQuery学习笔记之DOM操作、事件绑定(2)
jQuery学习笔记之DOM操作.事件绑定(2) --------------------学习目录------------------------ 4.DOM操作 5.事件绑定 源码地址: https ...
- JAVA软件工程师应该具备哪些基本素质?
必知:软件企业要求基础软件工程师具备六大基本素质,即良好的编码能力.自觉的规范意识和团队精神.认识和运用数据库的能力.较强的英语阅读和写作能力.具有软件工程的概念和求知欲和进取心. 1.良好的编码能力 ...
- (转)Jetty实战之 安装 运行 部署
http://blog.csdn.net/kongxx/article/details/7218767 本文地址:http://blog.csdn.NET/kongxx/article/details ...
- vs2008 打开项目 无法读取项目文件
卸载vs2015之后 出现问题 C:\Windows\SysWOW64\regedit.exe 64系统运行这个 删除 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MS ...
- 团体程序设计天梯赛-练习集-L1-034. 点赞
L1-034. 点赞 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个“点赞”功能,你可以为你喜欢的博文点个赞表示支持 ...
- 自编码器----Autoencoder
一.自编码器:降维[无监督学习] PCA简介:[线性]原矩阵乘以过渡矩阵W得到新的矩阵,原矩阵和新矩阵是同样的东西,只是通过W换基. 自编码: 自动编码器是一种无监督的神经网络模型,它可以学习到输入数 ...