how react programmatically focus input

前端 未结 6 2088
滥情空心
滥情空心 2020-12-15 03:01

I\'m trying to implement a very simple use case, a UI feature, where:

  1. There is a label with some content in it
  2. If clicked, a text input replaces it wi
6条回答
  •  情话喂你
    2020-12-15 03:37

    In addition to the previous answers, I've added setTimeout to make it work

    handleClick() {
    
    
        if (this.searchInput) {
            setTimeout(() => {
    
                this.searchInput.focus();
    
            }, 100);
        }
    }
    

    where searchInput is the jsx ref of the input

     { this.searchInput = input; }}
          placeholder="Search" />
    

    and the handleClick() is an onClick handler to any element

提交回复
热议问题