我有一个反应表单我试图使用Material UI <Input />
组件,但它正在注销undefined
并且无法在提交时获取表单字段值。使用普通的<input>
表单字段可以正常工作。我错过了什么,不允许我使用MUI <Input />
组件?我认为这可能是由于ref={(a) => this._inputElement = a}
的使用,但不确定替代方案。
class Contacts extends Component {
constructor(props) {
super(props);
this.state = {
items: []
};
this.addContact = this.addContact.bind(this);
this.deleteItem = this.deleteItem.bind(this);
}
addContact(e) {
if(this._inputElement.value !== "") {
var newItem = {
firstname: this._inputElement.value,
lastname: this._inputElement2.value,
phone: this._inputElement3.value,
key: Date.now()
};
this.setState((prevState) => {
return {
items: prevState.items.concat(newItem)
};
});
this._inputElement.value = "";
this._inputElement2.value = "";
this._inputElement3.value = "";
}
console.log(this.state.items);
e.preventDefault();
}
deleteItem(key) {
var filteredItems = this.state.items.filter(function (item) {
return (item.key !== key);
});
this.setState({
items: filteredItems
});
}
render () {
return (
<Tabs onChange={this.onChange} defaultSelectedIndex={0} justified={true}>
<Tab value="pane-1" label="Add Contact" onActive={this.onActive}>
<Panel>
<Form onSubmit={this.addContact}>
<Input ref={(a) => this._inputElement = a}
label="First Name" required={true} floatingLabel={true}/>
<Input ref={(a) => this._inputElement2 = a}
label="Last Name" required={true} floatingLabel={true}/>
<Input ref={(a) => this._inputElement3 = a}
label="Phone Number" type="number" required={true} floatingLabel={true}/>
<Button variant="raised">Add</Button>
</Form>
</Panel>
</Tab>
<Tab value="pane-2" label="List Contacts">
<Panel>
<ContactList entries={this.state.items} delete={this.deleteItem}/>
</Panel>
</Tab>
</Tabs>
);
}
}
export default Contacts
答案 0 :(得分:0)
我已更新您的代码并删除了参考号。所以我创建了一个handleChange,它将输入值保存到您可以访问的状态并将其推送到items数组
class Contacts extends Component {
constructor(props) {
super(props);
this.state = {
items: []
};
this.addContact = this.addContact.bind(this);
this.deleteItem = this.deleteItem.bind(this);
this.handleChange = this.handleChange.bind(this);
}
//Saves the user input into the state
handleChange(e) {
const {
name,
value
} = e.target;
if(value !== ""){
this.setState({
[name]: value
})
}
}
addContact(e) {
//Now you have access to firstname, lastname, phone in the state
var newItem = {
firstname: this.state.firstname,
lastname: this.state.lastname,
phone: this.state.phone,
key: Date.now()
};
this.setState((prevState) => {
return {
items: prevState.items.concat(newItem),
//reset the form after submission
firstname: "",
lastname: "",
phone: ""
};
});
}
console.log(this.state.items);
e.preventDefault();
}
deleteItem(key) {
var filteredItems = this.state.items.filter(function(item) {
return (item.key !== key);
});
this.setState({
items: filteredItems
});
}
render() {
return ( <
Tabs onChange = {
this.onChange
}
defaultSelectedIndex = {
0
}
justified = {
true
} >
<
Tab value = "pane-1"
label = "Add Contact"
onActive = {
this.onActive
} >
<
Panel >
<
Form onSubmit = {
this.addContact
} >
<
Input name = "firstname"
value={this.state.firstname}
onChange = {
this.handleChange
}
label = "First Name"
required = {
true
}
floatingLabel = {
true
}
/> <
Input name = "lastname"
value={this.state.lastname}
onChange = {
this.handleChange
}
label = "Last Name"
required = {
true
}
floatingLabel = {
true
}
/> <
Input name = "phone
value={this.state.phone}
onChange = {
this.handleChange
}
label = "Phone Number"
type = "number"
required = {
true
}
floatingLabel = {
true
}
/> <
Button variant = "raised" > Add < /Button> < /
Form > <
/Panel> < /
Tab > <
Tab value = "pane-2"
label = "List Contacts" >
<
Panel >
<
ContactList entries = {
this.state.items
}
delete = {
this.deleteItem
}
/> < /
Panel > <
/Tab> < /
Tabs >
);
}
}
export default Contacts
&#13;
答案 1 :(得分:0)
您缺少的是您输入的值和onChange道具。输入的值由状态控制。这是一个可以为您提供所需结果的工作示例:
class Contacts extends Component {
constructor(props) {
super(props)
this.state = {
items: []
}
this.addContact = this.addContact.bind(this)
this.deleteItem = this.deleteItem.bind(this)
}
addContact(e) {
//e.preventDefault should always be the first thing in the function
e.preventDefault()
//use state to access form values
var newItem = {
firstname: this.state.firstname,
lastname: this.state.lastname,
phone: this.state.phone,
key: Date.now()
}
//add newItem to items
let holder = this.state.items
holder.push(newItem)
this.setState({items: holder})
//reset form to blank values
this.setState({ firstname: '', lastName: '', phone: '' })
console.log(this.state.items)
}
deleteItem(key) {
var filteredItems = this.state.items.filter(function(item) {
return item.key !== key
})
this.setState({
items: filteredItems
})
}
//arrow functions automatically bind to this
handleChange = e => {
//destructuring to get the variables
let { name, value } = e.target
//setting state whith the value of "name" as the key
this.setState({ [name]: value })
}
render() {
return (
<Tabs onChange={this.onChange} defaultSelectedIndex={0} justified={true}>
<Tab value="pane-1" label="Add Contact" onActive={this.onActive}>
<Panel>
<Form onSubmit={this.addContact}>
<Input
value={this.state.firstname}
name="firstname"
onChange={this.handleChange}
label="First Name"
required={true}
floatingLabel={true}
/>
<Input
value={this.state.lastname}
name="lastname"
onChange={this.handleChange}
label="Last Name"
required={true}
floatingLabel={true}
/>
<Input
value={this.state.phone}
name="phone"
onChange={this.handleChange}
label="Phone Number"
type="number"
required={true}
floatingLabel={true}
/>
<Button variant="raised">Add</Button>
</Form>
</Panel>
</Tab>
<Tab value="pane-2" label="List Contacts">
<Panel>
<ContactList entries={this.state.items} delete={this.deleteItem} />
</Panel>
</Tab>
</Tabs>
)
}
}
导出默认联系人