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)});
selectedImagesisn'tnull, it's an empty array[]. What is the actual error message you're getting when you try to push to it?selectedImages: []wrong? it initializes an empty array normally, it's not null