鍍金池/ 問答/HTML/ React的setState的callback問題

React的setState的callback問題

代碼如下

render() {
...
 const columns = [
      {
        title: "序號(hào)",
        dataIndex: "number",
        key: "number"
      },
      {
        title: "用戶ID",
        dataIndex: "category",
        key: "category",
        render: (text, record) =>
          this.renderUserColumns(text, record, "category"),
        filterDropdown: (
          <div className="custom-filter-dropdown">
            <Input
              ref={ele => {
                this.searchInput = ele;
              }}
              placeholder="搜索用戶"
              value={this.state.searchText}
              onChange={this.onInputChange}
              onPressEnter={this.onSearch}
            />
            <Button type="primary" onClick={this.onSearch}>
              搜索
            </Button>
          </div>
        ),
        filterIcon: (
          <Icon
            type="search"
            style={{ color: this.state.filtered ? "#108ee9" : "#aaa" }}
          />
        ),
        filterDropdownVisible: this.state.filterDropdownVisible,
        onFilterDropdownVisibleChange: visible => {
          this.setState(
            {
              filterDropdownVisible: visible
            },
            () => this.searchInput.focus()
          );
        }
      },
...

用的antd的表頭自定義功能
callback不是在setState調(diào)用之后再執(zhí)行嗎?當(dāng)我的filterDropdownVisible更新后this.searchInput才會(huì)存在,但是callback并沒有在setState調(diào)用之后執(zhí)行

回答
編輯回答
薄荷糖

已解決

  this.setState(
    {
      filterDropdownVisible: visible
    },
    () =>  this.searchInput && this.searchInput.focus()
  );
2018年6月15日 22:35