javascript - 从 React Context 命名的导入不能按预期工作
问题描述
这是我的上下文设置:
export const myContext = Rect.CreateContext({
strings:[],
addString(newString){
this.strings.push(newString)
}
})
在我的一个嵌套组件中,我正在尝试执行以下操作:
class myComponent extends Component{
static contextType = myContext;
myFunction(){
const { addSting } = this.context ;
addString('mystring'); //- this causes "TypeError: Cannot read property 'string' of undefined"
this.context.addString('mystring'); //- this runs fine
}
}
为什么我不能在addString
不指定的情况下使用“”方法this.context
?
解决方案
在您的解构中,您缺少字符串中的“r”
尝试
const {addString} = this.context
推荐阅读
- php - 无法发布分钟,小时工作
- python - 为什么 Pycharm 在 typing.py 中打开 MutableMapping 而不是 _collections_abc.py 中的类 MutableMapping 定义
- python - python检查路径后的值是否相同
- vba - 访问 VBA 错误:表达式引用已关闭的对象
- java - 为什么 mongodb java 驱动程序在 ObjectId 中使用随机字节而不是机器 id?
- python - 如何将熊猫数据框列中的两种不同日期格式转换为相同格式?
- php - 获取帖子缩略图 URL 并分配给变量
- node.js - nodejs中的多个Promise等到它们全部完成
- c - 已解决的睡眠方法将程序线程挂在 C pthreads 中(竞争条件,它与睡眠无关)
- python - wxpython绑定windows toast通知处理程序的问题