我在下面的代码中使用开关盒比较对象键和字符串:
import { TextField, Button } from "@material-ui/core";
import React, { Component, ReactNode } from "react";
import classes from "./Contact.module.scss";
class contactForm extends Component {
state = {
contactForm: {
name: "",
email: "",
message: "",
phone: ""
}
};
render(): ReactNode {
return (
<form className={classes.ArticleBody}>
<div className={classes.ContactForm}>
<TextField
value={this.state.contactForm.name}
onChange={event => this._inputChangeHandler(event, "name")}
label="Full Name"
required
/>
<TextField
value={this.state.contactForm.email}
onChange={event => this._inputChangeHandler(event, "email")}
type="Email"
label="Email"
required
/>
<TextField
value={this.state.contactForm.phone}
onChange={event => this._inputChangeHandler(event, "phone")}
type="phone"
label="Phone Number"
required
/>
<TextField
type="textarea"
value={this.state.contactForm.message}
label="Comment/Message"
rows="4"
onChange={event => this._inputChangeHandler(event, "message")}
multiline
required
/>
</div>
<div className={classes.Submit}>
<Button type="submit" onClick={this._submitContactForm}>
Submit
</Button>
</div>
</form>
);
}
private _inputChangeHandler = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>, key: string) => {
const updatedFormData = { ...this.state.contactForm };
switch (key) {
case "email":
updatedFormData.email = event.target.value;
break;
case "phone":
updatedFormData.phone = event.target.value;
break;
case "message":
updatedFormData.message = event.target.value;
break;
case "name":
updatedFormData.name = event.target.value;
break;
}
this.setState({ contactForm: updatedFormData });
};
private _submitContactForm = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void => {
event.preventDefault();
console.log(this.state.contactForm);
};
}
export default contactForm;
我不想将我的对象键与switch
大小写进行比较。是否存在任何通用方法来更改已定义状态的输入更改的值?
例如:在下面的代码中,我试图匹配方法_inputChangeHandler
中参数的键,但是会引发错误
元素隐式具有“ any”类型,因为类型的表达式 'string'不能用于索引类型'{name:string;电子邮件:字符串; 讯息:字串;电话:字符串; }'
const updatedFormData = { ...this.state.contactForm };
updatedFormData[key] = event.target.value;
this.setState({ contactForm: updatedFormData });
谢谢
答案 0 :(得分:3)
尝试这样
this.setState({ contactForm: {...this.state.contactForm, [key]: event.target.value} });
@Ajay Verma
您可以设置name
的{{1}}属性,然后从TextField
获取密钥。
像这样
event
答案 1 :(得分:2)
TextField有一个名称道具。
当您需要处理多个受控输入元素时,可以 为每个元素添加一个名称属性,并让处理函数 根据event.target.name的值选择要执行的操作。 React Docs
例如:
<TextField
name="name"
value={this.state.contactForm.name}
onChange={this._inputChangeHandler}
label="Full Name"
required
/>
....
public _inputChangeHandler(e) {
this.setState((prevState) => {
return {
contactForm: {
...prevState.contactForm,
[e.target.name] : e.target.value
}
}
});
}
答案 2 :(得分:1)
一般的inputChangeHandler可以是:
private _inputChangeHandler = (
event: React.ChangeEvent<
HTMLInputElement | HTMLTextAreaElement
>,
key: string
) => {
this.setState({
contactForm: {
...this.state.contactForm,
[key]: event.target.value,
},
});
};
您的原始代码可以使用,但是在使用方括号表示法访问属性时,您必须输入typescript a hint:
const contactForm: {
[key: string]: string;
} = {
...this.state.contactForm,
};
contactForm[key] = event.target.value;
this.setState({
contactForm,
});