“Ref en componentes funcionales” Código de respuesta

Ref en componentes funcionales

function CustomTextInput(props) {
  // textInput must be declared here so the ref can refer to it  const textInput = useRef(null);  
  function handleClick() {
    textInput.current.focus();  }

  return (
    <div>
      <input
        type="text"
        ref={textInput} />      <input
        type="button"
        value="Focus the text input"
        onClick={handleClick}
      />
    </div>
  );
}
Cheerful Cat

Ref en componentes funcionales

function MyFunctionComponent() {  return <input />;
}

class Parent extends React.Component {
  constructor(props) {
    super(props);
    this.textInput = React.createRef();  }
  render() {
    // This will *not* work!
    return (
      <MyFunctionComponent ref={this.textInput} />    );
  }
}
Cheerful Cat

Respuestas similares a “Ref en componentes funcionales”

Preguntas similares a “Ref en componentes funcionales”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código