admin管理员组文章数量:1401673
Code:
import React, { Component } from 'react';
import { Button, Input, Row, Col, Label } from 'reactstrap';
export default class Settings extends Component {
constructor(props) {
super(props);
this.state = {
tallyPort: '', panyYear: '', interval: '', timeRange: '',
databasePort: '', databaseUserName: '', databasePassword: ''
};
}
handleChange = (stateName, e) => {
this.setState({ stateName: e.target.value });
}
handleSave = () => {
console.log(this.state)
}
render() {
return(
<div className="dashboard" >
<Input name="tallyPort" onChange={this.handleChange.bind(this, 'tallyPort')} />
<Input name="panyYear" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="interval" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="timeRange" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="databasePort" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="databaseUserName" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="databasePassword" onChange={this.handleChange.bind(this, 'panyYear')} />
<Button style={{ width: '200px', marginLeft: '720px'}} onClick={this.handleSave.bind(this)} color="primary">Save</Button>
</div>
);
}
}
Main problem with this.setState
function, I'm not understand why it is not working.
I'm trying to set each state value on "onChange"
of input field, "setState"
is not working properly, when i'm console all states after given values, it returns blank values, any one help me?
Code:
import React, { Component } from 'react';
import { Button, Input, Row, Col, Label } from 'reactstrap';
export default class Settings extends Component {
constructor(props) {
super(props);
this.state = {
tallyPort: '', panyYear: '', interval: '', timeRange: '',
databasePort: '', databaseUserName: '', databasePassword: ''
};
}
handleChange = (stateName, e) => {
this.setState({ stateName: e.target.value });
}
handleSave = () => {
console.log(this.state)
}
render() {
return(
<div className="dashboard" >
<Input name="tallyPort" onChange={this.handleChange.bind(this, 'tallyPort')} />
<Input name="panyYear" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="interval" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="timeRange" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="databasePort" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="databaseUserName" onChange={this.handleChange.bind(this, 'panyYear')} />
<Input name="databasePassword" onChange={this.handleChange.bind(this, 'panyYear')} />
<Button style={{ width: '200px', marginLeft: '720px'}} onClick={this.handleSave.bind(this)} color="primary">Save</Button>
</div>
);
}
}
Main problem with this.setState
function, I'm not understand why it is not working.
I'm trying to set each state value on "onChange"
of input field, "setState"
is not working properly, when i'm console all states after given values, it returns blank values, any one help me?
2 Answers
Reset to default 6Basic Idea:
You need to use Computed property name concept, to use the expressions for object property names. For that you have to put the expression inside []
.
Solution:
You are passing the state
variable name in onChange
function, so you need to use []
because it will be a variable that will hold some state
variable name, then only it will update that state
variable.
If you don't use []
then, stateName
will be treated as a key (string), it will create a new state
variable with name stateName
and put the value in that.
Write it like this:
handleChange(stateName, e) {
this.setState({ [stateName]: e.target.value });
}
Check this:
let obj = {b: 5};
let a = 'b';
obj = {...obj, a: 20}; //similar to setState
obj = {...obj, [a]: 1};
console.log(obj);
As mentioned above the Computed property name is the key to this solution. This solution worked well for me.
constructor() {
super(props);
this.state = {tallyPort: '', panyYear: '', interval: '', timeRange: '',
databasePort: '', databaseUserName: '', databasePassword: ''};
this.handleChange = this.handleChange.bind(this);
}
// assigning the name and value is the main objective of this solution.
handleChange = (e) => {
this.setState({
[e.target.name]: e.target.value
})
}
render() {
return(
<div className="dashboard" >
<Input name="tallyPort" onChange={this.handleChange} />
<Input name="panyYear" onChange={this.handleChange} />
<Input name="interval" onChange={this.handleChange} />
<Input name="timeRange" onChange={this.handleChange} />
<Input name="databasePort" onChange={this.handleChange} />
<Input name="databaseUserName" onChange={this.handleChange} />
<Input name="databasePassword" onChange={this.handleChange} />
<Button style={{ width: '200px', marginLeft: '720px'}} onClick={this.handleSave.bind(this)} color="primary">Save</Button>
</div>
);
}
I hope this solution helps. Thanks.
本文标签: javascriptSingle onChange function for multiple input fields is not working ReactJSStack Overflow
版权声明:本文标题:javascript - Single onChange function for multiple input fields is not working: ReactJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744285778a2598870.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论