[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) ...
随机推荐
- centos的vsftp修改上传下载速度
比如你想限制本地用户的上传速度和下载速度为1MB/s,则在vsftpd.conf中添加以下内容:local_max_rate = 1048576 ※ 默认单位是Byte/s
- 原生JS---6
原生js学习笔记6——事件 事件对象 鼠标事件 event.clientX在可视区中,鼠标点击的x坐标 event.clientY在可视区中,鼠标点击的y坐标 示例: <!DOCTYPE htm ...
- Spring Data 自动生成
之前一直用的mybatis逆向自动生成,由于最近学习springdata,所以看了一下springdata的自动生成,基本与mybatis一致,不同的也许就是逆向生成代码(实体类,mapper等)和正 ...
- C#中通过js实现个人用户和非个人用户的登陆
实现用户的登录功能,这里举一个个人和非个人用户的登录的例子 前台代码: <ul class="login_list clearfix"> <li> < ...
- Android HTTP下载文件并保存到本地或SD卡
想把文件保存到SD卡中,一定要知道SD卡的路径,获取SD卡路径: Environment.getExternalStorageDirectory() 另外,在保存之前要判断SD卡是否已经安装好,并且可 ...
- ★Java语法(七)——————————循环语句
package 自我总结; public class 循环语句 { public static void main(String[] args) { // while 循环 // 用法: // whi ...
- 【PostgreSQL-9.6.3】psql常用命令
命令 描述 \l 查看数据库 \c 换库 \d 查看所有表 \dt 只显示匹配的表 \di 只显示匹配的索引 \ds 只显示匹配的序列 \dv 只显示匹配的视图 \df 只显示匹配的函数 \d t1 ...
- SQL Server存储过程作业(一)
创建客房类型表RoomType 创建客房状态表RoomState 创建客房信息表Room 创建结账状态表ResideState 创建客人信息表GuestRecord 编写sql语句 USE maste ...
- 需要知道的TCP/IP三次握手
TCP/IP三次握手是TCP协议中比较重要的一个知识点,但是在很多博客中看到的三次握手的过程图很多都不是很正确.我在google找到了一篇写的非常不错的介绍TCP/IP技术文章期中就有三次握手的讲解, ...
- API接口测试用例编写规则(转载)
API接口测试用例编写规则 (1)必需参数覆盖.对于接口的参数,接口文档一般都会说明哪些儿是必需的,哪儿是非必需的.对于必需的参数,一定要测试传参数和不传参数接口是否报错? (2)必需的参数各种情况覆 ...