Redux 没有在反应中更新状态
Redux is not updating state in react
Redux 仅在 Safari 浏览器中不更新状态,但在 chrome 中它工作正常。当我在 VISITS_SEARCH 情况下控制操作时:我在 Safari 中得到空数组。但是,我在 chrome console
中得到了有效数组
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;
}
});
};
我假设您使用 fetch 填充数据,根据 Redux 文档,这是常见问题:
We use fetch API in the examples. It is a new API for making network
requests that replaces XMLHttpRequest for most common needs. Because
most browsers don't yet support it natively, we suggest that you use
cross-fetch library:
// Do this in every file where you use `fetch`
import fetch from 'cross-fetch'
Internally, it uses whatwg-fetch polyfill on the
client, and node-fetch on the server, so you won't need to change API
calls if you change your app to be universal.
Be aware that any fetch polyfill assumes a Promise polyfill is already
present. The easiest way to ensure you have a Promise polyfill is to
enable Babel's ES6 polyfill in your entry point before any other code
runs:
// Do this once before any other code in your app import
'babel-polyfill'
这是因为某些 browser
不支持 fetch
,这就是为什么 react
告诉您使用 fetch
,如下所示:
// Do this in every file where you use `fetch`
import fetch from 'cross-fetch'
Alos 检查浏览器和 OS 支持:https://caniuse.com/#search=fetch
官方 Redux 文档说:https://redux.js.org/tutorials/fundamentals/part-6-async-logic#note-on-fetch
在 redux 中,你永远不应该改变你从参数中获得的状态。相反,您应该 return 您想要更改的值和您不想更改的值。因为这个你有这个问题。你应该这样做:
case GET_VISIT_LIST:
return {
...state,
isworking: true,
error: null
}
更改您从 redux toolkit
中的参数获得的状态是正确的,您可以使用 redux toolkit
我修好了。浪费时间检查了它,它与 fetch 根本没有关系。在按日期排序的函数中,日期为 9999-1-1,这对 Safari 无效,我不得不更改为 9999-01-01
Redux 仅在 Safari 浏览器中不更新状态,但在 chrome 中它工作正常。当我在 VISITS_SEARCH 情况下控制操作时:我在 Safari 中得到空数组。但是,我在 chrome console
中得到了有效数组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;
}
});
};
我假设您使用 fetch 填充数据,根据 Redux 文档,这是常见问题:
We use fetch API in the examples. It is a new API for making network requests that replaces XMLHttpRequest for most common needs. Because most browsers don't yet support it natively, we suggest that you use cross-fetch library:
// Do this in every file where you use `fetch` import fetch from 'cross-fetch'
Internally, it uses whatwg-fetch polyfill on the client, and node-fetch on the server, so you won't need to change API calls if you change your app to be universal.
Be aware that any fetch polyfill assumes a Promise polyfill is already present. The easiest way to ensure you have a Promise polyfill is to enable Babel's ES6 polyfill in your entry point before any other code runs:
// Do this once before any other code in your app import 'babel-polyfill'
这是因为某些 browser
不支持 fetch
,这就是为什么 react
告诉您使用 fetch
,如下所示:
// Do this in every file where you use `fetch`
import fetch from 'cross-fetch'
Alos 检查浏览器和 OS 支持:https://caniuse.com/#search=fetch
官方 Redux 文档说:https://redux.js.org/tutorials/fundamentals/part-6-async-logic#note-on-fetch
在 redux 中,你永远不应该改变你从参数中获得的状态。相反,您应该 return 您想要更改的值和您不想更改的值。因为这个你有这个问题。你应该这样做:
case GET_VISIT_LIST:
return {
...state,
isworking: true,
error: null
}
更改您从 redux toolkit
中的参数获得的状态是正确的,您可以使用 redux toolkit
我修好了。浪费时间检查了它,它与 fetch 根本没有关系。在按日期排序的函数中,日期为 9999-1-1,这对 Safari 无效,我不得不更改为 9999-01-01