在 CodeSandBox 中反应上下文不是 运行

React Context not running in CodeSandBox

我正在尝试在 codesandbox 中 运行 this React Context example,但出现此错误:

Invariant Violation

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check the render method of `MyProvider`.

我将 React 设置为 16.3,并且代码本身是直接从 this Wes Bos example. 中提取的。我查看了代码几次,但没有发现任何问题。难道是codesandbox的上下文设置我不明白?

您还必须使用 react-dom 的 16.3 版本。在您的沙盒中,react 是 16.3.0,react-dom 是 16.2.0。这是一个带有工作示例的沙箱:https://codesandbox.io/s/1r1j221nl3