const [btn, setBtn] = useState([]);
let newArray = []
for (let i = 1; i<=10; i ) {
newArray.push(i);
console.log(btn);
setBtn([...btn,...newArray])
}
- i have tried to add newArray to state by using the spred operatar but this will result into an infinite loop and my react app is crashed and after geeting the 1 to 10 value in my btn array i need to use loop over my btn array state.*
CodePudding user response:
put it inside useEffect
const [btn, setBtn] = useState([]);
useEffect(() => {
const newArray = []
for (let i = 1; i<=10; i ) {
newArray.push(i);
setBtn([...btn,...newArray])
}
}, [])