Skip to content

Commit 33ff640

Browse files
committedAug 27, 2017
react redux container components added
1 parent 183b5ce commit 33ff640

File tree

12 files changed

+184
-32
lines changed

12 files changed

+184
-32
lines changed
 

‎react-redux-client/package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44
"private": true,
55
"dependencies": {
66
"react": "^15.6.1",
7+
"react-bootstrap": "^0.31.2",
78
"react-dom": "^15.6.1",
8-
"react-scripts": "1.0.11"
9+
"react-redux": "^5.0.6",
10+
"react-router": "^4.2.0",
11+
"react-router-bootstrap": "^0.24.2",
12+
"react-router-redux": "^4.0.8",
13+
"react-scripts": "1.0.11",
14+
"redux": "^3.7.2",
15+
"redux-thunk": "^2.2.0"
916
},
1017
"scripts": {
1118
"start": "react-scripts start",
1219
"build": "react-scripts build",
1320
"test": "react-scripts test --env=jsdom",
1421
"eject": "react-scripts eject"
1522
}
16-
}
23+
}

‎react-redux-client/src/App.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1+
// ./react-redux-client/src/App.js
12
import React, { Component } from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
3+
import { Provider } from 'react-redux';
4+
import { Router, browserHistory } from 'react-router';
5+
import PropTypes from 'prop-types';
6+
import { syncHistoryWithStore } from 'react-router-redux';
7+
import configureStore from './store/configureStore';
8+
import routes from './routes';
9+
10+
const store = configureStore();
11+
const history = syncHistoryWithStore(browserHistory, store);
412

513
class App extends Component {
614
render() {
715
return (
8-
<div className="App">
9-
<div className="App-header">
10-
<img src={logo} className="App-logo" alt="logo" />
11-
<h2>Welcome to React</h2>
16+
<Provider store={store}>
17+
<div>
18+
<Router history={history} routes={routes} />
1219
</div>
13-
<p className="App-intro">
14-
To get started, edit <code>src/App.js</code> and save to reload.
15-
</p>
16-
</div>
20+
</Provider>
1721
);
1822
}
1923
}
2024

25+
Root.propTypes = {
26+
store: PropTypes.object.isRequired,
27+
history: PropTypes.object.isRequired
28+
}
29+
2130
export default App;

‎react-redux-client/src/App.test.js

-8
This file was deleted.
File renamed without changes.
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React from 'react';
2+
import { Navbar,Nav,NavItem,MenuItem } from 'react-bootstrap';
3+
import { LinkContainer } from 'react-router-bootstrap';
4+
import './App.css';
5+
6+
export default class App extends React.Component {
7+
constructor(props){
8+
super(props);
9+
}
10+
11+
render(){
12+
return(
13+
<div>
14+
<Navbar inverse collapseOnSelect className="customNav">
15+
<Navbar.Header>
16+
<Navbar.Brand>
17+
<a href="#">BookLibrary</a>
18+
</Navbar.Brand>
19+
<Navbar.Toggle />
20+
</Navbar.Header>
21+
<Navbar.Collapse>
22+
<Nav>
23+
<LinkContainer to="/">
24+
<NavItem eventKey={1}>Home</NavItem>
25+
</LinkContainer>
26+
<LinkContainer to="/books">
27+
<NavItem eventKey={2}>Books</NavItem>
28+
</LinkContainer>
29+
<LinkContainer to="/favourites">
30+
<NavItem eventKey={3}>Favourites</NavItem>
31+
</LinkContainer>
32+
</Nav>
33+
<Nav pullRight>
34+
{this.props.location.pathname === '/books' &&
35+
<LinkContainer to="/" onClick={this.toggleAddBook}>
36+
<NavItem eventKey={1}>Add Book</NavItem>
37+
</LinkContainer>
38+
}
39+
</Nav>
40+
</Navbar.Collapse>
41+
</Navbar>
42+
<div className="container">
43+
{ /* Each Smaller Components */}
44+
{this.props.children}
45+
</div>
46+
</div>
47+
);
48+
}
49+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ./react-redux-client/src/containers/App.js
2+
import { connect } from 'react-redux';
3+
import * as appActions from '../actions/appActions';
4+
import App from '../components/App';
5+
6+
// map state from store to props
7+
const mapStateToProps = (state) => {
8+
return {
9+
//you can now say this.props.mappedAppSate
10+
mappedAppState: state.appState
11+
}
12+
}
13+
14+
// map actions to props
15+
const mapDispatchToProps = (dispatch) => {
16+
return {
17+
//you can now say this.props.mappedAppActions
18+
mappedToggleAddTodo: () => dispatch(appActions.toggleAddTodo())
19+
}
20+
}
21+
22+
export default connect(mapStateToProps,mapDispatchToProps)(App);
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ./react-redux-client/src/containers/Todo.js
2+
import { connect } from 'react-redux';
3+
import * as todoActions from '../actions/todoActions';
4+
import Todo from '../components/Todo';
5+
6+
// map state from store to props
7+
const mapStateToProps = (state) => {
8+
return {
9+
//you can now say this.props.mappedAppSate
10+
mappedTodo: state.todo
11+
}
12+
}
13+
14+
// map actions to props
15+
const mapDispatchToProps = (dispatch) => {
16+
return {
17+
//you can now say this.props.mappedAppActions
18+
19+
}
20+
}
21+
22+
export default connect(mapStateToProps,mapDispatchToProps)(Todo);
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ./react-redux-client/src/containers/Todos.js
2+
import { connect } from 'react-redux';
3+
import * as todoActions from '../actions/todoActions';
4+
import Todos from '../components/Todos';
5+
6+
// map state from store to props
7+
const mapStateToProps = (state) => {
8+
return {
9+
//you can now say this.props.mappedAppSate
10+
mappedTodosState: state.todos
11+
}
12+
}
13+
14+
// map actions to props
15+
const mapDispatchToProps = (dispatch) => {
16+
return {
17+
//you can now say this.props.mappedAppActions
18+
mappedAddTodo: todo => dispatch(todoActions.addTodo(todo)),
19+
mappedDeleteTodo: todoToDelete => dispatch(todoActions.deleteTodo(todoToDelete)),
20+
mappedEditTodo: todoToEdit => dispatch(todoActions.deleteTodo(todoToEdit))
21+
}
22+
}
23+
24+
export default connect(mapStateToProps,mapDispatchToProps)(Todos);

‎react-redux-client/src/index.css

-5
This file was deleted.

‎react-redux-client/src/logo.svg

-7
This file was deleted.

‎react-redux-client/src/routes.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// ./react-redux-client/src/routes.js
2+
import React from 'react';
3+
import { Route, IndexRoute } from 'react-router';
4+
import App from './containers/App';
5+
import Todos from './containers/Todos';
6+
import Todo from './containers/Todo';
7+
8+
export default (
9+
<Route path="/" component={App}>
10+
<IndexRoute component={Todos} />
11+
<Route path="/:id" component={Todo} />
12+
</Route>
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// ./react-redux-client/src/store/configureStore.js
2+
import {createStore, compose, applyMiddleware} from 'redux';
3+
import thunk from 'redux-thunk';
4+
import rootReducer from '../reducers';
5+
6+
export default function configureStore(initialState) {
7+
const middlewares = [
8+
thunk,
9+
];
10+
11+
const store = createStore(rootReducer, initialState, compose(
12+
applyMiddleware(...middlewares),
13+
window.devToolsExtension ? window.devToolsExtension() : f => f // add support for Redux dev tools
14+
)
15+
);
16+
17+
if (module.hot) {
18+
// Enable Webpack hot module replacement for reducers
19+
module.hot.accept('../reducers', () => {
20+
const nextReducer = require('../reducers').default; // eslint-disable-line global-require
21+
store.replaceReducer(nextReducer);
22+
});
23+
}
24+
25+
return store;
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.