I got a project where I am splitting the words from different inputfields and push these to a content array. The array should 'remember' all the words that have been pushed in it. Currently, whenever the user starts typing in a 'new' (and empty) inputfield, the content array is cleared because the value is empty.
I got a codesandbox setup here.
So lets say the user types lorem ipsum dolar in the first inputfield and sit amet in the second inputfield, the content array should like like this: ['lorem', 'ipsum', 'dolar', 'sit', 'amet']
How can I achieve this result?
Code for reference
import "./styles.css";
import { useState } from "react";
export default function App() {
const [fields, setFields] = useState([
{ id: 1, value: "" },
{ id: 2, value: "" }
]);
const [content, setContent] = useState([]);
const handleOnChange = (e, idx) => {
let newField = [...fields];
newField[idx].value = e.target.value;
setFields(newField);
//How do I push both the values from field 1 AND 2 in the array?
//Currently, when you switch from inputfield, the content array gets reset
let words = e.target.value.split(" ");
setContent(words);
};
return (
<div>
{fields.map((field, idx) => (
<div>
<span>Field {idx}</span>
<input
className="border-2 border-red-500"
type="text"
key={field.id}
value={field.value}
onChange={(e) => handleOnChange(e, idx)}
/>
</div>
))}
<div style={{ marginTop: "20px", backgroundColor: "gray" }}>
{content.map((item, idx) => (
<div key={idx}>{item}</div>
))}
</div>
</div>
);
}