最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

react typescript - Retaining focus on the previously selected input when switching between tabs in ReactJS? - Stack Overflow

programmeradmin1浏览0评论

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]);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论