如何解决 react 中 app.js 报错 uncaught typeerror: cannot read properties of undefined
在 react 开发过程中,经常会碰到一些报错。这里我们来看一个错误,是在 app.js 中报出来的。错误是这样的:app.js:69 uncaught typeerror: cannot read properties of undefined (reading 'setstate') at oninputchange (app.js:69:1)
下面是产生错误的完整的代码
// 调用 clarifai api接口
const app = new clarifai.app({
apikey : 'a2013154seuiasdfdse94302c5bd'
});
class app extends react.component {
constructor () {
super();
this.state = {
input : '',
imageurl : '',
}
}
oninputchange(event) {
this.setstate({input: event.target.value})
console.log('setstate input' this.state.input);
}
onbuttonsubmit() {
this.setstate({imageurl: this.state.input});
app.models.predict(clarifai.color_model, this.state.input).then(
function(response) {
console.log(response);
},
function(err) {
// 这里处理错误
}
)
}
render(){
return (
<div classname="app">
<particles classname="particles" id="tsparticles" options={particlesoptions} />
<navigation />
<logo />
<rank />
<imagelinkform oninputchange={this.oninputchange} onbuttonsubmit = {this.onbuttonsubmit} />
<facerecognition imageurl = {this.state.imageurl} />
div>
);
}
}
export default app;
解决这个问题的方法很少。
至于为什么会出现这种情况是因为在上面的代码中没有使用箭头方式,这里我们有两种解决方式
-
我们需要对处理程序进行绑定。如下所示
constructor() { ...code this.oninputchange = this.oninputchange.bind(this) this.onbuttonsubmit = this.oninputchange.bind(this) }
-
将方法改为箭头的方式(es6 语法)。
oninputchange = (event) => { this.setstate({input: event.target.value}) console.log('setstate input' this.state.input); } onbuttonsubmit = () => { this.setstate({imageurl: this.state.input}); app.models.predict(clarifai.color_model, this.state.input).then( function(response) { console.log(response); }, function(err) { // 这里处理错误 } ) }
我们可以选择上面两种方式中的任一种。排除各自的环境因素,都可以解决上面的报错。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
node.js 与 react js 的比较
发布时间:2023/03/27 浏览次数:173 分类:node.js
-
本文比较和对比了两种编程语言,node.js 和 react。react 和 node.js 都是开源 javascript 库的示例。 这些库用于构建用户界面和服务器端应用程序。
在 typescript 中 react usestate 钩子类型
发布时间:2023/03/19 浏览次数:246 分类:typescript
-
本教程演示了如何在 typescript 中使用 react usestate hook。
typescript 中的 react 事件类型
发布时间:2023/03/19 浏览次数:477 分类:typescript
-
本教程演示了如何在 typescript 中为 react 事件添加类型支持。
在 react 中循环遍历对象数组
发布时间:2023/03/18 浏览次数:602 分类:react
-
在 react 中循环对象数组: 使用 map() 方法迭代数组。 我们传递给 map() 的函数会为数组中的每个元素调用。 该方法返回一个新数组,其中包含传入函数的结果。 export default function app (
获取 react 中元素的类名
发布时间:2023/03/18 浏览次数:340 分类:react
-
在 react 中使用 event.target 获取元素的类名 获取元素的类名: 将元素上的 onclick 属性设置为事件处理函数。 访问元素的类名作为 event.currenttarget.classname 。 export default function app () { cons
如何将 key 属性添加到 react 片段
发布时间:2023/03/18 浏览次数:206 分类:react
-
使用更详细的片段语法将 key 属性添加到 react 片段,例如 react.fragment key={key} 。 更冗长的语法实现了相同的结果对元素列表进行分组,而不向 dom 添加额外的节点。 import react from react
如何在 react 中删除事件监听器
发布时间:2023/03/15 浏览次数:592 分类:react
-
在 react 中删除事件监听器: 在 useeffect 挂钩中添加事件侦听器。 从 useeffect 挂钩返回一个函数。 当组件卸载时,使用 removeeventlistener 方法移除事件监听器。 import {useref, useeffect} from r
react 中在 map() 中使用条件跳出map
发布时间:2023/03/15 浏览次数:261 分类:react
-
react 中在 map() 中使用条件: 在数组上调用 map() 方法。 使用 if 条件,如果条件满足则显式返回。 否则返回不同的值或返回 null 以不呈现任何内容。 export default function app () { const arr =
在 react 中调用多个 onclick 函数
发布时间:2023/03/15 浏览次数:267 分类:react
-
在 react 中调用多个 onclick 函数: 在元素上设置 onclick 属性。 在事件处理函数中调用其他函数。 事件处理函数可以根据需要调用尽可能多的其他函数。 export default function app () { const s