javascript - 动态生成时反应表单元素 onchange 事件不起作用
问题描述
我正在尝试从数据中生成输入列表,并让它们作为受控输入来捕获它们的数据。文本输入似乎得到了很好的捕获,但是文件输入 onchange 事件得到了错误"TypeError: _this2.handleFileInputChange is not a function"。如果我将它从箭头函数中取出,我不会收到错误消息,但回调仍然不起作用。各位法师怎么看?
- - - - - 编辑 - - - - - - -
实现了将函数定义为箭头函数的建议,这很好,但文件输入似乎仍然没有像其他字段那样触发更改事件。我开始认为这是组件的限制。
还有其他想法吗?谢谢!
import React from "react";
// import { Link } from "gatsby";
import Layout from "../components/layout";
import {Form,InputGroup,Button} from 'bootstrap-4-react'
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function(word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase();
}).replace(/\s+/g, '');
}
const formContent = [
["Profile Name","Ex. College soccer profile Spring 2020","text"],
["Player Name", "Ex. Jose Greer", "text"],
["Player Profile Photo", "Ex. Jose Greer", "file"]
]
class ProfileForm extends React.Component {
constructor(props) {
super(props);
this.state = {
profileName: '',
playerName: '',
profileImageFilename: '',
};
}
handleFileInputchange = (e) => {
console.log(e.target.files[0]);
this.setState({profileImageFilename: e.target.files[0].filename})
}
handleInputChange = (e) => {
console.log(e)
this.setState({[e.target]: e.target.value})
}
handleSubmit = (e) => {
console.log(e)
e.preventDefault()
}
createFormGroup(i) {
const title = i[0],
placeholder = i[1],
type = i[2]
const titleCamelCase = camelize(title);
if (type === "file") {
return (
<>
<label key={titleCamelCase + "-label"} htmlFor={titleCamelCase}>
Player Profile Image
</label>
<InputGroup key={titleCamelCase + "-group"} mb="3">
<Form.CustomFile
required
onChange={this.handleFileInputChange}
key={titleCamelCase + "-file"}
id={titleCamelCase}
accept=".jpg, .jpeg, .png"
>
{this.state.profileImageFilename
? this.state.profileImageFilename
: "Choose an image. ( .jpg, .jpeg, .png, .gif )"}
</Form.CustomFile>
</InputGroup>
</>
);
}
return (
<>
<label key={titleCamelCase + "-label"} htmlFor={titleCamelCase}>
{title}
</label>
<Form.Input
key={titleCamelCase + "-input"}
mb="3"
required
type={type}
id={titleCamelCase}
placeholder={placeholder}
onChange={this.handleInputChange}
valule={this.state[titleCamelCase]}
/>
</>
);
}
render() {
return (
<Layout>
<Form onSubmit={this.handleSubmit}>
<Form.Group>
{/* {formContent.map(function(i){this.createFormGroup(i)}.bind(this))} */}
{formContent.map((i)=>this.createFormGroup(i))}
</Form.Group>
<Button primary type="submit">
Submit
</Button>
</Form>
</Layout>
);
}
}
export default ProfileForm
解决方案
问题
this
类组件的 未绑定到回调handleFileInputchange
处理程序。
解决方案
您应该绑定this
到或将handleFileInputchange
其constructor
声明为箭头函数。
constructor(props) {
super(props);
this.state = {
profileName: '',
playerName: '',
profileImageFilename: '',
};
this.handleFileInputchange = this.handleFileInputchange.bind(this);
}
或者
handleFileInputchange = (e) => {
console.log(e);
this.setState({ profileImageFilename: e.target.file[0].filename });
}
您还可以安全地删除匿名回调并直接传递this.handleFileInputChange
,因为onChange
事件处理程序和您的回调具有相同的函数签名:
onChange={this.handleFileInputChange}
更新
我用你的代码创建了一个代码框。奇怪的是,我也没有看到任何 onChange 回调触发(控制台日志),我什componentDidUpdate
至添加了一个日志状态更新,这也不会触发。我可以添加一个新文件并查看带有我选择的文件名的文本工具提示,但 UI 中没有任何更新。如果您的引导输入仍然导致问题,我建议实施原始输入元素。
推荐阅读
- ios - 我在哪里可以找到应用内订阅的取消日期?
- canvas - 如何在 Apache NiFi 中管理、排序和过滤大量流?
- python - 从 STEP 文件中提取参数方程
- ios - 安装 GraphQL iOS 框架
- javascript - Colspan 以匹配上方根据屏幕尺寸隐藏 td 的行
- javascript - 使用回调时从函数中获取空数组
- c - 为什么在 x64dbg 中加载 .exe 文件时找不到“_main”符号?
- android - 无法查看在 android studio 的设计视图中删除的文本视图
- java - 使用比较器,如何确定对象之间的匹配质量,或者有更好的方法
- javascript - 如何在 Angular 4 中的表单中按下按钮时将表单状态更改为脏状态?