1

lets say i have

        var form = {
            header: {
                value: null,
                isValid: false,
                type: 'textinput',
                rules: {
                    isRequired: true,
                },
            },
           selectedImages: []
        }

at some point, user will push some images into this form.selectedImages i am doing it in this way:form.selectedImages.push(image) BUT because my selectedImages is null, push is not working here. so, how can i initilise an empty array btw, i have tried selectedImages: new Array() but not worked too.

Is there anyway i can fix this issue?

thanks.

UPDATE

after i initialized this form. i am using setState({form: form}) to set this form to the state. after set is done, form.selectedImages: null, console.log(form) says..

UPDATE 2 here is the full react code:

 var form = {
            header: {
                value: null,
                isValid: false,
                type: 'textinput',
                rules: {
                    isRequired: true,
                },
            },
            description: {
                value: null,
                isValid: false,
                type: 'textinput',
                rules: {
                    isRequired: true,
                },
            },
            provinence: {
                value: null,
                isValid: false,
                type: 'dropdowninput',
                rules: {
                    isRequired: true,
                },
            },
            district: {
                value: null,
                isValid: false,
                type: 'dropdowninput',
                rules: {
                    isRequired: true,
                },
            },
            quarter: {
                value: null,
                isValid: false,
                type: 'dropdowninput',
                rules: {
                    isRequired: true,
                },
            },
            address: {
                value: null,
                isValid: false,
                type: 'textinput',
                rules: {
                    isRequired: true,
                },
            },
            where: {
                value: null,
                isValid: false,
                type: 'dropdowninput',
                rules: {
                    isRequired: true,
                },
            },
            to: {
                value: null,
                isValid: false,
                type: 'dropdowninput',
                rules: {
                    isRequired: true,
                },
            },
            deliverTo: {
                value: null,
                isValid: false,
                type: 'dropdowninput',
                rules: {
                    isRequired: true,
                },
            },
            selectedImages: [],
            whatsappContact: false,
            emailContact: false,
            isFree: false
        }

this is how i use this form i a switch case. i have n category, and they all have different kind of input information. so created a function, called initializeFormState() where basically i get related category info from state and making nullable objects which i don't need based on selected category. at the end,i set this form inputs to the state in order to validate and push them to the server.

this is one of my cases:

            case 0: //emlak
                form.where = null;
                form.to = null;
                form.address = null;
                deliverTo = null;
                this.setState({ ...form, selectedImages: [] }, () =>{console.log(form)});
9
  • 2
    selectedImages isn't null, it's an empty array []. What is the actual error message you're getting when you try to push to it? Commented Oct 22, 2019 at 9:24
  • 2
    That should work, can you show some more code? Commented Oct 22, 2019 at 9:24
  • 1
    and how is this selectedImages: [] wrong? it initializes an empty array normally, it's not null Commented Oct 22, 2019 at 9:24
  • it is working fine.Just update your code and the error log Commented Oct 22, 2019 at 9:25
  • you already initialized it with an empty array like this: selectedImages: [] what is the problem? Commented Oct 22, 2019 at 9:25

3 Answers 3

3

In JavaScript, You can directly initialize object or array via passing empty records like {} or [].

Your code is right, you don't need to do anything now. just try to push images in the selectedImages.

form.selectedImages.push(image)

It will work.

UPDATE
In order to make it working, You have to set state via below command.

if(!Array.isArray(form.selectedImages)) form.selectedImages = [];
this.setState({form: form});
Sign up to request clarification or add additional context in comments.

2 Comments

Hello again, i have tried what you have suggested. the thing is, before setState and after setState, form.selectedImages = null.
I guess the problem with this line this.setState({ ...form, selectedImages: [] }, () =>{console.log(form)});. It should be this.setState({ ...form, selectedImages: [] }, () =>{console.log(this.state)});.
1

[], like you did, will initialize an empty array. Array.push is available on form.selectedImages. If you run into issue, it's probably something else.

An empty array ([]) is not null, and is actually truthy (Boolean([]) equals true).

        var form = {
            header: {
                value: null,
                isValid: false,
                type: 'textinput',
                rules: {
                    isRequired: true,
                },
            },
           selectedImages: []
        }
        
document.write(form.selectedImages);
form.selectedImages.push('foo')
document.write(form.selectedImages);

Comments

0

if you have initialized like this : form.selectedImages = [] you can not have the error that it's null when you try the push method

Now if you want to make sure that the table is not empty when you want to push use this:

form.selectedImages = [...image]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.