REDUX:Dispatch 未更新商店
REDUX: Dispatch is not update the store
我正在开发一个生成 chrome 扩展的项目。我正在尝试在 popup.tsx 中调度函数。但是不派送。我的商店不更新。相同的代码在后台页面中工作。我能做什么?可能是什么问题?
这是我的 popup.tsx:
useEffect(() => {
if (
!ip4Cache.hasOwnProperty(hostname) ||
!geoIPcache.hasOwnProperty(hostname)
) {
console.log('cachede yok')
checkLookup([hostname]).then((res) => {
setIp4(res.data['ip4'][hostname])
dispatch(addIP4(res.data.ip4)) //I also tried store.dispatch in here
})
}
}, [hostname])
这是我的商店:
let sagaMiddleware = createSagaMiddleware()
const localStorage = createLocalStorage()
const persistConfig = {
key: 'root',
storage: localStorage,
}
const reducers = combineReducers({
ip4: ip4Reducer,
})
export type RootState = ReturnType<typeof reducers>
const createPersistReducer = (config) => persistReducer(config, reducers)
export const store = configureStore({
reducer: createPersistReducer(persistConfig),
middleware: [sagaMiddleware],
})
export const persistor = persistStore(store)
sagaMiddleware.run(rootSaga)
wrapStore(store, { portName: 'bla' })
最后一个,IP4 切片:
import { createSlice } from '@reduxjs/toolkit'
export const ip4Slice = createSlice({
name: 'ip4',
initialState: {},
reducers: {
addIP4: (state, action) =>
(state = new Object({ ...state, ...action.payload })),
},
})
export const { addIP4 } = ip4Slice.actions
export default ip4Slice.reducer
reducers: {
addIP4: (state, action) => {
// either:
Object.assign(state, action.payload);
// or
return { ...state, ...action.payload }
}
},
请阅读Writing Reducers with Immer documentation page, especially to Resetting and replacing state。
引用最重要的部分:
A common mistake is to try assigning state = someValue
directly. This will not work! This only points the local state variable to a different reference. That is neither mutating the existing state object/array in memory, nor returning an entirely new value, so Immer does not make any actual changes.
我正在开发一个生成 chrome 扩展的项目。我正在尝试在 popup.tsx 中调度函数。但是不派送。我的商店不更新。相同的代码在后台页面中工作。我能做什么?可能是什么问题?
这是我的 popup.tsx:
useEffect(() => {
if (
!ip4Cache.hasOwnProperty(hostname) ||
!geoIPcache.hasOwnProperty(hostname)
) {
console.log('cachede yok')
checkLookup([hostname]).then((res) => {
setIp4(res.data['ip4'][hostname])
dispatch(addIP4(res.data.ip4)) //I also tried store.dispatch in here
})
}
}, [hostname])
这是我的商店:
let sagaMiddleware = createSagaMiddleware()
const localStorage = createLocalStorage()
const persistConfig = {
key: 'root',
storage: localStorage,
}
const reducers = combineReducers({
ip4: ip4Reducer,
})
export type RootState = ReturnType<typeof reducers>
const createPersistReducer = (config) => persistReducer(config, reducers)
export const store = configureStore({
reducer: createPersistReducer(persistConfig),
middleware: [sagaMiddleware],
})
export const persistor = persistStore(store)
sagaMiddleware.run(rootSaga)
wrapStore(store, { portName: 'bla' })
最后一个,IP4 切片:
import { createSlice } from '@reduxjs/toolkit'
export const ip4Slice = createSlice({
name: 'ip4',
initialState: {},
reducers: {
addIP4: (state, action) =>
(state = new Object({ ...state, ...action.payload })),
},
})
export const { addIP4 } = ip4Slice.actions
export default ip4Slice.reducer
reducers: {
addIP4: (state, action) => {
// either:
Object.assign(state, action.payload);
// or
return { ...state, ...action.payload }
}
},
请阅读Writing Reducers with Immer documentation page, especially to Resetting and replacing state。
引用最重要的部分:
A common mistake is to try assigning
state = someValue
directly. This will not work! This only points the local state variable to a different reference. That is neither mutating the existing state object/array in memory, nor returning an entirely new value, so Immer does not make any actual changes.