Skip to content

Instantly share code, notes, and snippets.

View parth-koshta's full-sized avatar
:electron:

Parth N Koshta parth-koshta

:electron:
View GitHub Profile
@parth-koshta
parth-koshta / Login.js
Created December 26, 2021 16:40
Medium article - redux setup
import React from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
import {useDispatch} from 'react-redux';
import {setIsLoggedIn} from '../../redux/actions/UserActions';
const Login = () => {
const dispatch = useDispatch();
const onLogin = () => {
dispatch(setIsLoggedIn(true));
};
@parth-koshta
parth-koshta / App.js
Created December 26, 2021 16:39
Medium article - redux setup
import React from 'react';
import {Provider} from 'react-redux';
import store, {persistor} from './src/redux/store';
import AppWrapper from './src/components/AppWrapper';
import {PersistGate} from 'redux-persist/integration/react';
const App = () => {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
@parth-koshta
parth-koshta / AppWrapper.js
Created December 26, 2021 16:38
Medium article - redux setup
import React from 'react';
import {useSelector} from 'react-redux';
import {selectIsLoggedIn} from '../../redux/selectors/UserSelector';
import Counter from '../../screens/counter';
import Login from '../../screens/login';
const AppWrapper = () => {
const isLoggedIn = useSelector(selectIsLoggedIn);
return !!isLoggedIn ? <Counter /> : <Login />;
};
@parth-koshta
parth-koshta / CounterSelector.js
Created December 26, 2021 16:35
Medium article - redux setup
import { createSelector } from 'reselect';
export const selectCounterValue = createSelector(
(state) => state.counter,
(counter) => counter.value,
);
@parth-koshta
parth-koshta / UserSelector.js
Created December 26, 2021 16:35
Medium article - redux setup
import { createSelector } from 'reselect';
export const selectIsLoggedIn = createSelector(
(state) => state.user,
(user) => user.isLoggedIn,
);
@parth-koshta
parth-koshta / store.js
Created December 26, 2021 16:30
Medium article - redux setup
import {configureStore} from '@reduxjs/toolkit';
import logger from 'redux-logger';
import {
persistStore,
persistReducer,
FLUSH,
REHYDRATE,
PAUSE,
PERSIST,
PURGE,
@parth-koshta
parth-koshta / RootReducer.js
Created December 26, 2021 15:05
Medium article - redux setup
import { combineReducers } from 'redux';
import CounterReducer from './CounterReducer';
import UserReducer from './UserReducer';
const Reducers = combineReducers({
user: UserReducer,
counter: CounterReducer
});
export default Reducers;
@parth-koshta
parth-koshta / CounterReducer.js
Created December 26, 2021 15:03
Medium article - redux setup
import {createReducer} from '@reduxjs/toolkit';
import {PURGE} from 'redux-persist';
import {CounterActionTypes} from '../actions/CounterActions';
const initialState = {value: 0};
const CounterReducer = createReducer(initialState, builder => {
builder
.addCase(CounterActionTypes.INCREMENT, state => {
state.value++;
@parth-koshta
parth-koshta / CounterActions.js
Created December 26, 2021 14:56
Medium article - redux setup
import { createAction } from '@reduxjs/toolkit';
export const CounterActionTypes = {
INCREMENT: 'INCREMENT',
DECREMENT: 'DECREMENT',
};
export const incrementCount = createAction(CounterActionTypes.INCREMENT);
export const decrementCount = createAction(CounterActionTypes.DECREMENT);
@parth-koshta
parth-koshta / UserActions.js
Last active December 26, 2021 14:59
Medium article - redux setup
import {createAction} from '@reduxjs/toolkit';
// createAction function is used to define a redux action,
// it takes an action type and returns an action creator for that type.
// The action creator can be called either without arguments or with a payload to be attached to the action.
export const UserActionTypes = {
SET_IS_LOGGED_IN: 'SET_IS_LOGGED_IN',
};
export const setIsLoggedIn = createAction(UserActionTypes.SET_IS_LOGGED_IN);