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

161
Views
convertir el texto de entrada en varias líneas en React

Estoy tratando de crear un área de texto donde la entrada del usuario se convertirá en 2 líneas separadas (li) si la longitud del texto es mayor que 10.

 import { useState } from "react"; import "./styles.css"; export default function App() { const [area, setArea] = useState([]); const [newText, setNewText] = useState([]); const onChange = (event) => { let val = event.target.value; //convert to array const arr = val.split(""); setArea(arr); }; const changeNewText = () => { setNewText(area); }; return ( <div className="App"> <textarea onChange={onChange} /> <br /> <br /> <button onClick={changeNewText}>change text</button> <br /> {newText.length < 10 ? newText : "text is too long"} </div> ); }

Actualmente, solo puede mostrar texto si la longitud del texto es inferior a 10; sin embargo, si es mayor que 10, mostrará 'el texto es demasiado largo'.

¿Cómo harías para dividir el texto en dos líneas?

Por ejemplo, digamos que la entrada de texto del usuario

 Hello, the earth revolves around sun.

debe compilar para

 - Hello, the - n earth re

ambas líneas deben ser un elemento div o li.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Puedes usar una cadena de fragmentos

 ArrayString = str.match(/.{1,10}/g);

El código debería ser así.

 import { useState } from "react"; import "./styles.css"; export default function App() { const [area, setArea] = useState([]); const [newText, setNewText] = useState([]); const onChange = (event) => { let val = event.target.value; //convert to array const arr = val.split(""); setArea(arr); }; const changeNewText = () => { setNewText(area); }; ArrayString = newText.match(/.{1,10}/g); Var text = <div></div> For (let i = 0; i < ArrayString.length; i++) { text.append(<li>ArrayString[i]</li>) } return ( <div className="App"> <textarea onChange={onChange} /> <br /> <br /> <button onClick={changeNewText}>change text</button> <br /> {text} </div> ); }
about 4 years ago · Santiago Trujillo 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!