I am tring to push new value to arrays but the arrays is not updating with new values. It only contains one value after push. The arr value after button click only showing one value, but I want it to have multiple values. What are multiple ways to put values into arr variable or copy value to another variable?
function App() {
const [value, setValue] = useState(null);
let arr = [];
function addToArr() {
console.log(value);
arr.push(value);
console.log(arr); // it is only showing one value
}
return (
<div>react app
<div>
<textarea
value={value}
onChange={(e) => setValue(e.target.value)}
>
</textarea>
<button onClick={addToArr}>Add</button>
</div>
</div>
);
}
export default App;
arris updated.refin this context? I understand using a state variable to holdarrwill do the trick; but, couldn't quite figure-out (for myself) howrefwould help solve.const arr = useRef([]), and then instead of OP doinngarr.push(value)they could usearr.current.push(value):)let copyValue = value, then what will be correct approach..currentvalue:arr.current = valueif that is what you're after?