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