呈现您的上下文值似乎是一个问题,以下代码运行良好
index.js
import React from "react";
import { render } from "react-dom";
import { HashRouter, Switch, Route } from "react-router-dom";
import Hello from "./Hello";
import { Context } from "./context";
const App = () => (
<Context.Provider value={{ valueTest: 1 }}>
<HashRouter>
<Switch>
<Route
exact
path="/"
render={props => <Hello {...props} name="CodeSandbox" />}
/>
</Switch>
</HashRouter>
</Context.Provider>
);
render(<App />, document.getElementById("root"));
上下文.js
import React from "react";
export const Context = React.createContext();
你好.js
import React from "react";
import { Context } from "./context";
export default ({ name }) => (
<Context.Consumer>
{value => (
<h1>
Hello {name} {value.valueTest}!
</h1>
)}
</Context.Consumer>
);
工作代码框