javascript - API 调用后 React 事件处理程序设置为 null
问题描述
我今天面临一个奇怪的问题
我在onChange上调用方法
用户界面:
Data:
<form onChange={this.handleChangeattribute} >
<DynamicInputs dynamicAttributes={this.state.dynamicAttributes}/>
</form>
我的动态输入文件:
import React from "react"
const DynamicInputs = (props) => {
return (
props.dynamicAttributes.map((val, idx)=> {
let catId = `title-${idx}`, ageId = `image-${idx}`
return (
<div key={idx}>
<br/>
<label htmlFor={catId}>{`${idx + 1}:`}</label>
<br />
Title <input
type="text"
name={catId}
data-id={idx}
id={catId}
value={props.dynamicAttributes[idx].title}
className="title"
/><br/><br/>
Image: <input
type="file"
data-id={idx} id={ageId} className="image"
/>
<br/><br/>
Description<input
type="text"
name={catId}
data-id={idx}
id={catId}
value={props.dynamicAttributes[idx].description}
className="description"
/>
<br/>
</div>
)
})
)
}
export default DynamicInputs
功能:
handleChangeattribute = async (e) => {
// here I got all function of the event including value of e.target.dataset.id
if (["title", "image", "description"].includes(e.target.className) ) {
let dynamicAttributes = [...this.state.dynamicAttributes]
if(e.target.className === "title") {
dynamicAttributes[e.target.dataset.id][e.target.className] = e.target.value
}
if(e.target.className === "image") {
let data
await axios.post(data)
.then((result) => {
data = result
});
console.log("e.target", e)
console.log("e.target", e.target)
dynamicAttributes[e.target.dataset.id][e.target.className] = data
}
if(e.target.className === "description") {
dynamicAttributes[e.target.dataset.id][e.target.className] = e.target.value
}
this.setState({ dynamicAttributes }, () => console.log(this.state.dynamicAttributes))
} else {
this.setState({ [e.target.title]: e.target.value })
}
}
当我选择图像然后完成 axios 调用时,我收到这样的错误
ExploreDetails.js:86 Uncaught (in promise) TypeError: Cannot read property 'dataset' of null
在第一个控制台的handleChangeattribute方法中,我得到了这个,在第二个中我得到了null
解决方案
我在这里重构了代码。请让我知道它是否有效。您将 async/await 与 with 结合使用.then()
没有意义,因此我已将其更改为仅使用.then()
.
handleChangeattribute = (e) => {
// here I got all function of the event including value of e.target.dataset.id
if (["title", "image", "description"].includes(e.target.className)) {
let dynamicAttributes = [...this.state.dynamicAttributes]
if (e.target.className === "title") {
dynamicAttributes[e.target.dataset.id][e.target.className] = e.target.value
}
if (e.target.className === "image") {
let data
axios.post(data)
.then((result) => {
console.log("e.currentTarget", e.currentTarget)
dynamicAttributes[e.currentTarget.dataset.id][e.target.className] = result
});
}
if (e.target.className === "description") {
dynamicAttributes[e.target.dataset.id][e.target.className] = e.target.value
}
this.setState({ dynamicAttributes }, () => console.log(this.state.dynamicAttributes))
} else {
this.setState({ [e.target.title]: e.target.value })
}
}
推荐阅读
- html - 如何修复动态主体和动态页脚的css高度
- c - 对链表使用空闲函数时出现内存错误
- exist-db - eXistDB - 按索引删除用户 ACE....如何获取索引?
- mysql - 如何修复mysql中的“功能不存在”错误?
- javascript - 如何将 javascript 变量中的信息添加到 Django ModelForm 输入?
- java - 同步块是否会导致所有写入缓存刷新?
- typescript - 如何制作通用的`class Foo
` 在 TypeScript 中具有特定的初始值? - python - 如何使用 python 请求库读取 xml 响应?
- php - 在php中获取视频文件的持续时间/长度
- build - 创建新的构建定义时,工作空间映射由什么构成?