1

I am facing a problem. If you can give me some advice, it would be a huge help for me.

I have a state with object

const [work , setWork] = useState({company:"" , jobTitle:"", jobType:"", location:""});

const [list, setList] = useState([]);

I want when the user update and submit the state, I send the object in an array list [ ]

companyValue is is the result of onChange

const add = (e) => {
            e.preventDefault();
    
            if(companyValue){
                setWork(prevState => ({
                    ...prevState,
                    company: companyValue
                }))
                 setList(prevState => ({
                    ...prevState,
                    work
                 }))
                    }
               }

and I want to have a result like this

list = [
        {company:"" , jobTitle:"", jobType:"", location:""},
        {company:"" , jobTitle:"", jobType:"", location:""},
        {company:"" , jobTitle:"", jobType:"", location:""}
        ]

1 Answer 1

1

Do something like this:

const newWork = {
  ...work,
  company: companyValue
}

setWork(newWork);

setList(
  list.concat(newWork)
)

If you want to add only that companyValue, which I assume look like this: {company:"" , jobTitle:"", jobType:"", location:""}, then you can do this:

setList(
  list.concat(companyValue)
);
Sign up to request clarification or add additional context in comments.

7 Comments

thank's it works, but the value it added after 2 submit, do you have any idea about that
Which solution you used? the work one or the companyValue one?
the first one with list.concat(work)
Okay, try the second one and lemme know. Or you can try this: ` const newWork = { ...work, company: companyValue } setWork(newWork); setList( list.concat(newWork) ) `
i have the result, thank you so much Anas, you saved my day
|

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.