22import React , { Component } from "react" ;
33import { Route , NavLink } from 'react-router-dom' ;
44
5- // STYLING
5+ // STYLING IMPORTS
66import "./Blog.css" ;
77
88// CUSTOM COMPONENTS
99import Posts from '../../containers/Blog/Posts/Posts' ;
1010import NewPost from './NewPost/NewPost' ;
11+ import FullPost from './FullPost/FullPost' ;
1112
1213class Blog extends Component {
1314 componentDidMount ( ) {
@@ -18,71 +19,15 @@ class Blog extends Component {
1819 return (
1920 < div className = "Blog" >
2021 < header >
21- {
22- /**
23- * Using <NavLink /> Component, we can use some extra
24- * props which allow us to define some styling for the
25- * active link, i.e., the link that's actually clicked.
26- *
27- * Now, when we can style the links in Blog.css file,
28- * for their respective class/id/element along with the
29- * .active class, to change the styling when the link
30- * is actually active, i.e., the contents of the route
31- * related to that link are being displayed on the
32- * page/view.
33- *
34- * Now, when we click on the "New Post" link, even the
35- * "Home" link is styled as if it is having the .active
36- * class, but it is actually having the .active class,
37- * and that's because the <NavLink /> applies the
38- * .active class to every link that has the route
39- * prefixed with the path "/". We need to get rid of
40- * this behaviour by giving the `exact` prop to the
41- * <NavLink /> component related to "Home" Link as
42- * shown below.
43- */
44- }
4522 < nav >
4623 < ul >
47- < li > < NavLink
48- to = "/"
49- exact
50- /**
51- * Sometimes, we might not want the default
52- * class, which is .active, to be the class name,
53- * when we click the link. We might want to have
54- * our own class name applied to the active link,
55- * and that can be done as shown below:
56- */
24+ < li > < NavLink to = "/" exact
5725 activeClassName = "my-active"
58- /**
59- * Because of the prop above, we have to add
60- * styling for the "Home" route separately to
61- * style it when it is active i.e., when the "/"
62- * route (i.e., "Home") is active, the .my-active
63- * class is the one attached to the respective
64- * <NavLink /> Component for the "Home" link, and
65- * so, we style using the .my-active class inside
66- * the Blog.css file.
67- */
68-
69- /**
70- * We can also directly style the <NavLink />
71- * directly in here, using inline styling, using
72- * the activeStyle prop as shown below:
73- */
7426 activeStyle = { {
7527 color : '#FA923F' ,
7628 textDecoration : 'underline'
7729 } }
7830 > Home</ NavLink > </ li >
79- {
80- /**
81- * Here, we don't need the `exact` prop as, there
82- * is no other link that prefixes '/new-post'
83- * route.
84- */
85- }
8631 < li > < NavLink to = { {
8732 pathname : '/new-post' ,
8833 hash : '#submit' ,
@@ -91,8 +36,42 @@ class Blog extends Component {
9136 </ ul >
9237 </ nav >
9338 </ header >
39+ {
40+ /**
41+ * For every <Post /> Component we click on, we should
42+ * be able to render the entire <FullPost /> Component
43+ * related to the clicked <Post />.
44+ *
45+ * Which is why, we set a specific Route for the
46+ * <FullPost /> to be loaded whenever the path matches
47+ * some kind of an `id`, which in this case is `post.id`.
48+ * handled inside the <Posts /> Component.
49+ *
50+ * Therefore, we add the route for "/:id", which is
51+ * telling to the react-router that this route is a
52+ * dynamic route. Anything that's prefixed with "/" and
53+ * has some string after it, will render the component
54+ * mentioned respectively.
55+ *
56+ * But that would also make the "/new-post" route render
57+ * the component we mention inside the route for "/:id"
58+ * because now, "/new-post" is also a route which
59+ * matches the rule of the route mentioned as "/:id".
60+ *
61+ * To avoid this confusion, we can render the <Route />
62+ * for the "/new-post" path before the <Route /> for the
63+ * "/:id" path, as shown below.
64+ */
65+ }
9466 < Route path = "/" exact component = { Posts } />
9567 < Route path = "/new-post" component = { NewPost } />
68+ < Route path = "/:id" exact component = { FullPost } />
69+
70+ { // The following ordering of Routes won't work properly
71+ // <Route path="/" exact component={Posts} />
72+ // <Route path="/:id" exact component={FullPost} />
73+ // <Route path="/new-post" component={NewPost} />
74+ }
9675 </ div >
9776 ) ;
9877 }
0 commit comments