1. 程式人生 >實用技巧 >其它 >React報錯: Render methods should be a pure function of props and state

React報錯: Render methods should be a pure function of props and state


技術標籤:reactreactjs

	最近在用react寫移動端頁面,遇到這樣一個報錯。一開始無從下手,解決後記錄下來
	報錯場景:每次重新整理頁面請求列表介面就報錯

React報錯: Render methods should be a pure function of props and state


解決方法:原來是我在render裡寫了一個函式處理,對state裡的資料進行了setState。
刪掉render方法裡的函式方法就行了。

記住:render裡不能進行setState處理

現在就可以解釋這個報錯的意思了,“render方法應該是一個對於props和state純粹的函式”,意味著不能在render裡進行setState的操作。

至於為什麼??我會後續查資料繼續更新,如果有知道原因的朋友可以分享一下