React的setState的callback问题

代码如下

render() {
...
 const columns = [
      {
        title: "序号",
        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调用之后再执行吗?当我的filterDropdownVisible更新后this.searchInput才会存在,但是callback并没有在setState调用之后执行

阅读 3.8k
1 个回答

已解决

  this.setState(
    {
      filterDropdownVisible: visible
    },
    () =>  this.searchInput && this.searchInput.focus()
  );
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题