admin管理员组文章数量:1289542
I am attempting to use two methods, handleChange and handleSubmit for a login page in react. I have tried to set two values for username and password within state, update the values in state whenever the input are of the form is modified, then submit using the values stored in state. However, my values return undefined when printed to console. (p.s. I am aware I still yet need to encrypt password for all you security gurus).
I'm new to react so there may be an issue with my logic:
Login.js
export default class Login extends React.Component {
constructor(props) {
super(props);
this.state = {uname: '', password: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({uname: event.target.uname, password: event.target.password});
}
handleSubmit(event) {
alert('A username and password was submitted: ' + this.state.uname + this.state.password);
event.preventDefault();
fetch('https://localhost:8080/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
uname: this.state.uname,
password: this.state.password,
})
});
}
render() {
return (
<div>
<Header titleName={"Login"}>
<div className="container">
<div className="card"/>
<div className="card">
<h1 className="title">Login</h1>
<form onSubmit={this.handleSubmit}>
<div className="input-container">
<input name="uname" type="text" value={this.state.uname} id="#uname" required="required"
onChange={this.handleChange}/>
<label form="#unamelabel">Username</label>
<div className="bar"/>
</div>
<div className="input-container">
<input name="password" type="password" value={this.state.password} id="#pass" required="required"
onChange={this.handleChange}/>
<label form="#passlabel">Password</label>
<div className="bar"/>
</div>
<div className="button-container">
<button type="submit" value="Submit"><span>Go</span></button>
</div>
<div className="footer"><a href="#">Forgot your password?</a></div>
</form>
</div>
</div>
</Header>
<Footer/>
</div>
);
}
}
I am attempting to use two methods, handleChange and handleSubmit for a login page in react. I have tried to set two values for username and password within state, update the values in state whenever the input are of the form is modified, then submit using the values stored in state. However, my values return undefined when printed to console. (p.s. I am aware I still yet need to encrypt password for all you security gurus).
I'm new to react so there may be an issue with my logic:
Login.js
export default class Login extends React.Component {
constructor(props) {
super(props);
this.state = {uname: '', password: ''};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({uname: event.target.uname, password: event.target.password});
}
handleSubmit(event) {
alert('A username and password was submitted: ' + this.state.uname + this.state.password);
event.preventDefault();
fetch('https://localhost:8080/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
uname: this.state.uname,
password: this.state.password,
})
});
}
render() {
return (
<div>
<Header titleName={"Login"}>
<div className="container">
<div className="card"/>
<div className="card">
<h1 className="title">Login</h1>
<form onSubmit={this.handleSubmit}>
<div className="input-container">
<input name="uname" type="text" value={this.state.uname} id="#uname" required="required"
onChange={this.handleChange}/>
<label form="#unamelabel">Username</label>
<div className="bar"/>
</div>
<div className="input-container">
<input name="password" type="password" value={this.state.password} id="#pass" required="required"
onChange={this.handleChange}/>
<label form="#passlabel">Password</label>
<div className="bar"/>
</div>
<div className="button-container">
<button type="submit" value="Submit"><span>Go</span></button>
</div>
<div className="footer"><a href="#">Forgot your password?</a></div>
</form>
</div>
</div>
</Header>
<Footer/>
</div>
);
}
}
Share
Improve this question
asked May 31, 2018 at 18:44
Joss MTJoss MT
2991 gold badge6 silver badges17 bronze badges
4 Answers
Reset to default 2Replace handle change with this
handleInputChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
You need to set value of the inputs to the state, not the name of the inputs
I think the handleChange should setState should have event.tagert.name = event.target.value:
handleChange(evt) {
this.setState({
[evt.target.name]: evt.target.value,
});
}
If you want to get the value of an input inside a react statefull ponent, you should set the ref property of this input like this:
<input name="username" ref={node => this.username = node} />
then calling this.username will return you the actual value of username input.
You don't want to set the state in a submit action since this makes your ponent rerender each times a letter push your input value.
More in the react doc
The problem lies with handleChange function. In the code posted above, there are two distinct 'event' objects associated with the two form inputs. Each one of them have their own set of properties. For example: 'event.target.name' stores the 'name' attribute specified by 'input' tag while 'event.target.value' stores the data entered by the user. So, change the implementation of handleChange to either
(A) Verbose
handleChange(event) {
if (event.target.name == "uname") {
this.setState({
uname: event.target.value
});
}
if (event.target.name == "password") {
this.setState({
password: event.target.value
});
}
}
where you can match the 'name' attribute to either uname or password and set state accordingly.
(B) Succinct
handleChange(event) {
this.setState({
[event.target.name]: event.target.value
});
}
本文标签: javascriptReact passing value through state on handle changeStack Overflow
版权声明:本文标题:javascript - React passing value through state on handle change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741419728a2377729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论