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

83
Views
Reaccionar a los atributos de acceso de un componente de clase desde su padre

Imagina el siguiente escenario:

 class Input extends React.Component { render() { return <input value="123" /> } } function App() { const inputRef = React.useRef(); return ( <Input ref={inputRef} /> ); }

Quiero acceder al atributo de value de la etiqueta de entrada en el componente de mi App . Sin embargo, inputRef.current.value no está definido.

¿Cómo puedo acceder a los atributos de un componente de clase en React?

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

0

Simplemente pasaré la referencia como accesorio y la usaré en el componente de clase.

 import React from "react"; class Input extends React.Component { render() { const { inputRef } = this.props; return <input ref={inputRef} name="inputExample" defaultValue="123" />; } } export default function App() { const inputRef = React.useRef(null); const logValue = () => { console.log(inputRef.current ? inputRef.current.value : "No Input found"); }; return ( <div className="App"> <Input inputRef={inputRef} /> <button onClick={logValue} type="button"> Input current value </button> </div> ); }

Verifique el sandbox en caso de que quiera ver un ejemplo en vivo: https://codesandbox.io/s/distracted-curran-f6bhbrs-f6bhbr?file=/src/App.js

about 4 years ago · Juan Pablo Isaza Report

0

Debe reenviar la referencia usando forwardRef y establecer la ref en el elemento de input como se muestra a continuación.

componente de clase

 class Input extends React.Component { render() { return <input ref={this.props.innerRef} />; } } const MyInput = React.forwardRef((props, ref) => ( <Input innerRef={ref} {...props} /> )); function App() { const inputRef = React.useRef(); const checkValue = () => { console.log(inputRef.current && inputRef.current.value); }; return ( <div> <MyInput ref={inputRef} /> <button onClick={checkValue}>check value</button> </div> ); } ReactDOM.render(<App />, document.querySelector('.react'));
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div class='react'></div>

Componente de función

 const Input = React.forwardRef((props, ref) => { return <input ref={ref} />; }); function App() { const inputRef = React.useRef(); const checkValue = () => { console.log(inputRef.current && inputRef.current.value); }; return ( <div> <Input ref={inputRef} /> <button onClick={checkValue}>check value</button> </div> ); } ReactDOM.render(<App />, document.querySelector('.react'));
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div class='react'></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!