Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
View Rendered Text
Selectors for Context are an in-demand feature, but with a slightly different interface, can be made much more universally useful (.slice as proposed here is similar).
Following from functional programming techniques, this RFC proposes adding:
ReadonlyContext<T>
type which is a supertype ofContext<T>
without.Provider
and these methods to Context:
.map
, of type:<T, U>(this: ReadonlyContext<T>, fn: (data: T) => U) => ReadonlyContext<U>
.apply
, of type:<T, U>(this: ReadonlyContext<T>, fn: ReadonlyContext<(data: T) => U>) => ReadonlyContext<U>
(I actually recommend one of its alternatives).map
allows users to refine and select their Contexts, while.apply
allows users to combine multiple Contexts together. This is covered in detail in Motivation.Every call, without respect to the identity of the given argument (function / context of functions), returns a distinct Context. To be usable within renders, existing user-land techniques like WeakMap can be used to stabilize the identity of the returned Context to the identity of the given argument.
RFC 119 useContextSelector draws from much of the same motivation.