Created
June 2, 2018 16:28
-
-
Save larkintuckerllc/d3ccb292d731f5d22c5acd96ac139695 to your computer and use it in GitHub Desktop.
GraphQL and Apollo Client by Example: Part 2 - 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import gql from 'graphql-tag'; | |
| import { PropTypes } from 'prop-types'; | |
| import React from 'react'; | |
| import { Query } from 'react-apollo'; | |
| const CounterView = ({ counter, onDecrement, onIncrement }) => ( | |
| <div> | |
| <div>{counter}</div> | |
| <button onClick={onDecrement}>-</button> | |
| <button onClick={onIncrement}>+</button> | |
| </div> | |
| ); | |
| CounterView.propTypes = { | |
| counter: PropTypes.number.isRequired, | |
| onDecrement: PropTypes.func.isRequired, | |
| onIncrement: PropTypes.func.isRequired, | |
| }; | |
| const GET_COUNTER = gql` | |
| { | |
| counter @client | |
| } | |
| `; | |
| const Counter = () => ( | |
| <Query query={GET_COUNTER}> | |
| {({ data, client }) => { | |
| const { counter } = data; | |
| const handleIncrement = () => client.writeData({ data: { counter: counter + 1 }}); | |
| const handleDecrement = () => client.writeData({ data: { counter: counter - 1 }}); | |
| return ( | |
| <CounterView | |
| {...data} | |
| onDecrement={handleDecrement} | |
| onIncrement={handleIncrement} | |
| /> | |
| ); | |
| }} | |
| </Query> | |
| ); | |
| export default Counter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment