个股论坛重做

This commit is contained in:
2026-01-06 10:30:08 +08:00
parent d4366b041f
commit 03160da91f

View File

@@ -94,9 +94,16 @@ const ChannelSidebar: React.FC<ChannelSidebarProps> = ({
const [newChannelType, setNewChannelType] = useState<ChannelType>('text');
const [newChannelTopic, setNewChannelTopic] = useState('');
const [creating, setCreating] = useState(false);
const [hasInitialized, setHasInitialized] = useState(false);
// 加载频道列表
// 使用 ref 保存回调,避免依赖项变化导致重新加载
const onChannelSelectRef = React.useRef(onChannelSelect);
onChannelSelectRef.current = onChannelSelect;
// 加载频道列表(只在组件挂载时执行一次)
useEffect(() => {
if (hasInitialized) return;
const loadChannels = async () => {
try {
setLoading(true);
@@ -108,14 +115,16 @@ const ChannelSidebar: React.FC<ChannelSidebarProps> = ({
for (const category of data) {
const channel = category.channels.find(c => c.id === initialChannelId);
if (channel) {
onChannelSelect(channel);
onChannelSelectRef.current(channel);
break;
}
}
} else if (data.length > 0 && data[0].channels.length > 0) {
// 默认选中第一个频道
onChannelSelect(data[0].channels[0]);
onChannelSelectRef.current(data[0].channels[0]);
}
setHasInitialized(true);
} catch (error) {
console.error('加载频道失败:', error);
} finally {
@@ -124,7 +133,7 @@ const ChannelSidebar: React.FC<ChannelSidebarProps> = ({
};
loadChannels();
}, [initialChannelId, onChannelSelect]);
}, [initialChannelId, hasInitialized]);
// 切换分类折叠状态
const toggleCategory = useCallback((categoryId: string) => {