I'm new to React and trying to add input fields when button Add Bullet is clicked (sort of a ToDo list functionality).
My component is:
return (
<Container>
{bullet.map((text, i) => {
return (
<div className="box">
<p>Bullet {i+1}</p>
<input
name="Bullet" placeholder="Enter text"
value={text} onChange={(event) => {
setBullet([event.target.value])
}}/>
<button onClick={handleAddClick}>Add Bullet</button>
</div>
);
})}
</Container>
);
};
The problem is that the function handleAddClick is called (checked via console.log) but it doesn't add the new input method. I'm using state array to update so it will be helpful is someone can help me in fixing the problem.
handleAddClick:
const handleAddClick = useCallback(() => {
setBullet([
...bullet,
'Lorem Ipsum is simply dummy text'
])
}, [ bullet ]);
Update: After modifying the function now the problem is that whenever I start typing in an input box it immediately removes all other input boxed and keep only one. I'm passing the value of event.target.value to onChange function.
handleAddClickcallback<button onClick={(e) => handleAddClick}>Add Bullet</button>, it should beonClick={(e) => handleAddClick(e)}or more simplyonClick={handleAddClick}.