Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
56
rated 0 times [  63] [ 7]  / answers: 1 / hits: 71254  / 7 Years ago, sun, march 12, 2017, 12:00:00

I'm learning React myself with online tutorial.



So this is a basic example about using React Router:



<Router history={browserHistory}>
<Route path=/ component={App}>
<IndexRoute component={Home}/>
<Route path=/home component={Home}/>
<Route path=/about component={About}/>
<Route path=/contact component={Contact}/>
</Route>
</Router>


With my App component:



class App extends React.Component {
render() {
return (
<div>
<ul>
<li><Link to=home>Home</Link></li>
<li><Link to=about>About</Link></li>
<li><Link to=contact>Contact</Link></li>
</ul>
{this.props.children}
</div>
)
}
}
export default App;


However, I have problem when using IndexRoute because it shows nothing, so I search for the module of react-router-dom v4 on npm and there is no IndexRoute inside.
Instead it uses this:



<Router>
<div>
<ul>
<li><Link to=/>Home</Link></li>
<li><Link to=/about>About</Link></li>
<li><Link to=/contact>Contact</Link></li>
</ul>
<hr/>
<Route exact path=/ component={Home}/>
<Route path=/about component={About}/>
<Route path=/contact component={Contact}/>
</div>
</Router>


So how can I render 2 component for 1 path ?


More From » node.js

 Answers
3

UPDATE
react-router-4 has changed in that it no longer has children. However, with the Route component you can render anything that matches the path.


<Router>
<div>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
<hr/>

// All 3 components below would be rendered when in a homepage
<Route exact path="/" component={Home}/>
<Route exact path="/" component={About}/>
<Route exact path="/" component={Contact}/>

<Route path="/about" component={About}/>
<Route path="/contact" component={Contact}/>
</div>
</Router>

This means that if you want a wrapper, you can write it inside the component.


[#58584] Thursday, March 9, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
juliettec

Total Points: 327
Total Questions: 127
Total Answers: 102

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
;