Skip to content

Commit 2658cb2

Browse files
fix:Warning: Cannot update during an existing state transition (such as within render).
Render methods should be a pure function of props and state.
1 parent 7173477 commit 2658cb2

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,33 @@ React DOM嵌套警告 :<div> 不能作为 <p> 的子元素
6666

6767
修复后:
6868
<img width="1519" height="630" alt="image" src="https://github.com/user-attachments/assets/aeec791e-e240-4f1b-938a-d55283aede08" />
69-
----
70-
71-
7269

70+
----
71+
修复前:
72+
(匿名) @ react-dom.development.js:25690
73+
add.tsx:55 {label: 'storage.WebDav', type: 'WebDav', description: 'description.webdav', framework: 'openlist', defaultParams: {…}}
74+
add.tsx:75 Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state.
75+
at AddStorage_page (http://localhost:5173/src/page/storage/add.tsx:47:17)
76+
at RenderedRoute (http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=24e56973:4028:5)
77+
at Routes (http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=24e56973:4467:5)
78+
at main
79+
at Content (http://localhost:5173/node_modules/.vite/deps/@arco-design_web-react.js?v=24e56973:18020:25)
80+
at section
81+
at Layout (http://localhost:5173/node_modules/.vite/deps/@arco-design_web-react.js?v=24e56973:18081:43)
82+
at section
83+
at Layout (http://localhost:5173/node_modules/.vite/deps/@arco-design_web-react.js?v=24e56973:18081:43)
84+
at ConfigProvider (http://localhost:5173/node_modules/.vite/deps/@arco-design_web-react.js?v=24e56973:7478:15)
85+
at App (http://localhost:5173/src/app.tsx:165:20)
86+
at Router (http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=24e56973:4410:15)
87+
at BrowserRouter (http://localhost:5173/node_modules/.vite/deps/react-router-dom.js?v=24e56973:5155:5)
88+
printWarning @ react-dom.development.js:86
89+
error @ react-dom.development.js:60
90+
warnAboutRenderPhaseUpdatesInDEV @ react-dom.development.js:27540
91+
92+
分析:问题在于 在渲染阶段直接调用 setFieldValue,违反了 React 的渲染纯净性原则
93+
94+
修复内容:src\page\storage\add.tsx 安全地设置 mount_path,避免在渲染阶段更新状态
95+
-----
7396
<h1 align="center">
7497
<br>
7598
<img src="https://raw.githubusercontent.com/VirtualHotBar/NetMount/main/public/img/color.svg" width="150"/>

src/page/storage/add.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ function AddStorage_page() {
7171
setStorageName(storageInfo.defaultParams.name)
7272
}, [storageTypeName])
7373

74-
if (storageInfo.framework === 'openlist') {
75-
formHook?.setFieldValue('mount_path', '/' + storageName)
76-
}
74+
// 安全地设置 mount_path,避免在渲染阶段更新状态
75+
useEffect(() => {
76+
if (storageInfo.framework === 'openlist' && formHook) {
77+
formHook.setFieldValue('mount_path', '/' + storageName);
78+
}
79+
}, [storageInfo.framework, formHook, storageName]);
7780

7881
let content: JSX.Element
7982

0 commit comments

Comments
 (0)