// Infer the `RootState` and `AppDispatch` types from the store itself
export type RootState = ReturnType<typeof store.getState>
// Inferred type: {posts: PostsState, comments: CommentsState, users: UsersState}
export type AppDispatch = typeof store.dispatch
2.component 內 useDispatch 加上 type
const dispatch = useDispatch<AppDispatch>();
Redux toolkit 結合 localstorage
用途:存入 state 到 localstorage,使用 redux-localstorage-simple 套件
store.js
import { configureStore } from '@reduxjs/toolkit';
import { combineReducers} from 'redux';
import { save, load } from "redux-localstorage-simple"
import user from './user.js';
const reducer = combineReducers({
user
})
const store = configureStore({
reducer,
middleware: (getDefaultMiddleware) => [
...getDefaultMiddleware(),
save(),
],
preloadedState: load(),
})
export default store;
如果用 next.js 出現 Hydration failed because the initial UI does not match what was rendered on the server