【发布时间】:2019-03-14 08:27:17
【问题描述】:
我下面的代码可以工作,但如果this.buttonRef.current.firstChild.focus() 不在 setTimeout 函数中,它就会停止工作。
通过查看官方文档的裁判,我不明白为什么会发生这种情况。我的组件有什么明显的问题吗?如果不是我想知道我网站上的另一个组件是否正在“窃取”焦点,因为当 url 道具更改时模式已关闭。
更新:一件奇怪的事情是,如果我 console.log 在 setTimeout 之外,那么我可以看到该元素存在于 DOM 中。
UPDATE2:原来是我的模式中的 React Trap Focus 导致了问题。移除焦点陷阱意味着我不需要超时。因为我需要焦点陷阱,所以我认为 setTimeout 需要保留。
https://github.com/davidtheclark/focus-trap-react
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.buttonRef = React.createRef();
}
componentDidUpdate(prevProps) {
if (this.props.url === '' && prevProps.url = "old-url") {
console.log('target element: ', this.buttonRef.current.firstChild)
// This doenst work if not in a setTimeout
// this.buttonRef.current.firstChild.focus();
setTimeout(() => {
this.buttonRef.current.firstChild.focus();
}, 1);
}
}
render() {
const {
limitIsReached,
add
} = this.props;
return (
<Fragment>
<Title>My title</Title>
<Section>
<Button>
Add a promo code
</Button>
<span ref={this.buttonRef}>
{limitIsReached ? (
<Alert
message="Sorry limit reached"
/>
) : (
<Button
onClick={add}
>
Add new
</Button>
)}
</span>
<List compact />
</Section>
</Fragment>
);
}
}
export default MyComponent;
【问题讨论】:
-
你有seen this issue吗?
-
@Tholle Ive 更新了我的问题。是的,我有,但随着 console.log 的工作,我认为我没有看到同样的问题。
标签: reactjs