When I proposed PR #576, I had some uncertainty about how Preact handles context. Currently, the TypeScript declarations do provide for context?: any in a few locations, so I just followed that convention. That said, it looks like Preact does _not_(?) support -- or at least make use of -- contextTypes. I searched the documentation and the source to learn more about this, and so far have two issues (that I'll file here as one issue):
I couldn't find anything in the documentation about whether Preact actually does make use of contextTypes. I'm assuming that it doesn't, since it doesn't make use of propTypes. My impression from the source is also that it does not. I think this should probably be documented somewhere (I'm happy to PR it; just want to check that I'm right and that I didn't actually miss some documentation, first).
Also, from looking at the source, it looks like Preact's behavior with respect to context may not be exactly the same as React's. Is this intended? As I understand it, in React, if a component does not define some context on contextTypes, then the component does not receive that context (see here, for example: https://github.com/facebook/react/blob/e452e3374135c116ef687a8bb3a5d277e3cde8fb/src/isomorphic/classic/__tests__/ReactContextValidator-test.js#L40). But if Preact doesn't use contextTypes at all, then it presumably doesn't have this behavior. Is _that_ right, and is it intended? This might create some subtle issues with migrations from React to Preact, especially if (as in point 1 above) it isn't documented anywhere, so I wanted to check.
Mostly, I'm looking for clarification on these things and seeing whether it would be helpful for me to try to PR some docs and/or some changes to the treatment of context. Thanks!
Both absolutely correct! The decision to automatically pass context down rather than relying on contextTypes comes from the size constraints placed on Preact. Since context is an undocumented/hidden (less-documented) API in React, the change has never caused much trouble that I'm aware of.
Definitely lacking in the documentation, though part of that comes from the docs needing to walk a fine line between catering to React devs (who would understand contextTypes) and web devs approach Preact without having written React (who would be fine knowing "context is automatically passed down"). However we can document this such that is explains things to both groups of people would be awesome. Definitely open to PRs for docs, you seem to be a very good writer!
That said, having preact-compat inject context mutation that _does_ mirror the subscription behavior of React is an issue I've had on the backburner for a while now. I don't think there's a Github issue set up for it, so we might want to open one in preact-compat. That seems like the type of thing people would submit a PR for (preact-compat is mostly community maintained at this point, which is awesome).
Cool, and thanks for the quick response! That all sounds good. I'll try to take a look at the docs as time permits.
Hi guys, I'm interested in getting started as a contributor and would love to take a shot at this.
@paulsevere I'd say go for it! The "help wanted" label is attached, and I'm not expecting that I will have a lot of time to do this myself any time soon (despite reporting the issue).
We added a section about context starting with Preact X in our docs: https://preactjs.com/guide/v10/context
Most helpful comment
Hi guys, I'm interested in getting started as a contributor and would love to take a shot at this.