如何在打字稿中从 ReactJs 中的 inputText 更改传递键值?
How to Pass Key value from inputText change in ReactJs in typescript?
我在下面的代码中使用 switch case 来比较对象键和字符串:
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
中参数的键,但它抛出错误
Element implicitly has an 'any' type because expression of type
'string' can't be used to index type '{ name: string; email: string;
message: string; phone: string; }'
const updatedFormData = { ...this.state.contactForm };
updatedFormData[key] = event.target.value;
this.setState({ contactForm: updatedFormData });
谢谢
这样试试
this.setState({ contactForm: {...this.state.contactForm, [key]: event.target.value} });
@Ajay Verma
您可以设置 TextField
的 name
属性,然后您可以从 event
.
中获取密钥
像这样
...
private _inputChangeHandler = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const key = event.target.name;
...
<TextField
value={this.state.contactForm.phone}
name="phone"
onChange={this._inputChangeHandler}
type="phone"
label="Phone Number"
required
/>
...
一个通用的 inputChangeHandler 可以是:
private _inputChangeHandler = (
event: React.ChangeEvent<
HTMLInputElement | HTMLTextAreaElement
>,
key: string
) => {
this.setState({
contactForm: {
...this.state.contactForm,
[key]: event.target.value,
},
});
};
您的原始代码可以工作,但在使用括号表示法访问 属性 时必须提供 :
const contactForm: {
[key: string]: string;
} = {
...this.state.contactForm,
};
contactForm[key] = event.target.value;
this.setState({
contactForm,
});
TextField 有一个名称道具。
When you need to handle multiple controlled input elements, you can
add a name attribute to each element and let the handler function
choose what to do based on the value of 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
}
}
});
}
我在下面的代码中使用 switch case 来比较对象键和字符串:
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
中参数的键,但它抛出错误
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ name: string; email: string; message: string; phone: string; }'
const updatedFormData = { ...this.state.contactForm };
updatedFormData[key] = event.target.value;
this.setState({ contactForm: updatedFormData });
谢谢
这样试试
this.setState({ contactForm: {...this.state.contactForm, [key]: event.target.value} });
@Ajay Verma
您可以设置 TextField
的 name
属性,然后您可以从 event
.
像这样
...
private _inputChangeHandler = (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
const key = event.target.name;
...
<TextField
value={this.state.contactForm.phone}
name="phone"
onChange={this._inputChangeHandler}
type="phone"
label="Phone Number"
required
/>
...
一个通用的 inputChangeHandler 可以是:
private _inputChangeHandler = (
event: React.ChangeEvent<
HTMLInputElement | HTMLTextAreaElement
>,
key: string
) => {
this.setState({
contactForm: {
...this.state.contactForm,
[key]: event.target.value,
},
});
};
您的原始代码可以工作,但在使用括号表示法访问 属性 时必须提供
const contactForm: {
[key: string]: string;
} = {
...this.state.contactForm,
};
contactForm[key] = event.target.value;
this.setState({
contactForm,
});
TextField 有一个名称道具。
When you need to handle multiple controlled input elements, you can add a name attribute to each element and let the handler function choose what to do based on the value of 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
}
}
});
}