Skip to content
Snippets Groups Projects
Commit 7ba61a1d authored by Tadeusz Miesiąc's avatar Tadeusz Miesiąc
Browse files

feat(redux-toolkit init): initialised redux-toolkit and created PoC

parent 821ac306
No related branches found
No related tags found
3 merge requests!223reset the pin numbers before search results are fetch (so the results will be...,!16Feature/setup redux async,!15Feature/setup redux
Pipeline #78753 passed
...@@ -65,7 +65,9 @@ ...@@ -65,7 +65,9 @@
"**/jest.setup.ts", // jest setup "**/jest.setup.ts", // jest setup
"**/setupTests.ts" "**/setupTests.ts"
], ],
"optionalDependencies": false "optionalDependencies": false,
"peerDependencies": false,
"packageDir": "./"
} }
], ],
"indent": ["error", 2], "indent": ["error", 2],
...@@ -83,6 +85,12 @@ ...@@ -83,6 +85,12 @@
"rules": { "rules": {
"@typescript-eslint/explicit-function-return-type": "error" "@typescript-eslint/explicit-function-return-type": "error"
} }
},
{
// feel free to replace with your preferred file pattern - eg. 'src/**/*Slice.ts'
"files": ["src/**/*.slice.ts", "src/**/*.reducers.ts"],
// avoid state param assignment
"rules": { "no-param-reassign": ["error", { "props": false }] }
} }
], ],
"settings": { "settings": {
......
This diff is collapsed.
import { AppWrapper } from '@/components/AppWrapper';
import type { AppProps } from 'next/app';
const MyApp = ({ Component, pageProps }: AppProps): JSX.Element => (
<AppWrapper>
<Component {...pageProps} />
</AppWrapper>
);
export default MyApp;
import { useDispatch, useSelector } from 'react-redux';
import { selectSearchValue } from '@/redux/search/search.selectors';
import { setSearchValue } from '@/redux/search/search.slice';
const ReduxPage = (): JSX.Element => {
const dispatch = useDispatch();
const searchValue = useSelector(selectSearchValue);
const triggerSyncUpdate = (): void => {
// eslint-disable-next-line prefer-template
const newValue = searchValue + 'test';
dispatch(setSearchValue(newValue));
};
return (
<div>
{searchValue}
<button type="button" onClick={triggerSyncUpdate}>
sync update
</button>
</div>
);
};
export default ReduxPage;
import { ReactNode } from 'react';
import { Provider } from 'react-redux';
import { store } from '@/redux/store';
interface AppWrapperProps {
children: ReactNode;
}
export const AppWrapper = ({ children }: AppWrapperProps): JSX.Element => (
<Provider store={store}>{children}</Provider>
);
export { AppWrapper } from './AppWrapper.component';
import { useDispatch } from 'react-redux';
import { AppDispatch } from '@/redux/store';
export const useAppDispatch: () => AppDispatch = useDispatch;
import { useSelector } from 'react-redux';
import type { TypedUseSelectorHook } from 'react-redux';
import { RootState } from '@/redux/store';
// Use throughout your app instead of plain `useDispatch` and `useSelector`
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
// updating state
import { SearchState } from '@/redux/search/search.types';
import { PayloadAction } from '@reduxjs/toolkit';
export const setSearchValueReducer = (state: SearchState, action: PayloadAction<string>): void => {
state.searchValue = action.payload;
};
import type { RootState } from '@/redux/store';
// THIS IS EXAMPLE, it's not memoised!!!! Check redux-tookit docs.
export const selectSearchValue = (state: RootState): string => state.search.searchValue;
import { createSlice } from '@reduxjs/toolkit';
import { SearchState } from '@/redux/search/search.types';
import { setSearchValueReducer } from '@/redux/search/search.reducers';
const initialState: SearchState = {
searchValue: '',
searchResult: {
content: '',
drugs: '',
},
};
export const searchSlice = createSlice({
name: 'search',
initialState,
reducers: {
setSearchValue: setSearchValueReducer,
},
});
export const { setSearchValue } = searchSlice.actions;
export default searchSlice.reducer;
export interface SearchResult {
content: string;
drugs: string;
}
export interface SearchState {
searchValue: string;
searchResult: SearchResult;
}
import { configureStore } from '@reduxjs/toolkit';
import searchReducer from './search/search.slice';
export const store = configureStore({
reducer: {
search: searchReducer,
},
devTools: true,
});
// 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;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment