Skip to content

Commit a066733

Browse files
21 & 22 - React Router Dom
1 parent 56b0f61 commit a066733

File tree

7 files changed

+122
-9
lines changed

7 files changed

+122
-9
lines changed

src/reactify-ui/package-lock.json

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

src/reactify-ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"react": "^16.4.0",
88
"react-cookies": "^0.1.0",
99
"react-dom": "^16.4.0",
10+
"react-router-dom": "^4.2.2",
1011
"whatwg-fetch": "^2.0.4"
1112
},
1213
"devDependencies": {

src/reactify-ui/src/App.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import React, { Component } from 'react';
2+
import { BrowserRouter, Route, Redirect, Switch} from 'react-router-dom'
23
import logo from './logo.svg';
34
import './App.css';
45

56
import Posts from './posts/Posts';
7+
import PostDetail from './posts/PostDetail';
68

79
class App extends Component {
810
render() {
911
return (
10-
<Posts />
12+
<BrowserRouter>
13+
<Switch>
14+
<Route exact path='/posts' component={Posts}/>
15+
<Route exact path='/posts/:slug' component={PostDetail}/>
16+
<Route component={Posts}/>
17+
</Switch>
18+
</BrowserRouter>
1119
);
1220
}
1321
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, {Component} from 'react'
2+
import { Link } from 'react-router-dom'
3+
4+
class PostDetail extends Component {
5+
constructor(props){
6+
super(props)
7+
this.state = {
8+
slug: null
9+
}
10+
}
11+
componentDidMount(){
12+
this.setState({
13+
slug: null
14+
})
15+
if (this.props.match){
16+
const {slug} = this.props.match.params
17+
this.setState({
18+
slug: slug
19+
})
20+
}
21+
}
22+
render(){
23+
const {slug} = this.state
24+
return(
25+
<p>{(slug !== null) ? <div>
26+
27+
{slug}
28+
29+
<p className='lead'><Link maintainScrollPosition={false} to={{
30+
pathname: `/posts`,
31+
state: { fromDashboard: false }
32+
}}>Posts</Link></p>
33+
34+
</div> : "Not found"}</p>
35+
)
36+
}
37+
}
38+
39+
export default PostDetail

src/reactify-ui/src/posts/PostInline.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react';
2+
import { Link } from 'react-router-dom'
23

34
class PostInline extends Component {
45
render() {
@@ -8,7 +9,11 @@ class PostInline extends Component {
89
return (
910
<div>
1011
{post !== undefined ? <div className={elClass}>
11-
<h1>{post.title}</h1>
12+
<h1><Link maintainScrollPosition={false} to={{
13+
pathname:`/posts/${post.slug}`,
14+
state: {fromDashboard: false}
15+
}}>{post.title}</Link></h1>
16+
1217
<p className={showContent}>{post.content}</p>
1318
</div>
1419
: ""}

src/reactify/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
"""
1616
from django.contrib import admin
1717
from django.views.generic import TemplateView
18-
from django.urls import path, include
18+
from django.urls import path, include, re_path
1919

2020

2121
urlpatterns = [
2222
path('', TemplateView.as_view(template_name='react.html')),
23+
re_path(r'^posts/', TemplateView.as_view(template_name='react.html')),
2324
path('admin/', admin.site.urls),
2425
path('api/posts/', include('posts.urls'))
2526
]

src/staticfiles/js/reactify-django.ui.js

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

0 commit comments

Comments
 (0)