我正在try 理解(低效地)useSelector如何引用存储在Slice中的const count = useSelector((state) => state.counter.value)
行的InitialState
参考文档,这里是Slice:
import { createSlice } from '@reduxjs/toolkit'
const initialState = {
value: 0,
}
export const counterSlice = createSlice({
name: 'counter',
initialState,
reducers: {
increment: (state) => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the Immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
state.value += 1
},
decrement: (state) => {
state.value -= 1
},
incrementByAmount: (state, action) => {
state.value += action.payload
},
},
})
// Action creators are generated for each case reducer function
export const { increment, decrement, incrementByAmount } = counterSlice.actions
export default counterSlice.reducer
这是store :
import { configureStore } from '@reduxjs/toolkit'
import counterReducer from '../features/counter/counterSlice'
export const store = configureStore({
reducer: {
counter: counterReducer,
},
})
在组件计数器中,我正在try 访问InitialState:
...
export function Counter() {
const count = useSelector((state) => state.counter.value)
const dispatch = useDispatch()
return (
<div>
...
</div>
)
}
那么,如果在CounterSlice中默认只导出Reducer对象,Const count=useSelector((State)=>;state.Counter.value)如何引用Slice的InitialState呢?
export default counterSlice.reducer
个