Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs Js-FormControl不';我不能更新我的价值_Reactjs_State_Form Control - Fatal编程技术网

Reactjs Js-FormControl不';我不能更新我的价值

Reactjs Js-FormControl不';我不能更新我的价值,reactjs,state,form-control,Reactjs,State,Form Control,我目前正在使用我的react应用程序的登录端,并且在从输入端更新状态数据时遇到问题 我有这个部分: import React from 'react' import { login } from '../../api/Authentication' import { setStore } from '../../webapp/storage' import { Button, ControlLabel, Form, FormControl, FormGroup } from 'react-boo

我目前正在使用我的react应用程序的登录端,并且在从输入端更新状态数据时遇到问题

我有这个部分:

import React from 'react'
import { login } from '../../api/Authentication'
import { setStore } from '../../webapp/storage'
import { Button, ControlLabel, Form, FormControl, FormGroup } from 'react-bootstrap';


export default class LoginPage extends React.Component {

    constructor(props){
        super(props);
        this.state={
            email:'',
            password:''
        }
    }

    handleSubmit(event) {

        if (this.state.email == '' || this.state.password == '') {
            if (this.state.email == '') {
                this.badInfosAlert('Email field empty')
            } else if (this.state.password == '') {
                this.badInfosAlert('Password field empty')
            }
            return
        }

        console.log('-----------------------')
        console.log(this.state.email)
        console.log(this.state.password)
        var user = {
            email: this.state.email,
            password: this.state.password
        }
        console.log(JSON.stringify(user))
        console.log('-----------------------')

        login(user).then(result => {
            if (result != null && result.status == 200) {
                setStore('token', result.json.user.token)
            } else {
                this.badInfosAlert(result.json.error)
            }
        }).catch(this.badInfosAlert('An error happend'));
    }

    badInfosAlert(message) {
        console.log(message);
        alert(message);
    }

    render() {
        return (
            <div className='col-lg-12'>
                <Form>
                    <FormGroup controlId="formHorizontalEmail">
                        <ControlLabel>Email </ControlLabel>
                        <FormControl type="username" onChange = {(event,newValue) => this.setState({email:newValue})} placeholder="Email" />
                    </FormGroup>
                    <FormGroup controlId="formHorizontalPassword">
                        <ControlLabel>Password </ControlLabel>
                        <FormControl type="password" onChange = {(event,newValue) => this.setState({password:newValue})} placeholder="Password" />
                    </FormGroup>
                    <Button onClick={(event) => this.handleSubmit(event)}>Login</Button>
                </Form>
            </div>
        )
    }
}
从“React”导入React
从“../../api/Authentication”导入{login}
从“../../webapp/storage”导入{setStore}
从“react bootstrap”导入{Button,ControlLabel,Form,FormControl,FormGroup};
导出默认类LoginPage扩展React.Component{
建造师(道具){
超级(道具);
这个州={
电子邮件:“”,
密码:“”
}
}
handleSubmit(事件){
if(this.state.email=''| | this.state.password=''){
如果(this.state.email==''){
this.badInfosAlert('电子邮件字段为空')
}else if(this.state.password==''){
this.badInfosAlert('密码字段为空')
}
返回
}
console.log(“--------------------------”)
console.log(this.state.email)
console.log(this.state.password)
变量用户={
电子邮件:this.state.email,
密码:this.state.password
}
log(JSON.stringify(用户))
console.log(“--------------------------”)
登录(用户)。然后(结果=>{
if(result!=null&&result.status==200){
setStore('token',result.json.user.token)
}否则{
this.badInfosAlert(result.json.error)
}
}).catch(this.badInfosAlert(“发生错误”);
}
巴迪福萨勒特(留言){
控制台日志(消息);
警报(信息);
}
render(){
返回(
电子邮件
this.setState({email:newValue})占位符=“email”/>
密码
this.setState({password:newValue})占位符=“password”/>
this.handleSubmit(event)}>Login
)
}
}
问题是,每次我提交clic时,我的状态电子邮件/密码都是空的,即使字段已填充,为什么

我对JavaScript仍然非常熟悉(不仅仅是react),所以请尽可能多地解释您的答案:)

谢谢

像这样使用它

 <FormControl type="username" onChange = {(event) => this.setState({email:event.target.value})} placeholder="Email" />
this.setState({email:event.target.value})占位符=“email”/>
现在,它应该起作用了。基本上,附加到输入框的值是 在event.target.value中可用

像这样使用它

 <FormControl type="username" onChange = {(event) => this.setState({email:event.target.value})} placeholder="Email" />
this.setState({email:event.target.value})占位符=“email”/>
现在,它应该起作用了。基本上,附加到输入框的值是 在event.target.value中可用


正如您在的文档中所看到的,您将从指定给函数的事件对象中获得新值。因此,您的代码应该如下所示:

<FormControl type="username" onChange = {(event) => this.setState({email: event.target.value })} placeholder="Email" />
this.setState({email:event.target.value})占位符=“email”/>

对你的其他输入做同样的处理,一切都会很好。据我所知,react引导程序中的FormControls不会为您提供第二个参数“newValue”。

正如您在的文档中所看到的,您将从提供给函数的事件对象中获得newValue。因此,您的代码应该如下所示:

<FormControl type="username" onChange = {(event) => this.setState({email: event.target.value })} placeholder="Email" />
this.setState({email:event.target.value})占位符=“email”/>

对你的其他输入做同样的处理,一切都会很好。据我所知,react bootstrap中的FormControls不会给您第二个参数“newValue”。

谢谢您的回答!我误解了,并尝试了在StackOverflow上看到的一个解决方案,人们都说它有效。我想我想念这两个。。。谢谢通常,所有html输入标记都返回一个事件对象,您可以从中获取值。但在一些定制的react组件中,它们实际上将值作为第二个参数给出,这在我看来并不是很直观。谢谢你说得对!我误解了,并尝试了在StackOverflow上看到的一个解决方案,人们都说它有效。我想我想念这两个。。。谢谢通常,所有html输入标记都返回一个事件对象,您可以从中获取值。但在一些定制的react组件中,它们实际上将值作为第二个参数给出,在我看来,这并不是真正直观的。