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

214
Views
incapaz de asignar valores dentro de un forLoop

Estoy tratando de asignar un valor aleatorio a "customStyle.top" dentro de mi bucle for, pero el valor customStyle.top/customStyle.Left no cambia aleatoriamente. Mi código es el siguiente:

 import React from "react"; const customStyle = { position: "absolute", top: "", left: "", zIndex: "initial", color: "green" }; // Splitting Letters const SplitText = React.memo(({ str }) => { return ( <div> {str.split("").map((item, index) => { var randLeft = Math.floor(Math.random() * 1000); var randTop = Math.floor(Math.random() * 600); let letter = item; for (let i = 0; i < item; i++) { customStyle.top = +randTop + "px"; customStyle.left = +randLeft + "px"; letter = letter + i; } console.log(customStyle.top); return ( <div key={index} style={customStyle}> {letter} </div> ); })} </div> ); }); export default SplitText;

enlace a mi CodeSandbox: Haga clic.!

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

0

Creo que olvidó agregar la palabra clave de length en su for loop .

Aquí está el código de trabajo:

 import React from "react"; const customStyle = { position: "absolute", top: "", left: "", zIndex: "initial", color: "green" }; // Splitting Letters const SplitText = React.memo(({ str }) => { return ( <div> {str.split("").map((item, index) => { var randLeft = Math.floor(Math.random() * 1000); var randTop = Math.floor(Math.random() * 600); let letter = item; for (let i = 0; i < item.length; i++) { // item.length customStyle.top = +randTop + "px"; customStyle.left = +randLeft + "px"; letter = letter + i; } console.log(customStyle.top); return ( <div key={index} style={customStyle}> {letter} </div> ); })} </div> ); });
about 4 years ago · Juan Pablo Isaza Report

0

Arreglé 2 errores.

  1. Estás dividiendo una cadena en "". Por lo tanto, solo obtendrá letras, nunca una cadena de varias letras. ¡Eso significa que usar un bucle aquí no tiene sentido!
  2. En su código estaba usando una referencia a customStyle . Cada vez que cambia un valor, lo está cambiando en todas partes. Por lo tanto, copie el valor o cree uno nuevo. Inserte ese valor en su salida.

Código

 import React from "react"; // Splitting Letters const SplitText = React.memo(({ str }) => { return ( <div> {str.split("").map((item, index) => { const style = { position: "absolute", top: Math.floor(Math.random() * 600) + 'px', left: Math.floor(Math.random() * 600) + 'px', zIndex: "initial", color: "green" }; return ( <div key={index} style={style}> {item} </div> ); })} </div> ); }); export default SplitText;
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!