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

142
Views
Función del botón Copiar texto en React JS

Estoy trabajando en el proyecto create-react-app, cree una función en este proyecto, pero la función de texto de copia no funciona, los problemas se mencionan en la siguiente línea de código. por favor revise el problema...

 import React, {useState} from 'react' export default function TextForm(props) { const handleCopy = () => { console.log("I am copy"); let text = document.getElementById("mybox"); text.Select(); navigator.clipborad.writetext(text.value); } const handleOnChange = (event) =>{ // console.log("on change"); setText(event.target.value); } const [text, setText] = useState(''); return ( <> <div className='container'> <h1>{props.heading} </h1> <div className="mb-3"> <textarea className="form-control" value={text} id="myBox" rows="8" onChange={handleOnChange}></textarea> <button className="btn btn-primary mx-2 my-2" onClick={handleCopy}>Copy Text</button> </div> </div> <div className="container my-3"> <h2>Your text summary</h2> <p>{text.split(" ").length} Word and {text.length} Characters</p> <p>{0.008 * text.split(" ").length} Minute Read</p> <h3>Preview</h3> <p>{text}</p> </div> </> ) }

FormaTexto.js:16

 Uncaught TypeError: Cannot read properties of null (reading 'Select')
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Hay algunos problemas en su código:

  1. Ha creado un estado para el text y no necesita obtenerlo mediante getElementById y Select .
  2. writetext no existe en navigator.clipborad . Debe ser writeText (se distingue entre mayúsculas y minúsculas).
  3. Para usar un estado en todo el componente, debe definir state en la parte superior del componente. Entonces, mueva const [text, setText] = useState(''); a la parte superior de su componente.
  4. el estado del text no tiene propiedad de value . Puede acceder a su valor con solo enviar un mensaje de text .

Aquí está el código editado:

 function TextForm(props) { const [text, setText] = useState(''); const handleCopy = () => { navigator.clipboard.writeText(text); } const handleOnChange = (event) =>{ setText(event.target.value); } return ( <> <div className='container'> <h1>{props.heading} </h1> <div className="mb-3"> <textarea className="form-control" value={text} id="myBox" rows="8" onChange={handleOnChange}></textarea> <button className="btn btn-primary mx-2 my-2" onClick={handleCopy}>Copy Text</button> </div> </div> <div className="container my-3"> <h2>Your text summary</h2> <p>{text.split(" ").length} Word and {text.length} Characters</p> <p>{0.008 * text.split(" ").length} Minute Read</p> <h3>Preview</h3> <p>{text}</p> </div> </> ) }

Editar elocuente-gauss-turvqo

about 4 years ago · Santiago Trujillo Report

0

Tiene valor de text , por lo que no necesita esto.

 let text = document.getElementById("mybox"); text.Select();

Así que tal vez solo hagas esto

 navigator.clipborad.writetext(text);
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!