gpt4 book ai didi

reactjs - 如果没有 setTimeout,用 focus() react ref 就不起作用(我的例子)

转载 作者:行者123 更新时间:2023-12-03 13:16:53 24 4
gpt4 key购买 nike

我遇到了这个问题,.focus() 仅适用于 setTimeout 如果我将其取出并停止工作。谁能解释一下这是什么原因,可能是我做错了,我该如何解决这个问题。

    componentDidMount() {
React.findDOMNode(this.refs.titleInput).getElementsByTagName('input')[0].focus();
}

使用 setTimeout 的工作示例

componentDidMount() {
setTimeout(() => {
React.findDOMNode(this.refs.titleInput).getElementsByTagName('input')[0].focus();
}, 1);
}

JXS

<input ref="titleInput" type="text" />

我遵循了这个例子React set focus on input after render

渲染函数

render() {
const {title, description, tagtext, siteName} = (this.state.selected !== undefined) ? this.state.selected : {};
const hasContentChangedYet = this.hasContentChangedYet(title, description);

return (
<div>
<h2 className={styles.formMainHeader}>Edit Meta-Data Form</h2>
<table className={styles.formBlock}>
<tbody>
<tr>
<td className={styles.tagEditLabel}>
Tag
</td>
<td className={styles.inputFieldDisableContainer}>
{tagtext}
</td>
</tr>
<tr>
<td className={styles.tagEditLabel}>
Site
</td>
<td className={styles.inputFieldDisableContainer}>
{siteName}
</td>
</tr>
<tr>
<td className={styles.tagEditLabel}>
Title
</td>
<td className={styles.inputFieldContainer}>
<ReactInputField
ref="titleInput"
id="title"
defaultValue={(title) ? title : ''}
onChange={this.onInputChange}
placeholder="Title"
clearTool={true} />
</td>
</tr>
<tr>
<td className={styles.tagEditLabel}>
Description
</td>
<td className={styles.inputFieldContainer}>
<ReactInputField
id="description"
defaultValue={(description) ? description : ''}
onChange={this.onInputChange}
placeholder="Description"
clearTool={true} />
</td>
</tr>
</tbody>
</table>

<div className={styles.formFooter}>
<button id="save-button" className={styles.saveButton} disabled={!hasContentChangedYet} onClick={() => this.handleSavePressed()}>
Save
</button>
<button id="form-cancel-button" className={styles.cancelButton} onClick={this.actions.form.cancelUpdateToTagData}>
Cancel
</button>

</div>
</div>
);
}

最佳答案

看到问题的更新后,我意识到您已经深层嵌套了传递给 render 函数的 HTML,并且您感兴趣的输入元素在 < 时确实不可用em>componentDidMount 调用祖先元素。如 React v0.13 Change Log 中所述:

ref resolution order has changed slightly such that a ref to a component is available immediately after its componentDidMount method is called; this change should be observable only if your component calls a parent component's callback within your componentDidMount, which is an anti-pattern and should be avoided regardless

这就是你的情况。因此,您必须将 HTML 结构分解为单独呈现的元素,如 here 中所述。 ,然后您将在其自己的 componentDidMount 回调中访问输入元素;或者您只需坚持使用现有的计时器技巧即可。

使用componentDidMount确保代码仅在调用它的组件安装安装时运行(请参阅下面文档中的引用)。

请注意,调用React.findDOMNode is discouraged :

In most cases, you can attach a ref to the DOM node and avoid using findDOMNode at all.

Note

findDOMNode() is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because it pierces the component abstraction.

findDOMNode() only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling findDOMNode() in render() on a component that has yet to be created) an exception will be thrown.

来自 ref string attribute 上的文档:

  1. Assign a ref attribute to anything returned from render such as:

     <input ref="myInput" />
  2. In some other code (typically event handler code), access the backing instance via this.refs as in:

    var input = this.refs.myInput;  
    var inputValue = input.value;
    var inputRect = input.getBoundingClientRect();

或者,您可以消除代码的需要,并使用 JSX autoFocus属性:

<ReactInputField
ref="titleInput"
autoFocus
... />

关于reactjs - 如果没有 setTimeout,用 focus() react ref 就不起作用(我的例子),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com