RFC: Context should accept non-children props as value
This RFC proposes allowing React Context to accept non-children props as the provided value.
- Keeps
valuefor backward compatibility. - If no
valueis passed, all non-children props become the context value. - Excludes
childrenfrom 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);
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!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
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
valueand 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.
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.
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} />
Yes but be carefull about performances here : you're recreating a new value object during each render. You may want to memoize it