Я пытаюсь внедрить redux-persist в свой проект React. Redux работал нормально, но когда я реализовывал redux-persist, я начал получать эту ошибку:
Types of property 'dispatch' are incompatible.
Type 'Dispatch<{ type: string; payload: any; }>' is not assignable to type 'Dispatch<AnyAction>'.
Type 'AnyAction' is not assignable to type '{ type: string; payload: any; }'. TS2345
19 | const store = createStore(persistedReducer)
20 |
> 21 | let persistor = persistStore(store)
| ^
22 |
23 | ReactDOM.render(
24 | <Provider store={store}>
Я предполагаю, что это связано с моим редуктором и тем, как я определяю свойства в Действие, но я не смог понять это.
index.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import * as serviceWorker from './serviceWorker';
import {Provider} from 'react-redux';
import {createStore} from 'redux'
import reducer from './redux/reducer';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web
import { PersistGate } from 'redux-persist/integration/react';
const persistConfig = {
key: 'root',
storage
}
const persistedReducer = persistReducer(persistConfig, reducer)
const store = createStore(persistedReducer)
let persistor = persistStore(store)
ReactDOM.render(
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
<App />
</PersistGate>
</Provider>,
document.getElementById('root'));
serviceWorker.unregister();
reducer.ts
const defaultState = {
user: {}
}
export default function reducer(
state = defaultState,
{type, payload} : {type: string; payload: any}
): any {
switch(type){
case 'SET_USER_STATE':
return {
...state,
user: {
username:payload
}
}
}
return state
}
actions.tx
export const setUserState = (payload: any) => {
return { type: 'SET_USER_STATE', payload}
}