score:-1
Just set value to your tab, that should do the work
<Tab value={0} label="Item One" />
<Tab value={1} label="Item Two" />
<Tab value={2} label="Item Three" />
score:0
This simple solution worked for me. grabing url from the browser using window.location.pathname
and updating value state whenever browser is refreshed and reloaded using componentDidmount
lifecycle method.
class SimpleTabs extends React.Component {
state = {
value: 0,
}
handleChange = (event, `enter code here`value) => {
this.setState({ value })
}
componentDidMount() {
// assuming Item one as url "/" Item two as url "/itemTwo" and so on
switch (window.location.pathname) {
case "/":
this.setState({ value:0 })
break;
case "/itemTwo":
this.setState({ value:1 })
break;
case "/itemThree":
this.setState({ value:2 })
break;
default:
this.setState({ value })
break;
}
}
render() {
const { classes } = this.props
const { value } = this.state
return (
<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>
)
}
}
score:1
I manage to solve this by using the useLocation hook and returning the state value based on the path.
const currentTab = () => {
let path = location.pathname;
if (path === "/") return 0;
else if (path === "/path2") return 1;
else if (path === "/path3") return 2;
else if (path === "/path4") return 3;
else if (path === "/path5") return 4;
};
and then replacing the useState value from 0 (the first tab) to the currentTab function.
const [value, setValue] = useState(currentTab);
This solution helped me a lot, saw it somewhere else, so I hope it helps anyone!
score:3
That is because on page refresh the component is reinitialized, the state.value
is set to 0.
The onChange
method needs to store the selected tab somewhere, in the page URL for example.
In the component constructor or on componentDidMount
the state.value
needs to be set from the storage where it was set previously.
Source: stackoverflow.com
Related Query
- Reactjs keep Material Ui Tab selected on refresh
- Reactjs adding the selected react-bootstrap tab key # to the current URL
- How to get the index of the selected item from a dropdown in ReactJs and material UI?
- Refresh a tab and not the ReactJs Ajax page
- Material UI Popper over TextField how to keep popper open if the popper options are selected
- Material UI tab selected has different color than the rest of tabs
- How to get selected value of a dropdown menu in ReactJS
- Reactjs Browser Tab Close Event
- How to display a image selected from input type = file in reactJS
- Check if the browser tab is in focus in ReactJS
- How to submit the form by Material UI Dialog using ReactJS
- Material UI v4 makeStyles exported from a single file doesn't retain the styles on refresh
- How do I keep a user logged into firebase after refresh in React.JS?
- How to change the text color of the selected row in material ui table
- How to keep React components state after browser refresh
- Test with reactjs renderIntoDocument keep failed due to required DOM
- React add class active on selected tab
- Deploying Reactjs website on github pages with routing results in 404 error on refresh
- All React-bootstrap Tabs render function gets called every time regardless of which tab was selected
- Close current browser tab on button click, using reactjs
- How can I use CSS @media for responsive with makeStyles on Reactjs Material UI?
- reactJS material UI Icon button to upload file
- ReactJS - Using material UI Grid spacing
- ReactJS - trigger event even if the same option is selected from select dropdown
- Capturing Tab Key on Material UI AutoComplete Component
- How do I change the height of the tab indicator in material ui
- Dynamic height for material ui tab containers
- How to use refresh token in reactjs
- How to change Material UI tab button width
- Influence tab order of Material UI controls
More Query from same tag
- Windows.location.reload() not working in react js app hosted in hostgator
- How do I validate a checkout form in React?
- How to call other reducers in createSlice to change state?
- ReactJS: programmatically change component tag with JSX
- Issue with updating a property that is an array nested inside another array in redux state
- React.js - State doesn't update when function is called in componentDidMount()
- Disable Table pagination in material-ui Reactjs
- Microsoft Teams React App prevent style overwriting from SharePoint Online
- how to create a paging modal in reactjs
- webpack babel loader fails to compile jsx
- Clicking reverts array order in react
- How to send response from Nodemailer
- useState variable is called before useEffect API call
- Get react element string type
- How to get input textbox value in parent file from component in React Js
- readAsDataURL Multiple Image Upload ReactJS
- How to "Hide a div by clicking alert box ok button" in reactjs
- Click on <div> to focus <input> (React)
- React: Updating form on button click
- How can I mock return data from a custom hook in React/Jest?
- Overriding MUI Chip deleteIcon color
- ReactJS: Where in the project to put a Javascript function that is called when a onclick() event occurs
- React passing props from const to child component
- Why does popper jump to top-left corner when underlying component re-renders?
- Store x and y coordinates of mouse movements in a text file
- Loading SASS Modules with TypeScript in Webpack 2
- Can't get Google Ads to show in React no matter what I do
- TypeError: path.split is not a function in React-hook-form
- Fabric JS storing steps in database
- Mobx only re-render item when computed value changes