如何在类组件的函数内访问 React Context 内的对象。
我有以下 React 组件
import StoreContext from '../../context/StoreContext'
class ProductForm extends Component {
static contextType = StoreContext
constructor(props) {
super(props);
}
handleOptionChange(event) {
const test = this.contextType.client.testObject
}
我试过client
像这样访问上下文中的对象,但它不起作用,因为它说无法读取未定义的属性。
我想知道我的错误在哪里。