Skip to content

Instantly share code, notes, and snippets.

@kumar-aakash86
Last active June 2, 2020 07:34
Show Gist options
  • Save kumar-aakash86/e2a801ea71d21104e3ff59e8adea3eef to your computer and use it in GitHub Desktop.
Save kumar-aakash86/e2a801ea71d21104e3ff59e8adea3eef to your computer and use it in GitHub Desktop.
Setting up React Native with Redux
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
export const ACTIVE_TEXT_CHANGE = 'ACTIVE_TEXT_CHANGE';
export const changeText = (text) =>{
return {
type: ACTIVE_TEXT_CHANGE,
payload: text
}
};
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>
);
}
}
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);
import { combineReducers } from "redux";
import userReducer from "./userReducer";
const rootReducer = combineReducers({
userReducer
});
export default rootReducer;
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;
}
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