Khushbu
Khushbu

Reputation: 735

TypeError: Cannot convert undefined or null to object React Validation

I have used React validation library

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

If I visit my page from any other page, it does not give any error. but when I refresh the page, it gives an error

TypeError: Cannot convert undefined or null to object

Please suggest an idea if anybody has faced this issue before.

Code:

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;

Error image: check here

Upvotes: 4

Views: 8030

Answers (4)

Zeeshan Siddique
Zeeshan Siddique

Reputation: 19

after upgrading react & it's packages i was facing above issue & it's resolved by Hassan Ali Shahzad answer, code that may help some one

<Formik
  enableReinitialize={true}
  initialValues={{
    heatmaps: '',
  }}

Upvotes: 1

Sanan Ali
Sanan Ali

Reputation: 3407

I was facing the same error using formik

After debugging I came to know I had a typo in the initialValues object name. Make sure you are having the same object for initalValues that you have defined. In my case I had timeCapturesInitialValues object but used timeCapturesInitailValues in the Formik prop.

Upvotes: 0

Hassan Ali Shahzad
Hassan Ali Shahzad

Reputation: 2724

Really crazy error:

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

above required initial values on any cost if you dont want to give initial value initialize it with empty.for example

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

and then in <Formik tag

initialValues={initialValues}

Upvotes: 7

Mallappa Bijjur
Mallappa Bijjur

Reputation: 1

if your return statement is null at any given point this error occurs. Example:

const obj = null;

return(

obj

);

Please check your code .

Upvotes: -5

Related Questions