Skip to content

Commit e17b047

Browse files
committed
Fix example switching by removing conflicting useEffect
- Remove the second useEffect that was conflicting with example switching - Keep only the initialization useEffect to handle initial URL parameters - Let handleExampleSelect function fully control example switching - This prevents race conditions between multiple state update sources
1 parent 55770dc commit e17b047

File tree

1 file changed

+0
-50
lines changed

1 file changed

+0
-50
lines changed

docs/src/pages/example-detail.tsx

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -134,56 +134,6 @@ export default function ExampleDetail() {
134134
}
135135
}, [location.pathname, location.search, EXAMPLES_CONFIG.length]); // 监听 EXAMPLES_CONFIG 长度变化
136136

137-
138-
139-
// 监听路由变化,处理语言切换时的URL问题
140-
useEffect(() => {
141-
const handleLocationChange = () => {
142-
// 支持两种URL参数格式:查询参数 ?id=... 或路径参数 /example-detail/...
143-
const urlParams = new URLSearchParams(location.search);
144-
let urlExampleId = urlParams.get("id");
145-
146-
// 如果查询参数中没有找到id,尝试从路径中解析
147-
if (!urlExampleId) {
148-
const pathSegments = location.pathname.split('/');
149-
// 查找可能包含id的路径段
150-
for (let i = 0; i < pathSegments.length; i++) {
151-
if (pathSegments[i] === 'example-detail' && i + 1 < pathSegments.length) {
152-
const potentialId = pathSegments[i + 1];
153-
// 检查是否是有效的示例ID(存在于EXAMPLES_CONFIG中)
154-
if (EXAMPLES_CONFIG.some(ex => ex.id === potentialId)) {
155-
urlExampleId = potentialId;
156-
break;
157-
}
158-
}
159-
}
160-
}
161-
162-
if (!urlExampleId) {
163-
urlExampleId = "hello-world";
164-
}
165-
166-
// 确保示例存在
167-
const targetExample = EXAMPLES_CONFIG.find(ex => ex.id === urlExampleId) || EXAMPLES_CONFIG[0];
168-
169-
// 如果当前示例ID与URL参数不匹配,更新示例
170-
if (targetExample.id !== exampleId) {
171-
setExampleId(targetExample.id);
172-
setCode(targetExample.code);
173-
174-
// 运行新示例
175-
setTimeout(() => {
176-
if (previewRef.current) {
177-
runCodeWithContent(targetExample.code);
178-
}
179-
}, 0);
180-
}
181-
};
182-
183-
// 监听location变化,包括语言切换
184-
handleLocationChange();
185-
}, [location.pathname, location.search, EXAMPLES_CONFIG, exampleId]);
186-
187137
// 监听浏览器的 popstate 事件,处理前进后退和语言切换
188138
useEffect(() => {
189139
const handlePopState = () => {

0 commit comments

Comments
 (0)