Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

124
Views
Reaccionar usestate con error de bucle for en renderizado

Quiero crear una matriz con valores aleatorios entre 0 y 7 y usar esta matriz como valor de estado, pero usarla en el bucle de llamada de componente funcional para bucle varias veces cuando el valor de estado de console.log. Esto creará un error de representación en reaccionar

 import React from "react"; import "./style.css"; import { usestate} from "react" export default function App() { const [ state , setstate ] = usestate([]); let arr = [] ; for(let i = 0 ; i < 64 ; i++){ arr[i] = Math.floor((Math.random ()*8)); } setstate(arr); return ( <div> <h1>State</h1> <p>{state}</p> </div> ); }

Gracias

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Si usa setstate(myArrayMakingFunction) dentro de un gancho useEffect , esto debería funcionar.

https://reactjs.org/docs/hooks-effect.html

about 4 years ago · Juan Pablo Isaza Report

0

Primero, necesita importar useState, en lugar de usestate. Necesita importar useEffect también o causará muchos renderizados. Su solución es el siguiente código:

 import React, {useState, useEffect} from "react"; import "./style.css"; export default function App() { const [ state , setstate ] = useState([]); let arr = [] ; for(let i = 0 ; i < 64 ; i++){ arr[i] = Math.floor((Math.random ()*8)); } useEffect(()=>{ setstate(arr); }, []) return ( <div> <h1>State</h1> <p>{state}</p> </div> ); }
about 4 years ago · Juan Pablo Isaza Report

0

Haga esto en la fase componentDidMount.

 import React, { useState, useEffect} from 'react'; import "./style.css"; export default function App() { const [ state , setstate ] = useState([]); useEffect(()=>{ let arr = [] ; for(let i = 0 ; i < 64 ; i++){ arr[i] = Math.floor((Math.random ()*8)); } setstate(arr); }, []) return ( <div> <h1>State</h1> {state?.map(i=><p key={i}>{i}</p>)} </div> ); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!