Answer the question
In order to leave comments, you need to log in
Why are they not saved to React + Redux stores?
I'm trying to add a store to the application so that when the data in the input changes, the store is updated. in the input component, you need to get the value from the store, but the state is always empty, when writing code, typeScript does not highlight possible fields, + in the browser in the redux store initialState is also not visible, I
create a store:
```typescript
const rootReducer = combineReducers({
slice: slice
} )
export const setupStore = () => {
return configureStore({
reducer: rootReducer
})
}
export type RootState = ReturnType
export type AppStore = ReturnType
export type AppDispatch = AppStore['dispatch']
create initial state and slice:
```typescript
```
interface InterfaceSlice {
application: array[],
}
const initialState: InterfaceSlice = {
application: [
{id: 0, name: "alice" }
],
}
export const userSlice: any = createSlice({
name: 'user',
initialState: initialState,
reducers: {{
getUser(state, action: PayloadAction) {
state.application = [...action.payload]
}
}}
})
export default userSlice.reducer;
```
dispatch to store data :
```typescript
const {getUser} = userSlice.actions
const dispatch = useAppDispatch()
const {application} = useAppSelector((state) => state.userSlice) //doesn't see that state.userSlice has an application field
useEffect(() => {
getRequest().then((request) => {
dispatch( getUser(request.data))
})
}, []);
```
In devtools, the state is always an empty object, although it is clear that the payload contains the data that I need,
maybe there is an error in the logic somewhere?
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question