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

An object from arrays of objects – React – useState

help please I want to add value to state without overwriting. Currently, when adding a value, the array is overwritten. I want to use useState and I want to use the value from the form.

import {useState} from 'react';

const initialState = {
    people: [
        {email: 'Jan'},
        {email: 'Izabela'},
        {email: 'Michael'}
    ] }

const StateModification = () => {

    const [names,setNames] = useState(initialState)

    const handleSubmit = (e) => {
        e.preventDefault(); 
    }

    const handleChange2 = (e) => {
        setNames({
            ...names,
            people: [{[e.target.name]: e.target.value}]
        })
    }

    return (
        <div>
            <form onSubmit={handleSubmit}>
                <label>E-mail</label>
                <input 
                    id='email' 
                    type='text' 
                    name='email'
                    value={names.email}
                    onChange={handleChange2}    
                />
                
                <button type='submit'>Add</button>
            </form>
        </div>`enter code here`
    ) }

export default StateModification;

>Solution :

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

I think you need to add an email in your data and after click on add button that email will store in people variable with your previous data so i have update your code and it should work for you.

import {useState} from 'react';

const initialState = {
    people: [
        {email: 'Jan'},
        {email: 'Izabela'},
        {email: 'Michael'}
    ] }

const StateModification = () => {

    const [names,setNames] = useState(initialState)
    const [email,setEmail] = useState("")

    const handleSubmit = (e) => {
      e.preventDefault();
        setNames({
          people: [...names.people, { email }]
      })
    }

    const handleChange2 = (e) => {
      e.preventDefault();
      setEmail(e.target.value)
    }

    return (
        <div>
            <form onSubmit={handleSubmit}>
                <label>E-mail</label>
                <input 
                    id='email' 
                    type='text' 
                    name='email'
                    value={email}
                    onChange={handleChange2}    
                />
                
                <button type='submit'>Add</button>
            </form>
        </div>
    ) }

export default StateModification;
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