Skip to content

[Bug]: useContext does not always require destructuring-assignment #3617

Closed as not planned
@uipoptart

Description

@uipoptart

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

Hi! This library is awesome!

In latest (7.33.2) I'm getting a linting error for a non-destructured assign from useContext. Looking at the React docs for useContext I think the context value might not always require destructuring.

Expected Behavior

Here is a simple example based on the React docs for creating a context. In this example mapping over the items returned from useContext yields the following linting error:

Must use destructuring availableFonts assignment react/destructuring-assignment

import React, { useContext } from 'react';

const items = ['beep', 'boop', 'bop'];
const MyContext = React.createContext<string[]>(items);

const ParentComponent = () => (
  <MyContext.Provider value={items}>
    <ChildComponent />
  </MyContext.Provider>
);

const ChildComponent = () => {
  const itemsFromContext = useContext(MyContext);
  return (
    <div>
      {itemsFromContext.map((name:string) => ( // Linting error here
        <span>{name}</span>
      ))}
    </div>
  );
};

eslint-plugin-react version

v7.31.11

eslint version

v8.28.0

node version

v8.19.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions