Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use event.target.name instead of hardcoded value when setting state in nested array

I have a nested array i add dynamically to my state and therefore i dont know the key/name of the nested array and I can not give the key/name of the nested array when i need to add, update, iterate or remove somethings in the array. An example is the function addClick

  const addClick = (event) => {
    // setValue({ event.target.name: [...value.(event.target.name)), ""] });
    setValue({ DevOps: [...value.DevOps, ""] });
  }; 

The comment contains the way i am thinking it should be. So instead of writing "DevOps" which is the key/name of the array, i want it to use the value of "event.target.name", but it will not use it. So how do i use the value when setting state of "value"??

other examples:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  const removeClick = (event) => {
    //let vals = [...value.(event.target.name))];
    let vals = [...value.DevOps];
    let index = Number(event.target.id);
    vals.splice(index, 1);
    // setValue({ event.target.name: vals });
    setValue({ DevOps: vals });
  };
{/*value.(input[form].name).map((el, i) => ( */ }
    {value.DevOps.map((el, i) => (
           <div key={i}>
           <Array
             name={input[form].name}
             placeholder={input[form].placeholder}
             required={input[form].required}
             key={input[form].placeholder}
             el={el}
             i={i}
             handleChange={(event) => handleChange(event, input[form].input_type)}
             removeClick={removeClick.bind(i)}
              />
             </div>
             ))}

>Solution :

Close. Using a variable/expression/etc. as a property name in an object literal requires bracket notation:

setValue({ [event.target.name]: [...value[event.target.name], ""] });

or:

setValue({ [event.target.name]: vals });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading