Tri*_*ani 1 javascript php login reactjs
我正在使用REACT JS作为前端和PHP作为后端创建登录表单。我正在尝试获取输入值。代码如下:
import React, { Component} from 'react';
import ReactDOM from 'react-dom';
import {Button, IconButton} from 'react-toolbox/lib/button';
import Input from 'react-toolbox/lib/input';
export default class Login extends React.Component {
constructor() {
super();
this.state = {email: ''};
this.state = {password: ''};
this.onSubmit = this.onSubmit.bind(this);
this.handleEmailChange = this.handleEmailChange.bind(this);
this.handlePasswordChange = this.handlePasswordChange.bind(this);
}
handleEmailChange(e) {
this.setState({email: e.target.value});
}
handlePasswordChange(e) {
this.setState({password: e.target.value});
}
onSubmit() {
fetch('http://xyz/check-login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: this.state.email,
password: this.state.password,
})
})
}
Run Code Online (Sandbox Code Playgroud)
形式如下:
<form name="Login">
<Input type="text" name="email" value={this.state.email} placeholder="Email Id" className="form-control" onChange={this.handleEmailChange} />
<Input name="password" value={this.state.password} placeholder="Password" type="password" className="form-control m-b-10" onChange={this.handlePasswordChange} />
<Button type="button" className="m-t-20 orange" label="Sign in " onClick={this.onSubmit} />
</form>
Run Code Online (Sandbox Code Playgroud)
但是出现以下错误:
未捕获的TypeError:无法读取未定义的属性“值”
我正在使用react-toolbox。所以,我正在使用https://github.com/react-toolbox/react-toolbox/blob/dev/components/input/Input.js中的组件和https://github.com/react-toolbox/react中的组件-toolbox / blob / dev / components / button / Button.js。
首先将您的this.statein 构造函数更改为 single - this.state = {emai:'',password:''},然后尝试在构造函数中绑定handleEmailChange和handlePasswordChange输入内部,您需要将this直接设置为输入,
更新
或者如果Input和Button是组件,你需要在其中实现 changeMethod 和 onChange 事件,并Login通过回调将值发送回组件
它是如何工作的-
class Input extends React.Component{
constructor(props){
super(props);
this.state= {
value : this.props.value
}
}
componentWillReceiveProps(nextProps){
this.setState({
value: nextProps.value,
})
}
render(){
return(
<input onChange={this.handleChangeValue.bind(this)} type="text" name={this.props.name} value={this.state.value} placeholder={this.props.placeholder} className={**just class name or send via props too**} />
)
}
handleChangeValue(e){
this.setState({value:e.target.value});
this.props.changeValue(e.target.value);
}
}
class Login extends React.Component{
constructor(props){
super(props);
this.state= {
emailValue : '',
passwordValue: '',
...
}
}
render(){
return(
<div>
<Input type="text" name='email' value={this.state.emailValue} placeholder={'Write email'} className='someName' changeValue={this.changeEmailValue.bind(this)} />
<Input type="text" name='password' value={this.state.passwordValue} placeholder={'Write password'} className='someName' changeValue={this.changePasswordValue.bind(this)} />
</div>
)
}
changeEmailValue(value){
this.setState({emailValue:value});
}
changePasswordValue(value){
this.setState({passwordValue:value});
}
}
Run Code Online (Sandbox Code Playgroud)
首先,是什么<Input ..../> and <Button .../>?它们是您的组件还是仅是表单输入字段?
我想它们只是形式字段,因此它们必须小写<input ..../> , <button .../>。
尝试将您的函数绑定到render中,例如:this.functionName.bind(this)。
这是一个工作代码:
class Test extends React.Component {
constructor(props){
super(props);
this.state = {
email: '',
password: '',
};
}
handleEmailChange(e) {
this.setState({email: e.target.value});
}
handlePasswordChange(e) {
this.setState({password: e.target.value});
}
render(){
return (
<div>
<form name="Login">
<input type="text" name="email" value={this.state.email} placeholder="Email Id" className="form-control" onChange={this.handleEmailChange.bind(this)} />
<input name="password" value={this.state.password} placeholder="Password" type="password" className="form-control m-b-10" onChange={this.handlePasswordChange.bind(this)} />
<button type="button" className="m-t-20 orange" label="Sign in " onClick={this.onSubmit}>Sign in</button>
</form>
</div>
)
}
}
React.render(<Test />, document.getElementById('container'));
Run Code Online (Sandbox Code Playgroud)
更新
constructor(props){
super(props);
this.state = {
name: '',
email: ''
}
}
handleChange(name, value){
let state = this.state;
state[name] = value;
this.setState({state});
}
render () {
return (
<section>
<Input type='text' label='Name' name='name' value={this.state.name} onChange={this.handleChange.bind(this, 'name')} maxLength={16} />
<Input type='email' label='Email address' icon='email' value={this.state.email} onChange={this.handleChange.bind(this, 'email')} />
</section>
);
}
Run Code Online (Sandbox Code Playgroud)
我不确定它是如何工作的,但是它将名称和值作为参数传递给handleChange函数,因此,您可以将值作为第二个参数。您不需要事件。
| 归档时间: |
|
| 查看次数: |
18351 次 |
| 最近记录: |