Skip to content

Commit 34bca1f

Browse files
committed
Routing with redux
1 parent f582171 commit 34bca1f

File tree

28 files changed

+689
-50
lines changed

28 files changed

+689
-50
lines changed

package-lock.json

Lines changed: 326 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"react": "^16.6.3",
7-
"react-dom": "^16.6.3",
8-
"react-scripts": "2.1.1"
6+
"react": "^16.8.6",
7+
"react-dom": "^16.8.6",
8+
"react-redux": "^7.0.3",
9+
"react-router-dom": "^5.0.0",
10+
"react-scripts": "2.1.1",
11+
"redux": "^4.0.1",
12+
"redux-thunk": "^2.3.0"
913
},
1014
"scripts": {
1115
"start": "react-scripts start",

src/App.test.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/actions/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* ACTIONS
3+
*/
4+
export const LOGIN = 'LOGIN';
5+
export const LOGOUT = 'LOGOUT';
6+
export const REDIRECT = 'REDIRECT';
7+
8+
/**
9+
* Action Creators
10+
*/
11+
export function login(username) {
12+
return {
13+
type: LOGIN,
14+
payload: username,
15+
};
16+
}
17+
18+
export function logout() {
19+
return {
20+
type: LOGOUT,
21+
};
22+
}
23+
24+
export function redirect(url) {
25+
return {
26+
type: REDIRECT,
27+
payload: url,
28+
};
29+
}

src/components/About/About.jsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React, { Component } from 'react';
2+
3+
class About extends Component {
4+
render() {
5+
return <div>About Page</div>;
6+
}
7+
}
8+
9+
export default About;

src/components/About/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import About from './About.jsx';
2+
export default About;

src/components/Contact/Contact.jsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React, { Component } from 'react';
2+
3+
class Contact extends Component {
4+
constructor(props) {
5+
super(props);
6+
7+
this.clickHandler = this.clickHandler.bind(this);
8+
}
9+
10+
clickHandler() {
11+
const id = this.props.match.params.id;
12+
const newId = Number(id) + 1;
13+
this.props.history.push('/contact/' + newId);
14+
}
15+
16+
render() {
17+
const { params } = this.props.match;
18+
return (
19+
<div>
20+
Contact Page {params.id}
21+
<button onClick={this.clickHandler}>Increment</button>
22+
</div>
23+
);
24+
}
25+
}
26+
27+
export default Contact;

src/components/Contact/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Contact from './Contact.jsx';
2+
export default Contact;

src/components/Header/Header.css

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
.header ul {
2+
margin: 0;
3+
padding: 15px;
4+
background-color: #094fbf;
5+
}
6+
7+
.header ul li {
8+
margin-right: 25px;
9+
list-style-type: none;
10+
display: inline-block;
11+
}
12+
13+
.header ul li a {
14+
text-decoration: none;
15+
color: white;
16+
}

src/components/Header/Header.jsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import { Link } from 'react-router-dom';
3+
import { logout } from '../../actions';
4+
import { connect } from 'react-redux';
5+
import './Header.css';
6+
7+
const _Header = (props) => {
8+
return (
9+
<div className="header">
10+
<ul>
11+
<li>
12+
<Link to="/">Home</Link>
13+
</li>
14+
<li>
15+
<Link to="/about">Users</Link>
16+
</li>
17+
<li>
18+
<Link to="/contact/5">Contact</Link>
19+
</li>
20+
<li>
21+
<Link to="/protected">Protected</Link>
22+
</li>
23+
{props.loggedIn ? (
24+
<li>
25+
<button onClick={props.logout}>Logout</button>
26+
</li>
27+
) : (
28+
''
29+
)}
30+
</ul>
31+
</div>
32+
);
33+
};
34+
35+
const mapStateToProps = (state) => {
36+
return {
37+
loggedIn: state.loggedIn,
38+
};
39+
};
40+
41+
const mapDispatchToProps = (dispatch) => {
42+
return {
43+
logout: () => {
44+
return dispatch(logout());
45+
},
46+
};
47+
};
48+
49+
const Header = connect(
50+
mapStateToProps,
51+
mapDispatchToProps,
52+
)(_Header);
53+
54+
export default Header;

0 commit comments

Comments
 (0)