Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React useContext #64

Closed
lifeiscontent opened this issue Oct 27, 2018 · 1 comment
Closed

React useContext #64

lifeiscontent opened this issue Oct 27, 2018 · 1 comment

Comments

@lifeiscontent
Copy link

lifeiscontent commented Oct 27, 2018

I'd love to see apollo take advantage of useContext in the new release of React.

potentially simplifying some of the work with Query Component, ApolloConsumer, etc.

e.g. (this is pseudo code)

import React, { useState, useEffect, useContext } from 'react';
import { ApolloContext, gql } from 'react-apollo';

function useQuery(query) {
  const [data, setData] = useState(null);
  const apollo = useContext(ApolloContext);

  function handleDataChange(data) {
    setData(data);
  }

  useEffect(() => {
    return apollo.watchQuery(query).valueChanges.subscribe(handleDataChange);
  });

  return data;
}

function ListView() {
  const data = useQuery({
    query: gql`
      {
        posts {
          title
        }
      }
    `,
  });

  return (
    <ol>
      {data.posts.map((item, index) => (
        <li key={index}>{item.title}</li>
      ))}
    </ol>
  );
}

I'd expect useApollo, useQuery, useMutation, etc to be a hook apollo would provide

@kristianmandrup
Copy link

Started implementing feature here
Added usage docs and examples for current implementation attempt: Apollo Hooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants