rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

RFC: Context should accept non-children props as value

Open Gokul-Gireesh opened this issue 7 months ago • 6 comments

This RFC proposes allowing React Context to accept non-children props as the provided value.

  • Keeps value for backward compatibility.
  • If no value is passed, all non-children props become the context value.
  • Excludes children from injection.
  • Simplifies context usage and makes it consistent with normal prop-passing in React.

Example:

<MyContext foo={foo} bar={bar}>
  <App />
</MyContext>

Consumer:

const { foo, bar } = useContext(MyContext);

Gokul-Gireesh avatar Aug 30 '25 16:08 Gokul-Gireesh

Hi @Gokul-Gireesh!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

meta-cla[bot] avatar Aug 30 '25 16:08 meta-cla[bot]

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

meta-cla[bot] avatar Aug 30 '25 16:08 meta-cla[bot]

Thanks for posting. I don’t think we’re likely to proceed with this change: it’s confusing. In the existing API there’s a clear, 1:1 match between the value you pass to a context provider and the value you get back out w useContext(). The proposal has a number of issues:

  • implicit object creation
  • unclear what should happen if you pass both a value and other props
  • Hard to type in type systems

More fundamentally, it isn’t clear this is solving a real problem. We’re always open to exploring api changes, but in this case I don’t think we will proceed given the clear problems and lack of a strong motivation.

josephsavona avatar Aug 30 '25 19:08 josephsavona

Thanks for the detailed feedback. I realize I may not have fully updated the PR comment when submitting — the Value Handling Rules in my commit already clarify how value is handled alongside other props.

I understand the concerns about implicit object creation, TypeScript typing, and whether this really solves a problem, and I see how that could add complexity.

Some of the points may reflect my earlier misunderstandings, so I appreciate the clarification and the focus on keeping the API predictable and backward-compatible.

Gokul-Gireesh avatar Aug 31 '25 10:08 Gokul-Gireesh

I think there's very little value in something that's trivially easy to implement by yourself.

const Context2 = ({ children, ...value }) => <React.Context value={value} children={children} />

vezaynk avatar Sep 10 '25 17:09 vezaynk

Yes but be carefull about performances here : you're recreating a new value object during each render. You may want to memoize it

fuunnx avatar Sep 11 '25 14:09 fuunnx