React Router 4, sub-routing for specific component The Next CEO of Stack OverflowReact-router: How to manually invoke Link?Programmatically navigate using react routerreact-router v4 doesn't trigger routesReact Router v4 - Dynamic Config with the changed default routeReactJs Router Duplicates itself when wrap it in componentReact Router: Route defined in child component not workingWith React-Router, can you set an “outer” component to render at every route?React Router 4 Nested Routes not in PropsNested routes in React Router v4 not rendering as expectedReact Router 4 and exact path with dynamic param
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?
Why didn't Khan get resurrected in the Genesis Explosion?
Grabbing quick drinks
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
Why the difference in type-inference over the as-pattern in two similar function definitions?
RigExpert AA-35 - Interpreting The Information
Do I need to write [sic] when a number is less than 10 but isn't written out?
Is it convenient to ask the journal's editor for two additional days to complete a review?
How do I align (1) and (2)?
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Axiom Schema vs Axiom
How to check if all elements of 1 list are in the *same quantity* and in any order, in the list2?
Why do remote US companies require working in the US?
Find non-case sensitive string in a mixed list of elements?
What was the first Unix version to run on a microcomputer?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
Make solar eclipses exceedingly rare, but still have new moons
Method for adding error messages to a dictionary given a key
Why is information "lost" when it got into a black hole?
Would a completely good Muggle be able to use a wand?
What flight has the highest ratio of timezone difference to flight time?
How to write a definition with variants?
Does increasing your ability score affect your main stat?
How did people program for Consoles with multiple CPUs?
React Router 4, sub-routing for specific component
The Next CEO of Stack OverflowReact-router: How to manually invoke Link?Programmatically navigate using react routerreact-router v4 doesn't trigger routesReact Router v4 - Dynamic Config with the changed default routeReactJs Router Duplicates itself when wrap it in componentReact Router: Route defined in child component not workingWith React-Router, can you set an “outer” component to render at every route?React Router 4 Nested Routes not in PropsNested routes in React Router v4 not rendering as expectedReact Router 4 and exact path with dynamic param
I'm having issues with figuring out React Router 4 routing for specific component. I have following project structure:
[dir] node_modules
[dir] public
[dir] src
[dir] components
[dir] Profile
- Navigation.jsx
- Card.jsx
- Overview.jsx
- About.jsx
- Home.jsx
- Login.jsx
- Profile.jsx
- App.jsx
- index.js
Problem is, I want my Profile.jsx (located in src/components/Profile.jsx
) to be my main point of entry and then I'd have another navigation that'd display two different sub-components named Overview
and About
. However what I want is for Profile.jsx
to default Overview
component when my visitor enters or navigates http://example.com/coolguy82
and then if the visitor clicks on About, it'd take him to http://example.com/coolguy82/about
(but now without rendering Overview
component).
So basically what I want is to get the same UI configuration when I visit http://example.com/coolguy82
and http://example.com/coolguy82/overview
, but different when I go to http://example.com/coolguy82/about
. I did make that happen, however when I navigate to http://example.com/coolguy82
(through browser address bar), my Profile.jsx
only renders my Card
and Navigation
component (which is what I want) but won't render Overview
component. Only when I click on navigation to go to Overview it renders that part of the page.
My code is as follows (some unneeded code omitted):
App.js
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Home from "./components/Home.jsx";
import Login from "./components/Login.jsx";
import Profile from "./components/Profile.jsx";
class App extends Component
render()
return (
<React.Fragment>
<Router>
<div>
<header>
<nav className="main">
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
</ul>
</nav>
</header>
<Switch>
<Route path="/" exact component=Home />
<Route exact path="/login" component=Login />
<Route
path="/:username"
render=props => <Profile ...props />
/>
</Switch>
</div>
</Router>
</React.Fragment>
);
export default App;
Navigation.jsx
import React, Component from "react";
import Link from "react-router-dom";
class Navigation extends Component
constructor(props)
super(props);
this.state =
user: this.props.user,
tab: this.props.active
;
render()
let user, tab = this.state;
return (
<React.Fragment>
<ul className="nav nav-tabs">
<li className="nav-item">
<Link
to=`/$user/overview`
className="nav-link " + (tab === "overview" ? "active" : "")
onClick=() => this.setState( tab: "overview" )
>
Overview
</Link>
</li>
<li className="nav-item">
<Link
to=`/$user/about`
className="nav-link " + (tab === "about" ? "active" : "")
onClick=() => this.setState( tab: "about" )
>
About
</Link>
</li>
</ul>
</React.Fragment>
);
export default Navigation;
Profile.jsx
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Overview from "./Profile/Overview";
import About from "./Profile/About";
import Card from "./Profile/Card";
import Navigation from "./Profile/Navigation";
class Profile extends Component
constructor(props)
super(props);
this.state =
user: this.props.match.params.username
;
render()
let user = this.state.user;
return (
<React.Fragment>
<Card user=this.state.user />
<Navigation user=this.state.user active="overview" />
<Route
path="/:username/overview"
exact=true
render=props => <Overview ...props />
/>
<Route
path="/:username/about"
exact=true
render=props => <About ...props />
/>
</React.Fragment>
);
export default Profile;
What am I doing wrong and how can I correct it? I'm assuming I have misconfigured code for react router.
reactjs react-router react-router-v4 react-router-dom
add a comment |
I'm having issues with figuring out React Router 4 routing for specific component. I have following project structure:
[dir] node_modules
[dir] public
[dir] src
[dir] components
[dir] Profile
- Navigation.jsx
- Card.jsx
- Overview.jsx
- About.jsx
- Home.jsx
- Login.jsx
- Profile.jsx
- App.jsx
- index.js
Problem is, I want my Profile.jsx (located in src/components/Profile.jsx
) to be my main point of entry and then I'd have another navigation that'd display two different sub-components named Overview
and About
. However what I want is for Profile.jsx
to default Overview
component when my visitor enters or navigates http://example.com/coolguy82
and then if the visitor clicks on About, it'd take him to http://example.com/coolguy82/about
(but now without rendering Overview
component).
So basically what I want is to get the same UI configuration when I visit http://example.com/coolguy82
and http://example.com/coolguy82/overview
, but different when I go to http://example.com/coolguy82/about
. I did make that happen, however when I navigate to http://example.com/coolguy82
(through browser address bar), my Profile.jsx
only renders my Card
and Navigation
component (which is what I want) but won't render Overview
component. Only when I click on navigation to go to Overview it renders that part of the page.
My code is as follows (some unneeded code omitted):
App.js
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Home from "./components/Home.jsx";
import Login from "./components/Login.jsx";
import Profile from "./components/Profile.jsx";
class App extends Component
render()
return (
<React.Fragment>
<Router>
<div>
<header>
<nav className="main">
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
</ul>
</nav>
</header>
<Switch>
<Route path="/" exact component=Home />
<Route exact path="/login" component=Login />
<Route
path="/:username"
render=props => <Profile ...props />
/>
</Switch>
</div>
</Router>
</React.Fragment>
);
export default App;
Navigation.jsx
import React, Component from "react";
import Link from "react-router-dom";
class Navigation extends Component
constructor(props)
super(props);
this.state =
user: this.props.user,
tab: this.props.active
;
render()
let user, tab = this.state;
return (
<React.Fragment>
<ul className="nav nav-tabs">
<li className="nav-item">
<Link
to=`/$user/overview`
className="nav-link " + (tab === "overview" ? "active" : "")
onClick=() => this.setState( tab: "overview" )
>
Overview
</Link>
</li>
<li className="nav-item">
<Link
to=`/$user/about`
className="nav-link " + (tab === "about" ? "active" : "")
onClick=() => this.setState( tab: "about" )
>
About
</Link>
</li>
</ul>
</React.Fragment>
);
export default Navigation;
Profile.jsx
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Overview from "./Profile/Overview";
import About from "./Profile/About";
import Card from "./Profile/Card";
import Navigation from "./Profile/Navigation";
class Profile extends Component
constructor(props)
super(props);
this.state =
user: this.props.match.params.username
;
render()
let user = this.state.user;
return (
<React.Fragment>
<Card user=this.state.user />
<Navigation user=this.state.user active="overview" />
<Route
path="/:username/overview"
exact=true
render=props => <Overview ...props />
/>
<Route
path="/:username/about"
exact=true
render=props => <About ...props />
/>
</React.Fragment>
);
export default Profile;
What am I doing wrong and how can I correct it? I'm assuming I have misconfigured code for react router.
reactjs react-router react-router-v4 react-router-dom
add a comment |
I'm having issues with figuring out React Router 4 routing for specific component. I have following project structure:
[dir] node_modules
[dir] public
[dir] src
[dir] components
[dir] Profile
- Navigation.jsx
- Card.jsx
- Overview.jsx
- About.jsx
- Home.jsx
- Login.jsx
- Profile.jsx
- App.jsx
- index.js
Problem is, I want my Profile.jsx (located in src/components/Profile.jsx
) to be my main point of entry and then I'd have another navigation that'd display two different sub-components named Overview
and About
. However what I want is for Profile.jsx
to default Overview
component when my visitor enters or navigates http://example.com/coolguy82
and then if the visitor clicks on About, it'd take him to http://example.com/coolguy82/about
(but now without rendering Overview
component).
So basically what I want is to get the same UI configuration when I visit http://example.com/coolguy82
and http://example.com/coolguy82/overview
, but different when I go to http://example.com/coolguy82/about
. I did make that happen, however when I navigate to http://example.com/coolguy82
(through browser address bar), my Profile.jsx
only renders my Card
and Navigation
component (which is what I want) but won't render Overview
component. Only when I click on navigation to go to Overview it renders that part of the page.
My code is as follows (some unneeded code omitted):
App.js
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Home from "./components/Home.jsx";
import Login from "./components/Login.jsx";
import Profile from "./components/Profile.jsx";
class App extends Component
render()
return (
<React.Fragment>
<Router>
<div>
<header>
<nav className="main">
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
</ul>
</nav>
</header>
<Switch>
<Route path="/" exact component=Home />
<Route exact path="/login" component=Login />
<Route
path="/:username"
render=props => <Profile ...props />
/>
</Switch>
</div>
</Router>
</React.Fragment>
);
export default App;
Navigation.jsx
import React, Component from "react";
import Link from "react-router-dom";
class Navigation extends Component
constructor(props)
super(props);
this.state =
user: this.props.user,
tab: this.props.active
;
render()
let user, tab = this.state;
return (
<React.Fragment>
<ul className="nav nav-tabs">
<li className="nav-item">
<Link
to=`/$user/overview`
className="nav-link " + (tab === "overview" ? "active" : "")
onClick=() => this.setState( tab: "overview" )
>
Overview
</Link>
</li>
<li className="nav-item">
<Link
to=`/$user/about`
className="nav-link " + (tab === "about" ? "active" : "")
onClick=() => this.setState( tab: "about" )
>
About
</Link>
</li>
</ul>
</React.Fragment>
);
export default Navigation;
Profile.jsx
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Overview from "./Profile/Overview";
import About from "./Profile/About";
import Card from "./Profile/Card";
import Navigation from "./Profile/Navigation";
class Profile extends Component
constructor(props)
super(props);
this.state =
user: this.props.match.params.username
;
render()
let user = this.state.user;
return (
<React.Fragment>
<Card user=this.state.user />
<Navigation user=this.state.user active="overview" />
<Route
path="/:username/overview"
exact=true
render=props => <Overview ...props />
/>
<Route
path="/:username/about"
exact=true
render=props => <About ...props />
/>
</React.Fragment>
);
export default Profile;
What am I doing wrong and how can I correct it? I'm assuming I have misconfigured code for react router.
reactjs react-router react-router-v4 react-router-dom
I'm having issues with figuring out React Router 4 routing for specific component. I have following project structure:
[dir] node_modules
[dir] public
[dir] src
[dir] components
[dir] Profile
- Navigation.jsx
- Card.jsx
- Overview.jsx
- About.jsx
- Home.jsx
- Login.jsx
- Profile.jsx
- App.jsx
- index.js
Problem is, I want my Profile.jsx (located in src/components/Profile.jsx
) to be my main point of entry and then I'd have another navigation that'd display two different sub-components named Overview
and About
. However what I want is for Profile.jsx
to default Overview
component when my visitor enters or navigates http://example.com/coolguy82
and then if the visitor clicks on About, it'd take him to http://example.com/coolguy82/about
(but now without rendering Overview
component).
So basically what I want is to get the same UI configuration when I visit http://example.com/coolguy82
and http://example.com/coolguy82/overview
, but different when I go to http://example.com/coolguy82/about
. I did make that happen, however when I navigate to http://example.com/coolguy82
(through browser address bar), my Profile.jsx
only renders my Card
and Navigation
component (which is what I want) but won't render Overview
component. Only when I click on navigation to go to Overview it renders that part of the page.
My code is as follows (some unneeded code omitted):
App.js
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Home from "./components/Home.jsx";
import Login from "./components/Login.jsx";
import Profile from "./components/Profile.jsx";
class App extends Component
render()
return (
<React.Fragment>
<Router>
<div>
<header>
<nav className="main">
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/login">Login</Link>
</li>
</ul>
</nav>
</header>
<Switch>
<Route path="/" exact component=Home />
<Route exact path="/login" component=Login />
<Route
path="/:username"
render=props => <Profile ...props />
/>
</Switch>
</div>
</Router>
</React.Fragment>
);
export default App;
Navigation.jsx
import React, Component from "react";
import Link from "react-router-dom";
class Navigation extends Component
constructor(props)
super(props);
this.state =
user: this.props.user,
tab: this.props.active
;
render()
let user, tab = this.state;
return (
<React.Fragment>
<ul className="nav nav-tabs">
<li className="nav-item">
<Link
to=`/$user/overview`
className="nav-link " + (tab === "overview" ? "active" : "")
onClick=() => this.setState( tab: "overview" )
>
Overview
</Link>
</li>
<li className="nav-item">
<Link
to=`/$user/about`
className="nav-link " + (tab === "about" ? "active" : "")
onClick=() => this.setState( tab: "about" )
>
About
</Link>
</li>
</ul>
</React.Fragment>
);
export default Navigation;
Profile.jsx
import React, Component from "react";
import
BrowserRouter as Router,
Switch,
Route,
Link,
Redirect
from "react-router-dom";
import Overview from "./Profile/Overview";
import About from "./Profile/About";
import Card from "./Profile/Card";
import Navigation from "./Profile/Navigation";
class Profile extends Component
constructor(props)
super(props);
this.state =
user: this.props.match.params.username
;
render()
let user = this.state.user;
return (
<React.Fragment>
<Card user=this.state.user />
<Navigation user=this.state.user active="overview" />
<Route
path="/:username/overview"
exact=true
render=props => <Overview ...props />
/>
<Route
path="/:username/about"
exact=true
render=props => <About ...props />
/>
</React.Fragment>
);
export default Profile;
What am I doing wrong and how can I correct it? I'm assuming I have misconfigured code for react router.
reactjs react-router react-router-v4 react-router-dom
reactjs react-router react-router-v4 react-router-dom
edited Mar 7 at 17:27
MerkisL
asked Mar 7 at 16:59
MerkisLMerkisL
509
509
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049178%2freact-router-4-sub-routing-for-specific-component%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55049178%2freact-router-4-sub-routing-for-specific-component%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown