React-native return 中的 UseSelector 未定义但 redux 中的变量得到更新
UseSelector in react-native return undefined but variable in redux get updated
我尝试使用 UseSelector 在 redux 中获取数据,但它总是返回未定义的,但是当我尝试记录 console.log 我想在 redux 中获取的变量时,它得到了更新,但是当我尝试控制台记录它返回的 useSelector undefined
代码 Redux :
export const EXCHAHNGETOSECOND = 'EXCHANGETOSECOND'
export const exchangetosecwork = (hour,minute,second) =>({
type:EXCHAHNGETOSECOND,
payloadhourwork : hour,
payloadminutework : minute,
payloadsecondwork : second
})
let initialState ={
resultWorkinSecond : 0
}
export const mainReducer = (state=initialState, action) =>{
switch (action.type) {
case EXCHAHNGETOSECOND:
const hourTosecond = action.payloadhourwork * 3600
const minuteTosecond = action.payloadminutework * 60
const res = hourTosecond+minuteTosecond+action.payloadsecondwork
state.resultWorkinSecond = res
console.log(state.resultWorkinSecond);
return state.resultWorkinSecond
default:
return state
}
}
export default mainReducer
存储 redux :
import { createStore } from "redux";
import mainReducer from "./redux";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { persistStore, persistReducer } from 'redux-persist'
const persistConfig={
key:'root',
storage:AsyncStorage,
};
const persistedReducer = persistReducer(persistConfig,mainReducer)
export default() => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return{ store, persistor }
}
myConsole 日志测试:
const resWorkinSec = useSelector((state)=>state.resultWorkinSecond)
<Pressable onPress={()=>{
dispatch(exchangetosecwork(hour,minute,second))
console.log(resWorkinSec)
}}>
[控制台日志结果1
您的减速器有多个错误:
- 它做一个赋值,改变状态。这在旧版 Redux reducer 中是不允许的,但仅在使用
createSlice
的现代 Redux 中是不允许的
return state.resultWorkinSecond
然后甚至改变完整的状态结构。
正确的遗留 Redux 代码如下所示:
export const mainReducer = (state=initialState, action) =>{
switch (action.type) {
case EXCHAHNGETOSECOND:
const hourTosecond = action.payloadhourwork * 3600
const minuteTosecond = action.payloadminutework * 60
const res = hourTosecond+minuteTosecond+action.payloadsecondwork
return { ...state, resultWorkinSecond: res }
default:
return state
}
}
综上所述,这是我们自 2019 年以来不再推荐的旧 Redux 风格。它更复杂,并且 error-prone。大约是现代 Redux 代码的 4 倍。现代 Redux 不使用 switch..case reducers,ACTION_TYPES,不可变的 reducer 逻辑(就像你需要在这里做的那样),createStore
等等。
我强烈建议您暂时放下手头的工作,仔细阅读 official Redux tutorial。
我尝试使用 UseSelector 在 redux 中获取数据,但它总是返回未定义的,但是当我尝试记录 console.log 我想在 redux 中获取的变量时,它得到了更新,但是当我尝试控制台记录它返回的 useSelector undefined
代码 Redux :
export const EXCHAHNGETOSECOND = 'EXCHANGETOSECOND'
export const exchangetosecwork = (hour,minute,second) =>({
type:EXCHAHNGETOSECOND,
payloadhourwork : hour,
payloadminutework : minute,
payloadsecondwork : second
})
let initialState ={
resultWorkinSecond : 0
}
export const mainReducer = (state=initialState, action) =>{
switch (action.type) {
case EXCHAHNGETOSECOND:
const hourTosecond = action.payloadhourwork * 3600
const minuteTosecond = action.payloadminutework * 60
const res = hourTosecond+minuteTosecond+action.payloadsecondwork
state.resultWorkinSecond = res
console.log(state.resultWorkinSecond);
return state.resultWorkinSecond
default:
return state
}
}
export default mainReducer
存储 redux :
import { createStore } from "redux";
import mainReducer from "./redux";
import AsyncStorage from '@react-native-async-storage/async-storage';
import { persistStore, persistReducer } from 'redux-persist'
const persistConfig={
key:'root',
storage:AsyncStorage,
};
const persistedReducer = persistReducer(persistConfig,mainReducer)
export default() => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return{ store, persistor }
}
myConsole 日志测试:
const resWorkinSec = useSelector((state)=>state.resultWorkinSecond)
<Pressable onPress={()=>{
dispatch(exchangetosecwork(hour,minute,second))
console.log(resWorkinSec)
}}>
[控制台日志结果1
您的减速器有多个错误:
- 它做一个赋值,改变状态。这在旧版 Redux reducer 中是不允许的,但仅在使用
createSlice
的现代 Redux 中是不允许的
return state.resultWorkinSecond
然后甚至改变完整的状态结构。
正确的遗留 Redux 代码如下所示:
export const mainReducer = (state=initialState, action) =>{
switch (action.type) {
case EXCHAHNGETOSECOND:
const hourTosecond = action.payloadhourwork * 3600
const minuteTosecond = action.payloadminutework * 60
const res = hourTosecond+minuteTosecond+action.payloadsecondwork
return { ...state, resultWorkinSecond: res }
default:
return state
}
}
综上所述,这是我们自 2019 年以来不再推荐的旧 Redux 风格。它更复杂,并且 error-prone。大约是现代 Redux 代码的 4 倍。现代 Redux 不使用 switch..case reducers,ACTION_TYPES,不可变的 reducer 逻辑(就像你需要在这里做的那样),createStore
等等。
我强烈建议您暂时放下手头的工作,仔细阅读 official Redux tutorial。