1

Please I am trying to dynamically set array object into input field and send it to the backend. Thanks

When I console.log printOut, it returns undefined.

const myArr = [
        { product: 'egg', price: 5, id: 1 },
        { product: 'cake', price: 3, id: 2 }
]

const [input, setInput] = useState(myArr)

const changeHandler = (id) => event => {
        const { name, value } = event.target;
        setInput(input => input.map((el) => el.id === id
          ? {
              ...el,
              [name]: value,
            }
          : el,
        ));
};

const submitForm = (e) => {
        e.preventDefault();
        
        let printOut = input
            
        console.log({print:printOut});

        try {
            axios.post('/products/print', printOut)
        } catch (error) {
            console.log(error);
        }
    }

    return (
        <form onSubmit={submitForm}>
            {myArr.map(x=>(
                <div key={x.id}>
                    <input name='product' value= {x.product} onChange={(e) =>changeHandler(x.id)(e)}  />
                    <input name='price' value= {x.price} onChange={(e)=> changeHandler(x.id)(e)} />
                    
                </div>
            ))}
            <input type="submit" value="Submit" />
        </form>
    )
16
  • Is not your x.id undefined? Commented Sep 20, 2021 at 13:54
  • input is an array but you're trying to access it like an object input.product. Commented Sep 20, 2021 at 13:55
  • Your onChange event should be ‘onChange={(e) => changeHander(x.I’d)(e) }’ . Your changeHandler returns a function. I think it’s called currying but it’s a function returning another function. Commented Sep 20, 2021 at 14:03
  • thanks Andy, please how can i access it like an array and again i got an empty data in the database Commented Sep 20, 2021 at 14:04
  • thanks joseph, i have modified it and i still got undefined error Commented Sep 20, 2021 at 14:09

1 Answer 1

1

As we discussed in the chat, there were plenty of issues.

  1. handleChange call was not correct. Your onChange event should be onChange={(e) => changeHander(x.id)(e) } . Your changeHandler returns a function. I think it’s called currying but it’s a function returning another function.
  2. Your setInput(input => input.map((el) => el.id === id? {...el, [name]: value,} : el,)); is also wrong. input.map will never work as you have set initial state for that as []. Now I don't know what you will need but, either update initial state to myArray or change setState mapping to myArray like setInput(input => myArray.map((el) => el.id === id? { ...el, [name]: value,} : el,));
Sign up to request clarification or add additional context in comments.

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.