Skip to content

Commit 9c39b54

Browse files
committed
Rendering Components for Routes using render and component prop
inside the `<Route />` Component
1 parent a22f668 commit 9c39b54

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,4 @@ Required Dependency: **`npm i --save axios`** | **[`axios` DOCS](https://github.
128128
2. Setting Up The Router Package `npm i --save react-router react-router-dom`: [Commit Details](https://github.com/Ch-sriram/react/commit/64ef82a4d09d2b0561861764c73a717fd754f4e4)
129129
3. Preparing the Project for Routing &mdash; Making `<NewPost />`, `<FullPost />` & `<Posts />` Components as a Container for Proper Routing: [Commit Details](https://github.com/Ch-sriram/react/commit/e8d0a46e2892af940f600481cba861393dca051b)
130130
4. Setting Up and Rendering Routes using `path`, `exact` & `render` attributes inside the `<Route />` Component: [Commit Details](https://github.com/Ch-sriram/react/commit/0ae8809410db1d704c99ab3b61a7a6d8e906b0f2)
131+
5. Rendering Components for Routes using `render` and `component` prop inside the `<Route />` Component: [Commit Details]()

http-ajax-routing/src/containers/Blog/Blog.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ import Posts from '../../containers/Blog/Posts/Posts';
2626
*/
2727

2828
/**
29-
* Instead of hard-coding the components to be rendered onto
30-
* the home "/" route, we can render the components dynamically
31-
* using the <Route /> component, in which we define the `path`
32-
* and `render` props. For a certain `path` we define, we can
33-
* also define the `exact` prop to be true/false depending on
34-
* how we want to render the components, whether we want to
35-
* render the components on the specified path exactly, or we
36-
* might not want to render the components on the specified
37-
* path exactly.
29+
* Instead of rendering the components directly using the
30+
* `render` prop, we will use the `component` prop inside the
31+
* <Route /> Component to give the `component` prop a reference
32+
* to the component we want to render at that exact specified
33+
* route in the `path` prop of the <Route /> Component.
34+
*
35+
* The example is shown below.
3836
*/
3937

4038
class Blog extends Component {
@@ -49,23 +47,26 @@ class Blog extends Component {
4947
</ul>
5048
</nav>
5149
</header>
52-
<Route path="/" exact render={() => <h1>Home</h1>}/>
53-
<Route path="/" render={() => <h1>Home 2</h1>}/>
50+
{/*<Route path="/" exact render={() => <h1>Home</h1>}/>
51+
<Route path="/" render={() => <h1>Home 2</h1>}/>*/}
52+
53+
{/**
54+
Instead of rendering the component using the `render`
55+
prop, we can render using the `component` prop as
56+
shown below:
57+
*/}
58+
<Route path="/" exact component={Posts} />
59+
60+
{/**
61+
If we still want to use the `render` prop to render
62+
the component we need onto the specified route inside
63+
the path we want to render to, we'd use the `render`
64+
prop as shown below:
65+
*/}
66+
<Route path="/" exact render={() => <Posts />} />
5467
</div>
5568
);
5669
}
5770
}
5871

59-
/**
60-
* Line #52's `render` prop will run every time the "/" path
61-
* is searched for in the browser, but Line #53's `render` prop
62-
* will run for every route, iff that route is not setup
63-
* previously. For example, on the route "/new-post", we will
64-
* see the output `<h1>Home 2</h1>`, but not the output,
65-
* `<h1>Home</h1>`, because `<h1>Home</h1>` (being rendered in
66-
* Line #52) will only be rendered when the specified path is
67-
* "/", because we've already mentioned the `exact` prop to be
68-
* true for that <Route /> Component.
69-
*/
70-
7172
export default Blog;

0 commit comments

Comments
 (0)