Redux 不會僅在 Safari 瀏覽器中更新狀態,但在 chrome 中它作業正常。當我在 VISITS_SEARCH 的情況下控制臺操作時:我在 Safari 中得到空陣列。但是,我在 chrome 控制臺中得到了有效的陣列
export const visitsReducer = (state = initialState, action) => {
console.log("?? ~ file: visits-reducer.js ~ line 28 ~ visitsReducer ~ action", action)
return produce(state, (draftState) => {
switch (action.type) {
case GET_VISIT_LIST:
draftState.isWorking = true;
draftState.error = null;
return;
case GET_VISIT_LIST_SUCCESS:
draftState.isWorking = false;
draftState.visits = action.visits;
console.log("?? ~ file: visits-reducer.js ~ line 39 ~ returnproduce ~ action", action)
return;
case VISITS_SEARCH:
draftState.searchResults = action.visits;
console.log("?? ~ file: visits-reducer.js ~ line 80 ~ returnproduce ~ action", action)
return;
default:
return;
}
});
};
uj5u.com熱心網友回復:
我假設您使用 fetch 填充資料,根據 Redux Docs,這是常見問題:
我們在示例中使用了 fetch API。它是一種用于發出網路請求的新 API,它取代了 XMLHttpRequest 以滿足大多數常見需求。因為大多數瀏覽器本身還不支持它,我們建議您使用交叉獲取庫:
// Do this in every file where you use `fetch` import fetch from 'cross-fetch'在內部,它在客戶端使用 whatwg-fetch polyfill,在服務器上使用 node-fetch,因此如果您將應用程式更改為通用應用程式,則無需更改 API 呼叫。
請注意,任何 fetch polyfill 都假定 Promise polyfill 已經存在。確保您擁有 Promise polyfill 的最簡單方法是在任何其他代碼運行之前在您的入口點啟用 Babel 的 ES6 polyfill:
// Do this once before any other code in your app import 'babel-polyfill'
uj5u.com熱心網友回復:
發生這種情況是因為有些browser人不支持fetch這就是為什么react告訴您使用fetch如下:
// Do this in every file where you use `fetch`
import fetch from 'cross-fetch'
Alos 檢查瀏覽器和作業系統支持:https ://caniuse.com/#search=fetch
Redux 官方檔案說:https ://redux.js.org/tutorials/fundamentals/part-6-async-logic#note-on-fetch
uj5u.com熱心網友回復:
在 redux 中,你永遠不應該改變你從引數中得到的狀態。相反,您應該回傳要更改的值和不想更改的值。因為這個你有這個問題。你應該做這個:
case GET_VISIT_LIST:
return {
...state,
isworking: true,
error: null
}
改變你從引數中得到的狀態redux toolkit是正確的,你可以使用redux toolkit
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/445724.html
標籤:javascript 反应 还原
上一篇:for...的迭代器而不關閉它
