admin管理员组文章数量:1387461
I have two tabs, each containing multiple components. When switching between tabs, all components reload every time. I need to retain the previously focused input (Control) when switching tabs. How can I achieve this in ReactJS?
Tried below code. Not working
const [activeTab, setActiveTab] = useState(0);
const tab1ContentRef = useRef(null);
const tab2ContentRef = useRef(null);
const handleTabClick = (tabIndex) => {
setActiveTab(tabIndex);
};
useEffect(() => {
if (activeTab === 0 && tab1ContentRef.current) {
tab1ContentRef.current.focus();
} else if (activeTab === 1 && tab2ContentRef.current) {
tab2ContentRef.current.focus();
}
}, [activeTab]);
本文标签:
版权声明:本文标题:react typescript - Retaining focus on the previously selected input when switching between tabs in ReactJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744575981a2613628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论