ReactJS Semantic ui - 当值属性存在时无法输入表单

华硕

我正在按照 ReactJS 教程设置登录表单。使用和导入语义ui。电子邮件和密码被传递到value表单内的属性中。发生这种情况时,我无法在表单中输入任何内容。一旦我删除它,我就可以输入信息,但我认为它不会被传递到任何地方。

似乎在其他任何地方都找不到这个问题。有没有人遇到过这个问题?

import React from 'react';
import PropTypes from 'prop-types';
import { Form, Button } from 'semantic-ui-react';
import Validator from 'validator';
import InlineError from '../messages/InlineError';



class LoginForm extends React.Component {
    state = {
        data: {
            email: "",
            password: ""
        },
        loading: false,
        errors: {}
    };

    //... is called spread
    onChange = e => this.setState({
        data: {...this.state.data, [e.target.name]: e.target.value }
    });

    //() means function takes no params
    onSubmit = () => {
        const errors = this.validate(this.state.data);
        this.setState({errors}); //if there are errors, display them
        if(Object.keys(errors).length === 0){
            this.props.submit(this.state.data);
        }
    };

    validate = (data) => {
        const errors = {};
        if(!Validator.isEmail(data.email))
            errors.email = "Invalid email";
        if(!data.password)
            errors.password = "Can't be blank";

        return errors;
    };

    render() {

        const { data, errors } = this.state; // import variables into html
        return (
            <div>
                <Form onSubmit={ this.onSubmit }>
                    <Form.Field error={!!errors.email}>
                        <label htmlFor="email">Email</label>
                        <input type="email"
                               id="email"
                               placeholder="[email protected]"
                               value={ data.email }
                               onChange={ this.onChange }/>
                        {errors.email && <InlineError text={errors.email}/>}
                    </Form.Field>
                    <Form.Field error={!!errors.email}>
                        <label htmlFor="password">Password</label>
                        <input type="password"
                               id="password"
                               value={ data.password }
                               onChange={this.onChange}/>
                        {errors.password && <InlineError text={errors.password}/>}
                    </Form.Field>
                    <Button primary>Login</Button>
                </Form>
            </div>
        );

    }
}

LoginForm.propTypes = {
    submit: PropTypes.func.isRequired
};



export default LoginForm;

教程:https : //www.youtube.com/watch?v=NO2DaxhoWHk&t=879s

阿格尼
onChange = e => this.setState({
    data: {...this.state.data, [e.target.name]: e.target.value }
});

此函数将状态设置为共享输入字段名称的变量。因此e.target.name但是您的输入字段没有 name 属性。

您可以使用以下方法解决此问题:

import React from 'react';
import PropTypes from 'prop-types';
import { Form, Button } from 'semantic-ui-react';
import Validator from 'validator';
import InlineError from '../messages/InlineError';



class LoginForm extends React.Component {
    state = {
        data: {
            email: "",
            password: ""
        },
        loading: false,
        errors: {}
    };

    //... is called spread
    onChange = e => this.setState({
        data: {...this.state.data, [e.target.name]: e.target.value }
    });

    //() means function takes no params
    onSubmit = () => {
        const errors = this.validate(this.state.data);
        this.setState({errors}); //if there are errors, display them
        if(Object.keys(errors).length === 0){
            this.props.submit(this.state.data);
        }
    };

    validate = (data) => {
        const errors = {};
        if(!Validator.isEmail(data.email))
            errors.email = "Invalid email";
        if(!data.password)
            errors.password = "Can't be blank";

        return errors;
    };

    render() {

        const { data, errors } = this.state; // import variables into html
        return (
            <div>
                <Form onSubmit={ this.onSubmit }>
                    <Form.Field error={!!errors.email}>
                        <label htmlFor="email">Email</label>
                        <input type="email"
                               id="email"
                               name="email"
                               placeholder="[email protected]"
                               value={ data.email }
                               onChange={ this.onChange }/>
                        {errors.email && <InlineError text={errors.email}/>}
                    </Form.Field>
                    <Form.Field error={!!errors.email}>
                        <label htmlFor="password">Password</label>
                        <input type="password"
                               id="password"
                               name="password"
                               value={ data.password }
                               onChange={this.onChange}/>
                        {errors.password && <InlineError text={errors.password}/>}
                    </Form.Field>
                    <Button primary>Login</Button>
                </Form>
            </div>
        );

    }
}

LoginForm.propTypes = {
    submit: PropTypes.func.isRequired
};



export default LoginForm;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 Semantic-UI,如何将表单字段居中对齐?

来自分类Dev

从列表中单击时,semantic-ui multiselect下拉列表选择项

来自分类Dev

当通过 Ajax 检索项目时,将自定义数据属性添加到 Semantic UI Dropdown

来自分类Dev

Responsive replacement for Semantic UI's navigation menu

来自分类Dev

Semantic-UI 的搜索元素返回 TypeError

来自分类Dev

ReactJS Material UI输入库输入提取-未定义

来自分类Dev

React CSS Webpack Hashing & Usage with semantic-ui-sass, semantic-ui-react

来自分类Dev

使用Appbar +抽屉(材料UI + ReactJS)时遇到问题

来自分类Dev

使用reactjs和Material UI单击按钮时更改内容

来自分类Dev

使用Appbar +抽屉(材料UI + ReactJS)时遇到问题

来自分类Dev

有没有一种方法可以为Semantic UI React Dropdown提供html“ name”属性,使其表现得像表单提交中的普通输入元素?

来自分类Dev

如何将Semantic-UI添加到Phoenix

来自分类Dev

使用Semantic-UI的最低JQuery版本是什么

来自分类Dev

使用Semantic-UI的div的垂直对齐

来自分类Dev

使用Webpack反应Semantic-ui捆绑包的大小太大

来自分类Dev

Internet Explorer 9中的Semantic-UI 2.0?

来自分类Dev

流星中的Semantic-ui复选框

来自分类Dev

React和Semantic-ui-react,代理事件

来自分类Dev

修复了 react-semantic-ui 中的侧边栏

来自分类Dev

Semantic-ui-react:搜索结果不可见

来自分类Dev

Semantic-UI Dropdown 也在“数据值”中搜索

来自分类Dev

如何使用 Semantic-UI 中的占位符元素?

来自分类Dev

材质 UI reactjs 中的 ListItem

来自分类Dev

提交时表单无法重置 ReactJS Flux

来自分类Dev

无法提交表单 - ReactJs

来自分类Dev

Material UI 表单输入的 className 属性

来自分类Dev

reactjs-redux表单和材料ui框架-自动完成字段

来自分类Dev

如何使用 reactJS 和 typescript 验证结构 UI 中提交表单上的多个文本字段?

来自分类Dev

语义UI表单验证-仅当值不为空时才验证某些表单字段

Related 相关文章

  1. 1

    使用 Semantic-UI,如何将表单字段居中对齐?

  2. 2

    从列表中单击时,semantic-ui multiselect下拉列表选择项

  3. 3

    当通过 Ajax 检索项目时,将自定义数据属性添加到 Semantic UI Dropdown

  4. 4

    Responsive replacement for Semantic UI's navigation menu

  5. 5

    Semantic-UI 的搜索元素返回 TypeError

  6. 6

    ReactJS Material UI输入库输入提取-未定义

  7. 7

    React CSS Webpack Hashing & Usage with semantic-ui-sass, semantic-ui-react

  8. 8

    使用Appbar +抽屉(材料UI + ReactJS)时遇到问题

  9. 9

    使用reactjs和Material UI单击按钮时更改内容

  10. 10

    使用Appbar +抽屉(材料UI + ReactJS)时遇到问题

  11. 11

    有没有一种方法可以为Semantic UI React Dropdown提供html“ name”属性,使其表现得像表单提交中的普通输入元素?

  12. 12

    如何将Semantic-UI添加到Phoenix

  13. 13

    使用Semantic-UI的最低JQuery版本是什么

  14. 14

    使用Semantic-UI的div的垂直对齐

  15. 15

    使用Webpack反应Semantic-ui捆绑包的大小太大

  16. 16

    Internet Explorer 9中的Semantic-UI 2.0?

  17. 17

    流星中的Semantic-ui复选框

  18. 18

    React和Semantic-ui-react,代理事件

  19. 19

    修复了 react-semantic-ui 中的侧边栏

  20. 20

    Semantic-ui-react:搜索结果不可见

  21. 21

    Semantic-UI Dropdown 也在“数据值”中搜索

  22. 22

    如何使用 Semantic-UI 中的占位符元素?

  23. 23

    材质 UI reactjs 中的 ListItem

  24. 24

    提交时表单无法重置 ReactJS Flux

  25. 25

    无法提交表单 - ReactJs

  26. 26

    Material UI 表单输入的 className 属性

  27. 27

    reactjs-redux表单和材料ui框架-自动完成字段

  28. 28

    如何使用 reactJS 和 typescript 验证结构 UI 中提交表单上的多个文本字段?

  29. 29

    语义UI表单验证-仅当值不为空时才验证某些表单字段

热门标签

归档