Index of every page on this site: llms.txt
All skills
github avatar

/react18-legacy-context

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

Provides the complete migration pattern for React legacy context API (contextTypes, childContextTypes, getChildContext) to the modern createContext API. Use this skill whenever migrating legacy context in class components - this is always a cross-file migration requiring the provider AND all consumers to be updated together. Use it before touching any contextTypes or childContextTypes code, because migrating only the provider without the consumers (or vice versa) will cause a runtime failure. Always read this skill before writing any context migration - the cross-file coordination steps here prevent the most common context migration bugs.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/react18-legacy-context

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencescontext-file-template.md

≈824 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Context File Template

Standard template for a new context module. Copy and fill in the name.

Template

// src/contexts/[Name]Context.js
import React from 'react';

// ─── 1. Default Value ───────────────────────────────────────────────────────
// Shape must match what the provider will pass as `value`
// Used when a consumer renders outside any provider (edge case protection)
const defaultValue = {
  // fill in the shape
};

// ─── 2. Create Context ──────────────────────────────────────────────────────
export const [Name]Context = React.createContext(defaultValue);

// ─── 3. Display Name (for React DevTools) ───────────────────────────────────
[Name]Context.displayName = '[Name]Context';

// ─── 4. Optional: Custom Hook (strongly recommended) ────────────────────────
// Provides a clean import path and a helpful error if used outside provider
export function use[Name]() {
  const context = React.useContext([Name]Context);
  if (context === defaultValue) {
    // Only throw if defaultValue is a sentinel - skip if a real default makes sense
    // throw new Error('use[Name] must be used inside a [Name]Provider');
  }
  return context;
}

Filled Example - AuthContext

// src/contexts/AuthContext.js
import React from 'react';

const defaultValue = {
  user: null,
  isAuthenticated: false,
  login: () => Promise.resolve(),
  logout: () => {},
};

export const AuthContext = React.createContext(defaultValue);
AuthContext.displayName = 'AuthContext';

export function useAuth() {
  return React.useContext(AuthContext);
}

Filled Example - ThemeContext

// src/contexts/ThemeContext.js
import React from 'react';

const defaultValue = {
  theme: 'light',
  toggleTheme: () => {},
};

export const ThemeContext = React.createContext(defaultValue);
ThemeContext.displayName = 'ThemeContext';

export function useTheme() {
  return React.useContext(ThemeContext);
}

Where to Put Context Files

src/
  contexts/           ← preferred: dedicated folder
    AuthContext.js
    ThemeContext.js

Alternative acceptable locations:

src/context/          ← singular is also fine
src/store/contexts/   ← if co-located with state management

Do NOT put context files inside a component folder - contexts are cross-cutting and shouldn't be owned by any one component.

Provider Placement in the App

Context providers wrap the components that need access. Place as low in the tree as possible, not always at root:

// App.js
import { ThemeProvider } from './ThemeProvider';
import { AuthProvider } from './AuthProvider';

function App() {
  return (
    // Auth wraps everything - login state is needed everywhere
    <AuthProvider>
      {/* Theme wraps only the UI shell - not needed in pure data providers */}
      <ThemeProvider>
        <Router>
          <AppShell />
        </Router>
      </ThemeProvider>
    </AuthProvider>
  );
}

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo4 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    The skill is a developer-focused migration guide for React applications. It provides instructions, templates, and static analysis commands to help developers move from legacy context APIs to modern React patterns. No security risks were identified.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 7 months ago

README badge

README badge for github/awesome-copilot/react18-legacy-context

Provides the step-by-step migration pattern for converting React legacy context (contextTypes, childContextTypes, getChildContext) to createContext, with emphasis on coordinating provider and consumer updates across multiple files. Targets React 18.3.1+ codebases migrating before React 19 removal, and prevents the common bug of updating only the provider or only consumers without the other.

Generated from the current SKILL.md.

Does this skill handle both class and function components?
Yes. Class components migrate to contextType, function components migrate to useContext. The skill provides patterns for both in the reference files.
What happens if I migrate only the provider without updating all consumers?
The app will break at runtime. Consumers will read from the wrong context or get undefined. This skill emphasizes that context migration is always cross-file and requires updating the provider AND every consumer together.
Does this work with React 19?
This skill is necessary for React 19 because legacy context (contextTypes, childContextTypes, getChildContext) was removed in that version.
How do I find all the places that need to be updated?
The skill includes grep commands to scan for providers (childContextTypes/getChildContext), consumers (contextTypes), and this.context usage across your codebase.
Does this skill handle multiple contexts in the same app?
Yes. The references include a multi-context pattern for apps with nested providers and multiple legacy contexts.

Generated from the current SKILL.md. These answers refresh after source changes.