TypeError:无法将未定义或null转换为对象React Validation

时间:2019-06-11 11:53:40

标签: reactjs

我已经使用过React验证库

https://www.npmjs.com/package/react-validation

如果我从任何其他页面访问我的页面,都不会出现任何错误。但是当我刷新页面时,它给出了一个错误

  

TypeError:无法将未定义或null转换为对象

如果有人以前遇到过此问题,请提出一个想法。

代码:

import React from 'react';

import {
    Card,
    CardBody,
    CardTitle,
    Row,
    Col,
    FormGroup,
    Label,
    Alert
  } from 'reactstrap';
import Input from 'react-validation/build/input';
import Button from 'react-validation/build/button';
import Form from 'react-validation/build/form';
import axios from 'axios';
import api from '../../config/api';
import messages from '../../config/messages';
import Select from 'react-validation/build/select';
import CKEditor from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';

const required = (value, props,components) => {

    if(components.formReset!=undefined)
    {
        if (!components.formReset[0].value && (!value || (props.isCheckable && !props.checked))) {

        return <span className="text-danger is-visible">{messages.required}</span>;
        }
        else if(components.formReset[0].value)
        {
            return <span className="text-danger is-visible"></span>;
        }
    }

}
;


class View extends React.Component {
    constructor(props) {
        super(props);
        this.onInputChange = this.onInputChange.bind(this);
        this.sendReply = this.sendReply.bind(this);
        this.toggle = this.toggle.bind(this);
        this.onStatusChange=this.onStatusChange.bind(this);
        this.handleEditorChange=this.handleEditorChange.bind(this);
        this.state = {
             response:null,
             loading: false,
             message:'',
             status:'',
             attachment1:[],
             status_list:[],
             formReset:true

         };

    }


    componentDidMount() {

        this.setState({formReset:true});


    }

    onStatusChange(event) {
        this.setState({"formReset":false});
        this.setState({
                status: event.target.value
            });

    }

    handleEditorChange(data) {
        this.setState({"formReset":false});
        this.setState({ message:data });
    }

    sendReply()
    {

            /*** code after form submission***/

    }

    toggle() {


    }

    onInputChange(event) {
        event.preventDefault();
        this.setState({"formReset":false});
        this.setState({
                [event.target.name]: event.target.value
            });


    }




    render() {
        var _this = this;
        return (
            <aside className="customizer">
                {/*--------------------------------------------------------------------------------*/}
                {/* Toggle Customizer From Here                                                    */}
                {/*--------------------------------------------------------------------------------*/}

                <div className="customizer-body pt-3">
                <div>

                    {/* --------------------------------------------------------------------------------*/}
                    {/* Start Inner Div*/}
                    {/* --------------------------------------------------------------------------------*/}
                    <Row>

                    <Col md="12">
                        <Card>
                            <CardTitle className=" border-bottom p-3 mb-0">

                            </CardTitle>
                            <CardBody>

                                <Form ref={c => {
                                    this.replyform = c;
                                    }}>


                                    <Input type="hidden" name="formReset" id="formReset" value={this.state.formReset} />

                                    <FormGroup>
                                        <Row>
                                            <Label sm="2">Reply *</Label>
                                            <Col sm="10">

                                                <CKEditor
                                                    editor={ ClassicEditor }
                                                    data={this.state.message}
                                                    config={ {

                                                        toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote' ]
                                                    } }
                                                    onChange={ ( event, editor ) => {
                                                        const data = editor.getData();
                                                        this.handleEditorChange(data);

                                                    } }

                                                />

                                            </Col>
                                        </Row>
                                    </FormGroup>
                                    <FormGroup>
                                        <Row>
                                            <Label sm="2">Status</Label>
                                            <Col sm="10">

                                                <Select id="status" className="custom-select form-control" name="status" onChange={this.onStatusChange} value={this.props.status_id: null}>
                                                    <option value="">Select</option>
                                                    {this.state.status_list.map((status, index) => {

                                                        return (

                                                        <option key={index} value={status.value}>{status.label}</option> 

                                                        );
                                                    })}
                                                </Select>


                                            </Col>
                                        </Row>
                                    </FormGroup>
                                    <FormGroup>
                                        <Row>
                                            <Label sm="2">Attachments</Label>
                                            <Col sm="10">

                                                    <Input
                                                    type="file"
                                                    id="attachment1" name="attachment1[]"
                                                    onChange={this.onInputChange} 
                                                    multiple="multiple"
                                                    />

                                            </Col>
                                        </Row>
                                    </FormGroup>

                                    <div className="border-top pt-3 mt-3 d-flex no-block"> 

                                        <Button type="button" onClick={this.sendReply} className="btn btn-dark mr-2 ml-auto">
                                        Send Reply
                                        </Button>

                                    </div>
                                </Form>
                            </CardBody>
                        </Card>
                    </Col>
                    </Row>
                    {/* --------------------------------------------------------------------------------*/}
                    {/* End Inner Div*/}
                    {/* --------------------------------------------------------------------------------*/}
                </div>
                </div>
            </aside>
        );
    }
}
export default View;

错误图片:check here

3 个答案:

答案 0 :(得分:0)

非常疯狂的错误:

import { Formik, Form, Field } from 'formik';

如果您不想给初始值,则以任何代价将其初始化为空值。例如

const initialValues = {
    firstname: "",
    lastname: ""
}

然后在

initialValues={initialValues}

答案 1 :(得分:0)

我在使用 formik

时遇到了同样的错误

调试后,我发现我的 initialValues 对象名称有误。确保您具有与您定义的initalValues 相同的对象。就我而言,我有 FormUrlEncodedFormatter 对象,但在 Formik 道具中使用了 timeCapturesInitialValues

答案 2 :(得分:-4)

如果您的return语句在任何给定点为null,则会发生此错误。 示例:

const obj = null;

返回(

对象

);

请检查您的代码。