navigation problem in Tabs provided by Material UI for React js ProjectReactJS - Does render get called any time “setState” is called?Programmatically navigate using react routerHow to pass state value from parent element to child in reactHow to highlight the selected bootstrap tab in ReactHow get value datapicker in react toobox custom?React this.state not updating correctly when called within if else statement with use of immutability-helper`setState` in React portal containing a text input causes browser to scroll in SafariTrying to use this.props.history.push but this.props returns an empty objectHow to add Material-UI form with Steppers in React-Amin Create ComponentReact Tabs Lazy Load
What are the cons of stateless password generators?
Rampant sharing of authorship among colleagues in the name of "collaboration". Is not taking part in it a death knell for a future in academia?
What language is Raven using for her attack in the new 52?
Why put copper in between battery contacts and clamps?
Blank spaces in a font
Classic vs Modern Experience
What is a good example for artistic ND filter applications?
Why is it considered acid rain with pH <5.6?
How long does it take for electricity to be considered OFF by general appliances?
Can Lightning Lure be used to knock out a creature like a magical Taser?
Wrapping IMemoryCache with SemaphoreSlim
Is it okay for me to decline a project on ethical grounds?
How to efficiently shred a lot of cabbage?
Why are we moving in circles with a tandem kayak?
Should I intervene when a colleague in a different department makes students run laps as part of their grade?
Why would anyone ever invest in a cash-only etf?
Why did House of Representatives need to condemn Trumps Tweets?
Can a US President, after impeachment and removal, be re-elected or re-appointed?
A cubeful of three-dimensional devilry
Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?
What would the United Kingdom's "optimal" Brexit deal look like?
How did the SysRq key get onto modern keyboards if it's rarely used?
Is it safe if the neutral lead is exposed and disconnected?
Is there a word to describe someone who is, or the state of being, content with hanging around others without interacting with them?
navigation problem in Tabs provided by Material UI for React js Project
ReactJS - Does render get called any time “setState” is called?Programmatically navigate using react routerHow to pass state value from parent element to child in reactHow to highlight the selected bootstrap tab in ReactHow get value datapicker in react toobox custom?React this.state not updating correctly when called within if else statement with use of immutability-helper`setState` in React portal containing a text input causes browser to scroll in SafariTrying to use this.props.history.push but this.props returns an empty objectHow to add Material-UI form with Steppers in React-Amin Create ComponentReact Tabs Lazy Load
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am using React Js in my project and the Material UI Tabs component to navigate between 2 components.
When I go to Tab 2 or Tab 1 by clicking on it, my component renders and the route gets changed but the tab indicator does not get updated on the active tab.
I've already tried to update the state on the button click but it isn't working
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
this.setState( value );
console.log(this)
;
navigateToTab2 = () =>
this.setState( value:1 );
console.log(this.state)
this.props.history.push("/tab2")
navigateToTab1 = (facility, event) =>
this.setState( value:0 );
console.log(this.state)
this.props.history.push('/tab1',
facilityId: facility.facilityId,
facilityName: facility.facilityName,
);
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
onClick = this.navigateToTab1
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
onClick = this.navigateToTab2
/>
</Tabs>
</div>
);
I expect that when I click on Tab1 it should go to Tab1 and render that component with Tab indicator be on Tab 1 and when I click on Tab 2 it should go to Tab 2 and render the Tab 2 component with tab indicator on tab 2[enter image description here][1]
[1]: https://i.stack.imgur.com/YRaqs.png
javascript reactjs material-ui setstate react-state-management
add a comment |
I am using React Js in my project and the Material UI Tabs component to navigate between 2 components.
When I go to Tab 2 or Tab 1 by clicking on it, my component renders and the route gets changed but the tab indicator does not get updated on the active tab.
I've already tried to update the state on the button click but it isn't working
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
this.setState( value );
console.log(this)
;
navigateToTab2 = () =>
this.setState( value:1 );
console.log(this.state)
this.props.history.push("/tab2")
navigateToTab1 = (facility, event) =>
this.setState( value:0 );
console.log(this.state)
this.props.history.push('/tab1',
facilityId: facility.facilityId,
facilityName: facility.facilityName,
);
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
onClick = this.navigateToTab1
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
onClick = this.navigateToTab2
/>
</Tabs>
</div>
);
I expect that when I click on Tab1 it should go to Tab1 and render that component with Tab indicator be on Tab 1 and when I click on Tab 2 it should go to Tab 2 and render the Tab 2 component with tab indicator on tab 2[enter image description here][1]
[1]: https://i.stack.imgur.com/YRaqs.png
javascript reactjs material-ui setstate react-state-management
add a comment |
I am using React Js in my project and the Material UI Tabs component to navigate between 2 components.
When I go to Tab 2 or Tab 1 by clicking on it, my component renders and the route gets changed but the tab indicator does not get updated on the active tab.
I've already tried to update the state on the button click but it isn't working
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
this.setState( value );
console.log(this)
;
navigateToTab2 = () =>
this.setState( value:1 );
console.log(this.state)
this.props.history.push("/tab2")
navigateToTab1 = (facility, event) =>
this.setState( value:0 );
console.log(this.state)
this.props.history.push('/tab1',
facilityId: facility.facilityId,
facilityName: facility.facilityName,
);
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
onClick = this.navigateToTab1
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
onClick = this.navigateToTab2
/>
</Tabs>
</div>
);
I expect that when I click on Tab1 it should go to Tab1 and render that component with Tab indicator be on Tab 1 and when I click on Tab 2 it should go to Tab 2 and render the Tab 2 component with tab indicator on tab 2[enter image description here][1]
[1]: https://i.stack.imgur.com/YRaqs.png
javascript reactjs material-ui setstate react-state-management
I am using React Js in my project and the Material UI Tabs component to navigate between 2 components.
When I go to Tab 2 or Tab 1 by clicking on it, my component renders and the route gets changed but the tab indicator does not get updated on the active tab.
I've already tried to update the state on the button click but it isn't working
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
this.setState( value );
console.log(this)
;
navigateToTab2 = () =>
this.setState( value:1 );
console.log(this.state)
this.props.history.push("/tab2")
navigateToTab1 = (facility, event) =>
this.setState( value:0 );
console.log(this.state)
this.props.history.push('/tab1',
facilityId: facility.facilityId,
facilityName: facility.facilityName,
);
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
onClick = this.navigateToTab1
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
onClick = this.navigateToTab2
/>
</Tabs>
</div>
);
I expect that when I click on Tab1 it should go to Tab1 and render that component with Tab indicator be on Tab 1 and when I click on Tab 2 it should go to Tab 2 and render the Tab 2 component with tab indicator on tab 2[enter image description here][1]
[1]: https://i.stack.imgur.com/YRaqs.png
javascript reactjs material-ui setstate react-state-management
javascript reactjs material-ui setstate react-state-management
edited Mar 26 at 20:38
Typhlow
253 bronze badges
253 bronze badges
asked Mar 26 at 20:33
Purvish OzaPurvish Oza
108 bronze badges
108 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
A couple things:
There's really no point in setting onClick
events for each Tab
, because everything you need to do can be done in the onChange
handler for Tabs
.
So, when onChange
is fired, you can set the selected tab value, then call this.props.history.push
. Make sure you are rendering your child components based on the tab that was selected.
If you want the route /tab2
to automatically select tab2, then youll need to check the current route in componentDidMount()
, then set state accordingly. So for instance, if route == '/tab2' then state.value == 2
Here's an example taken straight from Material-UI docs.
<div className=classes.root>
<AppBar position="static">
<Tabs value=value onChange=this.handleChange>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
value === 0 && <TabContainer>Item One</TabContainer>
value === 1 && <TabContainer>Item Two</TabContainer>
value === 2 && <TabContainer>Item Three</TabContainer>
</div>
Your component should end up looking something like this
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
if (value == 0) this.props.history.push('/tab1')
if(value == 1) this.props.history.push('/tab2')
;
componentDidMount()
if(this.props.location.pathname == '/tab1')
this.setState(value: 0)
else
this.setState(value: 1)
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
/>
</Tabs>
value == 0 && <p>Here is tab 1 content</p>
value == 1 && <p>Here is tab 2 content</p>
</div>
);
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
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%2f55365767%2fnavigation-problem-in-tabs-provided-by-material-ui-for-react-js-project%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A couple things:
There's really no point in setting onClick
events for each Tab
, because everything you need to do can be done in the onChange
handler for Tabs
.
So, when onChange
is fired, you can set the selected tab value, then call this.props.history.push
. Make sure you are rendering your child components based on the tab that was selected.
If you want the route /tab2
to automatically select tab2, then youll need to check the current route in componentDidMount()
, then set state accordingly. So for instance, if route == '/tab2' then state.value == 2
Here's an example taken straight from Material-UI docs.
<div className=classes.root>
<AppBar position="static">
<Tabs value=value onChange=this.handleChange>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
value === 0 && <TabContainer>Item One</TabContainer>
value === 1 && <TabContainer>Item Two</TabContainer>
value === 2 && <TabContainer>Item Three</TabContainer>
</div>
Your component should end up looking something like this
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
if (value == 0) this.props.history.push('/tab1')
if(value == 1) this.props.history.push('/tab2')
;
componentDidMount()
if(this.props.location.pathname == '/tab1')
this.setState(value: 0)
else
this.setState(value: 1)
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
/>
</Tabs>
value == 0 && <p>Here is tab 1 content</p>
value == 1 && <p>Here is tab 2 content</p>
</div>
);
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
add a comment |
A couple things:
There's really no point in setting onClick
events for each Tab
, because everything you need to do can be done in the onChange
handler for Tabs
.
So, when onChange
is fired, you can set the selected tab value, then call this.props.history.push
. Make sure you are rendering your child components based on the tab that was selected.
If you want the route /tab2
to automatically select tab2, then youll need to check the current route in componentDidMount()
, then set state accordingly. So for instance, if route == '/tab2' then state.value == 2
Here's an example taken straight from Material-UI docs.
<div className=classes.root>
<AppBar position="static">
<Tabs value=value onChange=this.handleChange>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
value === 0 && <TabContainer>Item One</TabContainer>
value === 1 && <TabContainer>Item Two</TabContainer>
value === 2 && <TabContainer>Item Three</TabContainer>
</div>
Your component should end up looking something like this
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
if (value == 0) this.props.history.push('/tab1')
if(value == 1) this.props.history.push('/tab2')
;
componentDidMount()
if(this.props.location.pathname == '/tab1')
this.setState(value: 0)
else
this.setState(value: 1)
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
/>
</Tabs>
value == 0 && <p>Here is tab 1 content</p>
value == 1 && <p>Here is tab 2 content</p>
</div>
);
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
add a comment |
A couple things:
There's really no point in setting onClick
events for each Tab
, because everything you need to do can be done in the onChange
handler for Tabs
.
So, when onChange
is fired, you can set the selected tab value, then call this.props.history.push
. Make sure you are rendering your child components based on the tab that was selected.
If you want the route /tab2
to automatically select tab2, then youll need to check the current route in componentDidMount()
, then set state accordingly. So for instance, if route == '/tab2' then state.value == 2
Here's an example taken straight from Material-UI docs.
<div className=classes.root>
<AppBar position="static">
<Tabs value=value onChange=this.handleChange>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
value === 0 && <TabContainer>Item One</TabContainer>
value === 1 && <TabContainer>Item Two</TabContainer>
value === 2 && <TabContainer>Item Three</TabContainer>
</div>
Your component should end up looking something like this
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
if (value == 0) this.props.history.push('/tab1')
if(value == 1) this.props.history.push('/tab2')
;
componentDidMount()
if(this.props.location.pathname == '/tab1')
this.setState(value: 0)
else
this.setState(value: 1)
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
/>
</Tabs>
value == 0 && <p>Here is tab 1 content</p>
value == 1 && <p>Here is tab 2 content</p>
</div>
);
A couple things:
There's really no point in setting onClick
events for each Tab
, because everything you need to do can be done in the onChange
handler for Tabs
.
So, when onChange
is fired, you can set the selected tab value, then call this.props.history.push
. Make sure you are rendering your child components based on the tab that was selected.
If you want the route /tab2
to automatically select tab2, then youll need to check the current route in componentDidMount()
, then set state accordingly. So for instance, if route == '/tab2' then state.value == 2
Here's an example taken straight from Material-UI docs.
<div className=classes.root>
<AppBar position="static">
<Tabs value=value onChange=this.handleChange>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</Tabs>
</AppBar>
value === 0 && <TabContainer>Item One</TabContainer>
value === 1 && <TabContainer>Item Two</TabContainer>
value === 2 && <TabContainer>Item Three</TabContainer>
</div>
Your component should end up looking something like this
class NavBars extends React
state =
value: 0,
;
handleChange = (event, value) =>
if (value == 0) this.props.history.push('/tab1')
if(value == 1) this.props.history.push('/tab2')
;
componentDidMount()
if(this.props.location.pathname == '/tab1')
this.setState(value: 0)
else
this.setState(value: 1)
render()
const classes = this.props;
const value = this.state;
return (
<div className=classes.root>
<Tabs
value=value
onChange=this.handleChange
classes= root: classes.tabsRoot, indicator: classes.tabsIndicator
>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 1"
/>
<Tab
disableRipple
classes= root: classes.tabRoot, selected: classes.tabSelected
label="Tab 2"
/>
</Tabs>
value == 0 && <p>Here is tab 1 content</p>
value == 1 && <p>Here is tab 2 content</p>
</div>
);
edited Mar 26 at 21:12
answered Mar 26 at 20:44
Isaac VidrineIsaac Vidrine
1,0751 gold badge4 silver badges16 bronze badges
1,0751 gold badge4 silver badges16 bronze badges
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
add a comment |
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
I am fairly new to react, can you please help with some pseudo code for OnChangeHandler? I want to go to Tab 1 when i click on Tab 1 and same for Tab 2
– Purvish Oza
Mar 26 at 20:51
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
@PurvishOza I updated my answer, see if that helps
– Isaac Vidrine
Mar 26 at 21:13
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55365767%2fnavigation-problem-in-tabs-provided-by-material-ui-for-react-js-project%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