我已经在 indexa.js 中声明了一个 React 上下文,并且我正在尝试访问在 Employee.js 中的 App.js 中设置的上下文值。但这并没有体现出我对 React 不熟悉的价值。请帮帮我。提前致谢
//index.js
export const employeeContext = React.createContext();
const AppContextInteration = <App></App>
ReactDOM.render(AppContextInteration, document.getElementById('root'));
//App.js
export class App extends React.Component {
constructor(props) {
super(props);
this.state = {
EmpId: 1,
EmpName: 'xxx'
}
}
render() {
return (
<div>
<h1> App Component</h1>
<p> Employee Id : {this.state.EmpId}</p>
<employeeContext.Provider value={this.state} >
<Employee/>
</employeeContext.Provider>
</div>
)
}
}
//Employee.js
export class Employee extends React.Component {
context = employeeContext;
constructor(props) {
super(props);
console.log();
}
render() {
return (
<div>
<h1>Employee Component </h1>
<p> Employee Id : {this.context.EmpId} </p>
</div>
)
}
}