execute route change from siblings component in React Router V4
I am learning React.js. I am using React router version 4.3.1 and React router dom 4.3.1.
My current app structure is given below.
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
in my content component i have a Router set up as given below.
<BrowserRouter>
<React.Fragment>
<Route exact path="/phoenix/public" component=Login />
<ProtectedRoute exact path="/phoenix/public/sample" component=SamplePage />
<ProtectedRoute exact path="/phoenix/public/view_ticket" component=ViewTicket />
<ProtectedRoute exact path="/phoenix/public/create_ticket" component=CreateTicket />
<ProtectedRoute exact path="/phoenix/public/dashboard" component=Dashborad />
</React.Fragment>
</BrowserRouter>
Here ProtectedRoute is a functional component which checks if the user is authenticated and returns a Route
My sidebar has some a tags like below.
<li>
<a href="dashboard">
<i className="ti-panel" />
<p>Dashboard</p>
</a>
</li>
My goal is to navigate to different Routes without page refresh on click from side bar. But clicking href reloads the page and all my state in my Mobx refreshes. I have tried to Redirect but could not get the Router which is stated in siblings component (content is siblings of sidebar). I have no idea how to achieve this.
reactjs react-router-v4 react-router-dom
add a comment |
I am learning React.js. I am using React router version 4.3.1 and React router dom 4.3.1.
My current app structure is given below.
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
in my content component i have a Router set up as given below.
<BrowserRouter>
<React.Fragment>
<Route exact path="/phoenix/public" component=Login />
<ProtectedRoute exact path="/phoenix/public/sample" component=SamplePage />
<ProtectedRoute exact path="/phoenix/public/view_ticket" component=ViewTicket />
<ProtectedRoute exact path="/phoenix/public/create_ticket" component=CreateTicket />
<ProtectedRoute exact path="/phoenix/public/dashboard" component=Dashborad />
</React.Fragment>
</BrowserRouter>
Here ProtectedRoute is a functional component which checks if the user is authenticated and returns a Route
My sidebar has some a tags like below.
<li>
<a href="dashboard">
<i className="ti-panel" />
<p>Dashboard</p>
</a>
</li>
My goal is to navigate to different Routes without page refresh on click from side bar. But clicking href reloads the page and all my state in my Mobx refreshes. I have tried to Redirect but could not get the Router which is stated in siblings component (content is siblings of sidebar). I have no idea how to achieve this.
reactjs react-router-v4 react-router-dom
Have you tried using Link?
– c-chavez
Nov 15 '18 at 7:14
Have you tried navigationOption props?
– Rajneesh Shukla
Nov 15 '18 at 7:30
I have tried to use links but i guess it needs a Router but my sidebar component does not have a Router. How can i use link in sidebar component if my Router is set up in content ?
– ahnafscm
Nov 15 '18 at 8:11
add a comment |
I am learning React.js. I am using React router version 4.3.1 and React router dom 4.3.1.
My current app structure is given below.
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
in my content component i have a Router set up as given below.
<BrowserRouter>
<React.Fragment>
<Route exact path="/phoenix/public" component=Login />
<ProtectedRoute exact path="/phoenix/public/sample" component=SamplePage />
<ProtectedRoute exact path="/phoenix/public/view_ticket" component=ViewTicket />
<ProtectedRoute exact path="/phoenix/public/create_ticket" component=CreateTicket />
<ProtectedRoute exact path="/phoenix/public/dashboard" component=Dashborad />
</React.Fragment>
</BrowserRouter>
Here ProtectedRoute is a functional component which checks if the user is authenticated and returns a Route
My sidebar has some a tags like below.
<li>
<a href="dashboard">
<i className="ti-panel" />
<p>Dashboard</p>
</a>
</li>
My goal is to navigate to different Routes without page refresh on click from side bar. But clicking href reloads the page and all my state in my Mobx refreshes. I have tried to Redirect but could not get the Router which is stated in siblings component (content is siblings of sidebar). I have no idea how to achieve this.
reactjs react-router-v4 react-router-dom
I am learning React.js. I am using React router version 4.3.1 and React router dom 4.3.1.
My current app structure is given below.
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
in my content component i have a Router set up as given below.
<BrowserRouter>
<React.Fragment>
<Route exact path="/phoenix/public" component=Login />
<ProtectedRoute exact path="/phoenix/public/sample" component=SamplePage />
<ProtectedRoute exact path="/phoenix/public/view_ticket" component=ViewTicket />
<ProtectedRoute exact path="/phoenix/public/create_ticket" component=CreateTicket />
<ProtectedRoute exact path="/phoenix/public/dashboard" component=Dashborad />
</React.Fragment>
</BrowserRouter>
Here ProtectedRoute is a functional component which checks if the user is authenticated and returns a Route
My sidebar has some a tags like below.
<li>
<a href="dashboard">
<i className="ti-panel" />
<p>Dashboard</p>
</a>
</li>
My goal is to navigate to different Routes without page refresh on click from side bar. But clicking href reloads the page and all my state in my Mobx refreshes. I have tried to Redirect but could not get the Router which is stated in siblings component (content is siblings of sidebar). I have no idea how to achieve this.
reactjs react-router-v4 react-router-dom
reactjs react-router-v4 react-router-dom
asked Nov 15 '18 at 7:11
ahnafscmahnafscm
174
174
Have you tried using Link?
– c-chavez
Nov 15 '18 at 7:14
Have you tried navigationOption props?
– Rajneesh Shukla
Nov 15 '18 at 7:30
I have tried to use links but i guess it needs a Router but my sidebar component does not have a Router. How can i use link in sidebar component if my Router is set up in content ?
– ahnafscm
Nov 15 '18 at 8:11
add a comment |
Have you tried using Link?
– c-chavez
Nov 15 '18 at 7:14
Have you tried navigationOption props?
– Rajneesh Shukla
Nov 15 '18 at 7:30
I have tried to use links but i guess it needs a Router but my sidebar component does not have a Router. How can i use link in sidebar component if my Router is set up in content ?
– ahnafscm
Nov 15 '18 at 8:11
Have you tried using Link?
– c-chavez
Nov 15 '18 at 7:14
Have you tried using Link?
– c-chavez
Nov 15 '18 at 7:14
Have you tried navigationOption props?
– Rajneesh Shukla
Nov 15 '18 at 7:30
Have you tried navigationOption props?
– Rajneesh Shukla
Nov 15 '18 at 7:30
I have tried to use links but i guess it needs a Router but my sidebar component does not have a Router. How can i use link in sidebar component if my Router is set up in content ?
– ahnafscm
Nov 15 '18 at 8:11
I have tried to use links but i guess it needs a Router but my sidebar component does not have a Router. How can i use link in sidebar component if my Router is set up in content ?
– ahnafscm
Nov 15 '18 at 8:11
add a comment |
2 Answers
2
active
oldest
votes
As stated in the comments on your question, regular <a> tags will not work, and refresh the page. So you will need to use Link or NavLink.
If you want to make it work in your sidebar (child component) you will need to wrap the <BrowserRouter> around all the components where you want to use the routes.
For example like this (using your code):
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
And your Sidebar will be something like this:
<React.Fragment>
<Link to="/dashboard">Dashboard</Link>
...
</React.Fragment>
Then in your content component. You need to delete the <BrowserRouter>
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
add a comment |
I fixed my problem by having BrowserRouter like below as suggested by @PreDinnerSnack.
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
Then i used Navlink in My Sidebar component. Which fixed the problem of navigating to the desired route without page refresh. But then i faced another problem as Content was not Refreshing according to the route.
For that problem I found the observer HOC of Mobx was interfering with my router. I had to wrap my content with withRouter as below to fix this problem.
withRouter(observer(Content));
I hope my solution will come in handy for someone as i found no tutorial with the examples of React Router implementation like mine on the web.
add a comment |
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%2f53314144%2fexecute-route-change-from-siblings-component-in-react-router-v4%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As stated in the comments on your question, regular <a> tags will not work, and refresh the page. So you will need to use Link or NavLink.
If you want to make it work in your sidebar (child component) you will need to wrap the <BrowserRouter> around all the components where you want to use the routes.
For example like this (using your code):
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
And your Sidebar will be something like this:
<React.Fragment>
<Link to="/dashboard">Dashboard</Link>
...
</React.Fragment>
Then in your content component. You need to delete the <BrowserRouter>
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
add a comment |
As stated in the comments on your question, regular <a> tags will not work, and refresh the page. So you will need to use Link or NavLink.
If you want to make it work in your sidebar (child component) you will need to wrap the <BrowserRouter> around all the components where you want to use the routes.
For example like this (using your code):
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
And your Sidebar will be something like this:
<React.Fragment>
<Link to="/dashboard">Dashboard</Link>
...
</React.Fragment>
Then in your content component. You need to delete the <BrowserRouter>
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
add a comment |
As stated in the comments on your question, regular <a> tags will not work, and refresh the page. So you will need to use Link or NavLink.
If you want to make it work in your sidebar (child component) you will need to wrap the <BrowserRouter> around all the components where you want to use the routes.
For example like this (using your code):
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
And your Sidebar will be something like this:
<React.Fragment>
<Link to="/dashboard">Dashboard</Link>
...
</React.Fragment>
Then in your content component. You need to delete the <BrowserRouter>
As stated in the comments on your question, regular <a> tags will not work, and refresh the page. So you will need to use Link or NavLink.
If you want to make it work in your sidebar (child component) you will need to wrap the <BrowserRouter> around all the components where you want to use the routes.
For example like this (using your code):
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
And your Sidebar will be something like this:
<React.Fragment>
<Link to="/dashboard">Dashboard</Link>
...
</React.Fragment>
Then in your content component. You need to delete the <BrowserRouter>
answered Nov 15 '18 at 14:31
PreDinnerSnackPreDinnerSnack
564
564
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
add a comment |
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
i am successful in navigating to the desired route with your solution but the content does not change to according route
– ahnafscm
Nov 15 '18 at 15:57
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
Do you have a switch around your routes? reacttraining.com/react-router/core/api/Switch
– PreDinnerSnack
Nov 15 '18 at 16:12
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
no i dont have switch around my routes ...
– ahnafscm
Nov 15 '18 at 17:41
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
i fixed my problem. thanks for your help.
– ahnafscm
Nov 15 '18 at 18:28
add a comment |
I fixed my problem by having BrowserRouter like below as suggested by @PreDinnerSnack.
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
Then i used Navlink in My Sidebar component. Which fixed the problem of navigating to the desired route without page refresh. But then i faced another problem as Content was not Refreshing according to the route.
For that problem I found the observer HOC of Mobx was interfering with my router. I had to wrap my content with withRouter as below to fix this problem.
withRouter(observer(Content));
I hope my solution will come in handy for someone as i found no tutorial with the examples of React Router implementation like mine on the web.
add a comment |
I fixed my problem by having BrowserRouter like below as suggested by @PreDinnerSnack.
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
Then i used Navlink in My Sidebar component. Which fixed the problem of navigating to the desired route without page refresh. But then i faced another problem as Content was not Refreshing according to the route.
For that problem I found the observer HOC of Mobx was interfering with my router. I had to wrap my content with withRouter as below to fix this problem.
withRouter(observer(Content));
I hope my solution will come in handy for someone as i found no tutorial with the examples of React Router implementation like mine on the web.
add a comment |
I fixed my problem by having BrowserRouter like below as suggested by @PreDinnerSnack.
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
Then i used Navlink in My Sidebar component. Which fixed the problem of navigating to the desired route without page refresh. But then i faced another problem as Content was not Refreshing according to the route.
For that problem I found the observer HOC of Mobx was interfering with my router. I had to wrap my content with withRouter as below to fix this problem.
withRouter(observer(Content));
I hope my solution will come in handy for someone as i found no tutorial with the examples of React Router implementation like mine on the web.
I fixed my problem by having BrowserRouter like below as suggested by @PreDinnerSnack.
<BrowserRouter>
<React.Fragment>
/* sidebar component */
<SideBar store = Mystore />
<div className="main-panel">
/* navbar component */
<NavBar store = Mystore/>
/* content component */
<Content store = Mystore/>
/* footer component */
</div>
</React.Fragment>
</BrowserRouter>
Then i used Navlink in My Sidebar component. Which fixed the problem of navigating to the desired route without page refresh. But then i faced another problem as Content was not Refreshing according to the route.
For that problem I found the observer HOC of Mobx was interfering with my router. I had to wrap my content with withRouter as below to fix this problem.
withRouter(observer(Content));
I hope my solution will come in handy for someone as i found no tutorial with the examples of React Router implementation like mine on the web.
answered Nov 15 '18 at 18:27
ahnafscmahnafscm
174
174
add a comment |
add a comment |
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%2f53314144%2fexecute-route-change-from-siblings-component-in-react-router-v4%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
Have you tried using Link?
– c-chavez
Nov 15 '18 at 7:14
Have you tried navigationOption props?
– Rajneesh Shukla
Nov 15 '18 at 7:30
I have tried to use links but i guess it needs a Router but my sidebar component does not have a Router. How can i use link in sidebar component if my Router is set up in content ?
– ahnafscm
Nov 15 '18 at 8:11