Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

TS: Type of ApolloContext makes it hard to use #3016

@sseppola

Description

@sseppola

Intended outcome:
I tried to use the exposed ApolloContext with React.useContext in my TS project.

Actual outcome:
I get a type error when using ApolloContext.

Screen Shot 2019-05-06 at 11 17 34

the problem is that the exposed ApolloContext type is defined as:

export interface ApolloContextValue {
    client?: ApolloClient<Object>;
    operations?: Map<string, {
        query: DocumentNode;
        variables: any;
    }>;
}
export declare const ApolloContext: React.Context<ApolloContextValue | undefined> | null;

where React.useContext doesn't accept null as a context.

How to reproduce the issue:
In a TS project:

import * as React from 'react';
import { ApolloContext } from 'react-apollo';

export function MyComponent(props) {
  const apolloContext = React.useContext(ApolloContext);
  return <div>stuff</div>;
}

I tried to use the codesandbox error template, but I got errors on all dependencies after changing the filenames to .tsx.

Version

Workaround

import * as React from 'react';
import { ApolloContext } from 'react-apollo';
import { ApolloContextValue } from 'react-apollo/ApolloContext';

type ApolloContextT = React.Context<ApolloContextValue>;

export function MyComponent(props) {
  const apolloContext = React.useContext(ApolloContext as ApolloContextT);
  return <div>stuff</div>;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions