我遇到了 TypeError 问题:this.setstate is not a function。我正在使用 react 创建一个具有登录和注册页面的应用程序,两个页面都有电子邮件、名称和密码输入字段,在字段中输入值时会立即发生错误。下面是登录页面中我的代码示例,我如何解决这个问题我已经尽了最大努力,但似乎我无法弄清楚我的代码有什么问题,因为它已成功编译,输入中没有任何其他错误.我也不太擅长编码,我目前正在学习。
`从“反应”导入反应;
class Signin extends React.Component {
constructor(props) {
super(props);
this.state = {
signInEmali: '',
signInPassword: '',
};
}
onEmaliChange = (Event) => {
this.setstate({ signInEmali: Event.target.value });//the error happens here
};
onPasswordChange = (Event) => {
this.setstate({ signInPassword: Event.target.value });//the error happens here
};
onSubmitSignIn = () => {
fetch('http://localhost:3001/signin', {
method: 'post',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({
email: this.state.signInEmali,
password: this.state.signInPassword,
}),
})
.then((Response) => Response.json())
.then((data) => {
if (data === 'success') {
this.props.onRouteChange('home');
}
});
};
render() {
const { onRouteChange } = this.props;
return (
<article className="br3 b--black-10 mv4 w-100 w-50-m w-25-l mw6 shadow-5 center">
<main className="pa4 black-80">
<div className="measure">
<fieldset id="sign_up" className="ba b--transparent ph0 mh0">
<legend className="f1 fw6 ph0 mh0">Sign In</legend>
<div className="mt3">
<label className="db fw6 lh-copy f6" htmlFor="email-address">
Email
</label>
<input
className="pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100"
type="email"
name="email-address"
id="email-address"
onChange={this.onEmaliChange}
/>
</div>
<div className="mv3">
<label className="db fw6 lh-copy f6" htmlFor="password">
Password
</label>
<input
className="b pa2 input-reset ba bg-transparent hover-bg-black hover-white w-100"
type="password"
name="password"
id="password"
onChange={this.onPasswordChange}
/>
</div>
</fieldset>
<div className="">
<input
onClick={this.onSubmitSignIn}
className="b ph3 pv2 input-reset ba b--black bg-transparent grow pointer f6 dib"
type="submit"
value="Sign in"
/>
</div>
<div className="lh-copy mt3">
<p
onClick={() => onRouteChange('register')}
className="f6 link dim black db pointer">
Register
</p>
</div>
</div>
</main>
</article>
);
}
}
export default Signin;
`
答案 0 :(得分:0)
只需将代码中的 setstate
更改为 setState
即可正常工作