【发布时间】:2016-09-28 12:54:20
【问题描述】:
我有一个带有输入元素的组件,我使用 defaultValue 来设置初始值。 我想聚焦该元素并最初选择整个值,但似乎调用 componentDidMount 时未设置默认值。
你有什么建议吗?
我使用 window.setTimeout 但我想在我的 react-components 中避免这种情况:
public componentDidMount(): void {
if (this.props.focus) {
let tInput: HTMLInputElement = ReactDOM.findDOMNode(this).getElementsByTagName("input").item(0);
if (tInput) {
tInput.focus();
// FixMe: defaultValue is set too late by react so i cant set selection instantly
if (this.props.defaultValue) {
window.setTimeout(
() => {tInput.setSelectionRange(0, this.props.defaultValue.length); },
100
);
}
}
}
}
【问题讨论】:
-
你能发布渲染调用吗?你是如何设置默认值的?
标签: select reactjs input focus