I'm trying to update my array with elements from another array using useState Hook. Is there anyway to do it? thanks code --->
function App() {
const nums = [1, 2, 3, 4, 5]
const [numbers, setNumbers] = useState([])
useEffect(() => {
console.log(numbers)
nums.forEach(num => {
numbers.push(num);
})
setNumbers(numbers)
console.log(numbers)
}, [])
return (
<>
{numbers.length && numbers.map(num =>
<p key={Math.random() * 10000}>{num}</p>
)}
</>
);
}
export default App;
numsinnumbers. That can be easily done withconst [numbers, setNumbers] = useState(nums). I just initializednumberswithnums. You don't event need auseEffect.