相关疑难解决方法(0)

componentDidUpdate与componentDidMount

我需要确保在满足以下条件时关注输入元素:

  • DOM是可用的
  • 属性发生了变化

问题:我是否需要将我的代码放入两者中componentDidUpdate,componentDidMount或者仅仅componentDidUpdate是足够的?

    private makeSureInputElementFocused() {
        if (this.props.shouldGetInputElementFocused && this.inputElement !== null) {
            this.inputElement.focus();
        }

    }

    componentDidMount() {
        this.makeSureInputElementFocused(); // <-- do i need this?
    }
    componentDidUpdate() {
        this.makeSureInputElementFocused();
    }
Run Code Online (Sandbox Code Playgroud)

reactjs

28
推荐指数
4
解决办法
2万
查看次数

标签 统计

reactjs ×1