0

I have a form where user can generate new inputs to the form he wants to submit, however i am stuck on handling the values of the new generated inputs as i need to store them in the state.

My code

const [supply_detail_data,setSupply_detail_data]=React.useState({
    suppCashDetail:[{text : [],val:[]}]
});

const addNewSuppDetailInput = () => {
    setSupply_detail_data(
        {suppCashDetail: [...supply_detail_data.suppCashDetail,{text : [],val:[]}]}
    )
}

function supply_detail_handler(event){
    // should store the values of inputs in the State
}

JSX

<div className='addNewInput' onClick={addNewSuppDetailInput}>+</div>
 {
    supply_detail_data.suppCashDetail.map((el,index) => {
    let textID='suppDetailText'+index;
    let valID='suppDetailVal'+index;
        return (
        <div key={index}>
        •<input type='text' 
            name={textID}
            value={el.text||''} 
            onChange={supply_detail_handler.bind(index)}
        />

        <input type='number'
            name={valID} 
            value={el||''} 
            onChange={supply_detail_handler.bind(index)}/> 
        </div>
        );

    }) 
 }

Each time the user press on the + two inputs generated, one of type text the other of type number,I need to know how the supply_detail_handler that is executed on value change to store the value of new generated inputs

1 Answer 1

2

What you need is to assign every input a unique identifier that later you can use in you store. Check this example: https://codesandbox.io/s/muddy-firefly-xz31w

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.