简体   繁体   English

如何将输入集中在父级的子级组件上?

[英]How to focus input on a child component from parent?

I'm using React.forwardRef to set a ref on my Child component, like so: 我正在使用React.forwardRef在我的Child组件上设置一个ref ,如下所示:

const Input = React.forwardRef(
  ({ value, onChange, onKeyPress, placeholder, type, label, ref }) => (
    <div style={{ display: "flex", flexDirection: "column" }}>
      <input
        ref={ref}
        style={{
          borderRadius: `${scale.s1}rem`,
          border: `1px solid ${color.lightGrey}`,
          padding: `${scale.s3}rem`,
          marginBottom: `${scale.s3}rem`
        }}
        value={value}
        onChange={onChange}
        onKeyPress={onKeyPress}
        placeholder={placeholder ? placeholder : "Type something..."}
        type={type ? type : "text"}
      />
    </div>
  )
);

In the parent I use const ref = React.createRef() and then call it: 在父级中,我使用const ref = React.createRef() ,然后调用它:

 onClick = () => {
    this.setState({ showOther: true });
    console.log(this.ref, "ref");
    this.ref.focus();
  };

  render() {
    return (
      <div className="App">
        <button onClick={this.onClick}>Click me</button>
        <Input
          ref={ref}
          value={this.state.value}
          onChange={this.handleChange}
          onKeyPress={this.handleKeyPress}
          placeholder="Type something..."
        />
      </div>
    );
  }

What I get from the console is this: 我从控制台得到的是:

Object {current: null} current: null "ref"

My questions: 我的问题:

  • Why is this null? 为什么这个为空?
  • How do I focus the input? 如何集中输入?

Codesandbox 密码箱

Based on your sandbox, You use ref from outside class Component instead this.ref . 根据您的沙箱,使用外部类Component的ref代替this.ref Just change it from 只需从

<Input
  ref={ref}
/>

into this 进入这个

<Input
  ref={this.ref}
/>

and inside onClick function to be like this 和里面的onClick函数是这样的

 onClick = () => {
    this.setState({ showOther: true });
    console.log(this.ref, "ref");
    this.ref.current.focus(); // change it
  };

this is the working codesandbox , enjoy! 这是工作代码框 ,请尽情享受!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在样式化组件中的子组件的输入焦点上设置父组件 - Style parent component on input focus of child component in styled-components 从 Angular 中的父组件设置焦点到子组件文本框 - Set focus on Child component textbox from Parent component in Angular React - 从父级单击子组件中的输入 - React - click input in child component from parent div父级的preventDefault会阻止输入子进程获得焦点 - preventDefault on a div parent prevent an input child from getting focus 如何将道具从父组件添加到子组件的子组件 - How to add props from parent component to child component of child component 将用户输入从子功能组件传递到父功能组件 - Passing user input from child functional component to parent functional component 来自父组件的角度更新子组件输入 - Angular updating child component input from parent component 如何将多个输入值从 CHILD 组件传递到 Vue 中的父组件? - How to pass multiple input values from CHILD Component to Parent component in Vue? 如何让子组件检测来自父组件的对象 (@Input()) 在 Angular 中已更改 - How to make child component detects object (@Input()) from parent component has changed in Angular Angular 2 @Input - 如何从父组件绑定子组件的 ID 属性? - Angular 2 @Input - How to bind ID property of child component from parent component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM