Last active
March 4, 2024 20:18
-
-
Save thechaudharysab/59a6f69ed7a7fcce5ac6857a0ef42678 to your computer and use it in GitHub Desktop.
Redux Hello World implemented in React Native. Tutorial Link: https://medium.com/@ibjects/tutorial-redux-react-redux-and-redux-persist-from-scratch-28249ee914e7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { NavigationContainer } from '@react-navigation/native'; | |
import { createStackNavigator } from '@react-navigation/stack'; | |
import Home from './src/screens/Home'; | |
import ReduxExample from './src/screens/ReduxExample'; | |
import { createStore } from 'redux'; | |
import { Provider } from 'react-redux'; | |
import { rootReducer } from './src/utils/redux/reducers'; | |
const Stack = createStackNavigator(); | |
function App(props) { | |
return ( | |
<Provider store={store}> | |
<NavigationContainer theme={SukhanTheme}> | |
<Stack.Navigator> | |
<Stack.Screen name="Home" component={Home} /> | |
<Stack.Screen name="ReduxExample" component={ReduxExample} /> | |
</Stack.Navigator> | |
</NavigationContainer> | |
</Provider> | |
); | |
} | |
export const store = createStore(rootReducer); | |
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const InitialState = { count: 0 } | |
function rootReducer(state = InitialState, action) { | |
switch (action?.type) { | |
case 'counter/increment': return { ...state, count: state.count + 1 }; | |
case 'counter/decrement': return { ...state, count: state.count - 1 }; | |
default: return state; | |
} | |
}; | |
export { rootReducer }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
// import { store } from '../../App'; | |
import { View, Text, Button } from 'react-native'; | |
import { useSelector, useDispatch } from 'react-redux' | |
import { rootReducer } from '../utils/redux/reducers'; | |
function ReduxExample(props) { | |
const counter = useSelector(rootReducer); | |
const dispatch = useDispatch(); | |
return ( | |
<View> | |
<Text>Redux Hello World</Text> | |
<Text>Counter {counter.count}</Text> | |
<View style={{ flexDirection: 'row', justifyContent: 'space-between', margin: 24 }} > | |
<Button title='Increment' onPress={() => { dispatch({ type: 'counter/increment' }) }} /> | |
<Button title='Decrement' onPress={() => { dispatch({ type: 'counter/decrement' }) }} /> | |
</View> | |
</View> | |
); | |
} | |
export default ReduxExample; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment