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

[Basic] : appContext is possibly null ? #603

Closed
alamenai opened this issue Jan 8, 2023 · 1 comment · Fixed by #604
Closed

[Basic] : appContext is possibly null ? #603

alamenai opened this issue Jan 8, 2023 · 1 comment · Fixed by #604
Assignees
Labels
BASIC Basic Cheatsheet

Comments

@alamenai
Copy link
Contributor

alamenai commented Jan 8, 2023

Site

react typescript cheatsheet

Section

Basic

Page

Context

Snippet

import { createContext } from "react";

interface AppContextInterface {
  name: string;
  author: string;
  url: string;
}

const AppCtx = createContext<AppContextInterface | null>(null);

// Provider in your app

const sampleAppContext: AppContextInterface = {
  name: "Using React Context in a Typescript App",
  author: "thehappybug",
  url: "http://www.example.com",
};

export const App = () => (
  <AppCtx.Provider value={sampleAppContext}>...</AppCtx.Provider>
);

// Consume in your app
import { useContext } from "react";

export const PostInfo = () => {
  const appContext = useContext(AppCtx);
  return (
    <div>
      Name: {appContext.name}, Author: {appContext.author}, Url:{" "}
      {appContext.url}
    </div>
  );
};

Issue location

  // appContext is possibly null?!
  <div>
      Name: {appContext.name}, Author: {appContext.author}, Url:{" "}
      {appContext.url}
    </div>

Reproduce

Try Typescript Playground

Recommended PR

Add non-nullish assertion symbol ! after appContext.

@alamenai alamenai added the BASIC Basic Cheatsheet label Jan 8, 2023
@filiptammergard filiptammergard linked a pull request Jan 9, 2023 that will close this issue
@gauravmnjwr
Copy link

Hey there, If the issue is still open I'd like to work on it.

gauravmnjwr added a commit to gauravmnjwr/react that referenced this issue Jan 12, 2023
@gauravmnjwr gauravmnjwr mentioned this issue Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BASIC Basic Cheatsheet
Projects
None yet
3 participants