Add Server Context deprecation warning (#27424)

As agreed, we're removing Server Context. This was never official
documented.

We've found that it's not that useful in practice. Often the better
options are:

- Read things off the url or global scope like params or cookies.
- Use the module system for global dependency injection.
- Use `React.cache()` to dedupe multiple things instead of computing
once and passing down.

There are still legit use cases for Server Context but you have to be
very careful not to pass any large data, so in generally we recommend
against it anyway.

Yes, prop drilling is annoying but it's not impossible for the cases
this is needed. I would personally always pick it over Server Context
anyway.

Semantically, Server Context also blocks object deduping due to how it
plays out with Server Components that can't be deduped. This is much
more important feature.

Since it's already in canary along with the rest of RSC, we're adding a
warning for a few versions before removing completely to help migration.

---------

Co-authored-by: Josh Story <josh.c.story@gmail.com>

DiffTrain build for [1ebedbec2b](https://github.com/facebook/react/commit/1ebedbec2bec08e07c286ea6c3cff62737a0fd3a)
This commit is contained in:
sebmarkbage
2023-09-28 15:08:13 +00:00
parent 6db6c398f0
commit 20fecfd7af
5 changed files with 23 additions and 6 deletions
+1 -1
View File
@@ -1 +1 @@
f81c0f1ed962036f25b87281b3c18cef3860ded8
1ebedbec2bec08e07c286ea6c3cff62737a0fd3a
+9 -1
View File
@@ -27,7 +27,7 @@ if (
}
"use strict";
var ReactVersion = "18.3.0-www-classic-ceb12d9d";
var ReactVersion = "18.3.0-www-classic-443dd2b0";
// ATTENTION
// When adding new symbols to this file,
@@ -2949,6 +2949,14 @@ function cloneElementWithValidation(element, props, children) {
var ContextRegistry = ReactSharedInternals.ContextRegistry;
function createServerContext(globalName, defaultValue) {
{
error(
"Server Context is deprecated and will soon be removed. " +
"It was never documented and we have found it not to be useful " +
"enough to warrant the downside it imposes on all apps."
);
}
var wasDefined = true;
if (!ContextRegistry[globalName]) {
+9 -1
View File
@@ -27,7 +27,7 @@ if (
}
"use strict";
var ReactVersion = "18.3.0-www-modern-6c7b6f54";
var ReactVersion = "18.3.0-www-modern-df3e4079";
// ATTENTION
// When adding new symbols to this file,
@@ -2915,6 +2915,14 @@ function cloneElementWithValidation(element, props, children) {
var ContextRegistry = ReactSharedInternals.ContextRegistry;
function createServerContext(globalName, defaultValue) {
{
error(
"Server Context is deprecated and will soon be removed. " +
"It was never documented and we have found it not to be useful " +
"enough to warrant the downside it imposes on all apps."
);
}
var wasDefined = true;
if (!ContextRegistry[globalName]) {
@@ -16694,7 +16694,7 @@ Internals.Events = [
var devToolsConfig$jscomp$inline_1805 = {
findFiberByHostInstance: getClosestInstanceFromNode,
bundleType: 0,
version: "18.3.0-www-classic-ceb12d9d",
version: "18.3.0-www-classic-443dd2b0",
rendererPackageName: "react-dom"
};
var internals$jscomp$inline_2154 = {
@@ -16724,7 +16724,7 @@ var internals$jscomp$inline_2154 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-classic-ceb12d9d"
reconcilerVersion: "18.3.0-www-classic-443dd2b0"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_2155 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
@@ -17212,4 +17212,4 @@ exports.unstable_renderSubtreeIntoContainer = function (
);
};
exports.unstable_runWithPriority = runWithPriority;
exports.version = "18.3.0-www-classic-ceb12d9d";
exports.version = "18.3.0-www-classic-443dd2b0";
+1
View File
@@ -258,6 +258,7 @@
"Rendering <Context.Consumer.Provider> is not supported and will be removed in a future major release. Did you mean to render <Context.Provider> instead?"
"Rendering <Context> directly is not supported and will be removed in a future major release. Did you mean to render <Context.Consumer> instead?"
"Select elements must be either controlled or uncontrolled (specify either the value prop, or the defaultValue prop, but not both). Decide between using a controlled or uncontrolled select element and remove one of these props. More info: https://reactjs.org/link/controlled-components"
"Server Context is deprecated and will soon be removed. It was never documented and we have found it not to be useful enough to warrant the downside it imposes on all apps."
"ServerContext can only have a value prop and children. Found: %s"
"Setting `displayName` on Context.Consumer has no effect. You should set it directly on the context with Context.displayName = '%s'."
"Setting defaultProps as an instance property on %s is not supported and will be ignored. Instead, define defaultProps as a static property on %s."