Skip to content

Commit

Permalink
integration with redux
Browse files Browse the repository at this point in the history
  • Loading branch information
Digvijay Upadhyay committed Aug 29, 2018
1 parent 9853cd9 commit eed332c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 15 deletions.
35 changes: 33 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-router-dom": "^4.3.1"
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"redux": "^4.0.0"
}
}
4 changes: 4 additions & 0 deletions src/Actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const textChange = text => ({
type: 'TEXT_CHANGE',
text
})
6 changes: 1 addition & 5 deletions src/App.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
body {
font-size: 13px;

div {
margin: auto;
padding: 20px;
}
padding: 2rem;
}
37 changes: 33 additions & 4 deletions src/Pages/Home.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { textChange } from '../Actions';

class Home extends Component {
render() {
let input
return (
<h2>
Home Page
</h2>
<div>
<h2>
Home Page
</h2>
<div>Write a name and check it in SomePage Link</div>
<input
type="text"
onChange={e =>
{
this.props.updateText(input.value)
}
}
ref={node => {
input = node
}}
/>
<div>
You typed: {this.props.text}
</div>
</div>
);
}
}

export default Home;
const mapStateToText = state => ({
text: state.appReducer.text,
});

const mapDispatch = dispatch => ({
updateText: text => dispatch(textChange(text))
});


export default connect(mapStateToText, mapDispatch)(Home);
19 changes: 19 additions & 0 deletions src/Reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { combineReducers } from 'redux';

// you can keep this reducer in another file and import it from there
// I have kept it here for simplifying
// I would recommend you to break the reducers and actions as well
const appReducer = (state = {}, action) => {
switch (action.type) {
case 'TEXT_CHANGE':
return {
text: action.text
}
default:
return state
}
}

export default combineReducers({
appReducer
});
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import React from 'react';
import './App.scss'
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import rootReducer from './Reducers'
import App from './App';
import { HashRouter } from 'react-router-dom'

const store = createStore(rootReducer)

const render = () =>
ReactDOM.render(
<HashRouter>
<App/>
</HashRouter>,
<Provider store={store}>
<HashRouter>
<App/>
</HashRouter>
</Provider>,
document.getElementById('index'));

render();

0 comments on commit eed332c

Please sign in to comment.