Last active
June 2, 2020 07:34
-
-
Save kumar-aakash86/e2a801ea71d21104e3ff59e8adea3eef to your computer and use it in GitHub Desktop.
Setting up React Native with Redux
This file contains hidden or 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
| npm install --save redux | |
| npm install --save react-redux | |
| Create following folders on root | |
| App | |
| -> store | |
| -> reducers | |
| -> actions | |
| -> components | |
| Create index.js inside actions folder | |
| In reducers folder create two file userReducer.js & index.js | |
| # See content below | |
| Create store.js inside store folder | |
| In App.js add code for store |
This file contains hidden or 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
| export const ACTIVE_TEXT_CHANGE = 'ACTIVE_TEXT_CHANGE'; | |
| export const changeText = (text) =>{ | |
| return { | |
| type: ACTIVE_TEXT_CHANGE, | |
| payload: text | |
| } | |
| }; |
This file contains hidden or 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 store from "./store/store"; | |
| import { Provider } from "react-redux"; | |
| export default class App extends React.Component { | |
| render() { | |
| return ( | |
| <View style={{ flex: 1 }}> | |
| <StatusBar | |
| backgroundColor={theme.PRIMARY_COLOR} | |
| barStyle="light-content"/> | |
| <Provider store={store}> | |
| <HomeView /> | |
| </Provider> | |
| </View> | |
| ); | |
| } | |
| } |
This file contains hidden or 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 { connect } from "react-redux"; | |
| import * as actions from '../actions/index'; | |
| // call this on button click | |
| // this.props.changeText('Aakash Kumar'); | |
| mapStateToProps = (state) => { | |
| return { | |
| username: state.userReducer.visibleTracker, | |
| }; | |
| } | |
| mapDispatchToProps = (dispatch) => { | |
| return{ | |
| changeText: (text) => dispatch(actions.changeText(text)) | |
| } | |
| } | |
| export default connect(mapStateToProps, mapDispatchToProps)(HomeView); |
This file contains hidden or 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 { combineReducers } from "redux"; | |
| import userReducer from "./userReducer"; | |
| const rootReducer = combineReducers({ | |
| userReducer | |
| }); | |
| export default rootReducer; |
This file contains hidden or 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 * as actions from "../actions/index"; | |
| const initialState = { | |
| username: "[username]" | |
| } | |
| export default (state=initialState, action) => { | |
| switch(action.type){ | |
| case actions.ACTIVE_TEXT_CHANGE: | |
| return {...state, username: action.payload}; | |
| break; | |
| } | |
| return state; | |
| } |
This file contains hidden or 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 { createStore } from "redux"; | |
| import reducers from "./reducers/index"; | |
| export default createStore(reducers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment