Skip to content

Context set in collection item node not propagating #7049

@jeffijoe

Description

@jeffijoe

Provide a general summary of the issue here

When wrapping collection item elements with context, the context value does not propagate correctly.

Example:

import React from "react";
import {
  Table,
  TableHeader,
  TableBody,
  Row,
  Column,
  Cell,
} from "react-aria-components";

const SomeContext = React.createContext("unset");

export default function App() {
  return (
    <div>
      <Table>
        <TableHeader>
          <Column isRowHeader>Column</Column>
        </TableHeader>
        <TableBody>
          {[1, 2, 3].map((i) => (
            <SomeContext.Provider
              key={i}
              value={`per-row context for row ${i}`}
            >
              <Row>
                <MyCell>Row {i} should have per-row context but is</MyCell>
              </Row>
            </SomeContext.Provider>
          ))}

          <SomeContext.Provider value="last row">
            <Row>
              <MyCell>Last row, but is</MyCell>
            </Row>
          </SomeContext.Provider>
        </TableBody>
      </Table>
      <SomeContext.Provider value="separate">
        <Inner>Rendered outside the context of a table, so is</Inner>
      </SomeContext.Provider>
    </div>
  );
}

function MyCell({ children, ...rest }) {
  // Works here, the context value being read is correct
  const contextValue = React.useContext(SomeContext);
  return (
    <Cell {...rest}>
      <Inner>{children}</Inner>
      [context read directly from cell: {contextValue}]
    </Cell>
  );
}

function Inner({ children }) {
  // For some reason doesn't work here? The context value is that of whichever
  // was set outside the collection component, in this case "unset".
  const contextValue = React.useContext(SomeContext);
  return (
    <div>
      {children} {contextValue}
    </div>
  );
}

Result:

image

🤔 Expected Behavior?

Context should propagate normally.

😯 Current Behavior

Only the nearest context value outside the collection is read.

💁 Possible Solution

I don't know too much about it, but I'm suspecting it's something to do with the collection node components.

🔦 Context

The code sandbox reproduces the issue using Tables, but I found a similar issue with other collection-based components such as Menu.

🖥️ Steps to Reproduce

Open the code sandbox and observe the context values not being propagated correctly.

Version

RAC 1.3.3, RA 3.3.34.3

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

macOS

🧢 Your Company/Team

No response

🕷 Tracking Issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions