My problem - whenever i use <Tabs> component the onChange method calls the handleTabChange function. The component gets called again and the after repainting the useEffect gets called. This causes my page to scroll on top.
How do i make sure the handleChange and useEffect does not cause the page to scroll on top?
export function VarianceGraph({ variancesGraphData }) {
if (variancesGraphData === undefined) {
return null;
}
const [currentProject, setCurrentProject] = useState('All');
const [currentTab, setCurrentTab] = useState('ABCD');
const projectCodes = prepareProjectCodesForConsumption(variancesGraphData);
let data = {};
function handleProjectChange(event) {
const { value } = event.target;
setCurrentProject(value);
}
function handleTabChange(event, tabValue) {
setCurrentTab(tabValue);
}
data = prepareProjectDataForRechartsConsumption(currentProject, variancesGraphData);
const hideABCD = data.ABCD.length < 1;
const hideBCDF = data['BCDF'].length < 1;
useEffect(() => {
if (hideABCD && hideBCDF) {
setCurrentTab('None');
} else if (hideABCD) {
setCurrentTab('BCDF');
} else if (hideBCDF) {
setCurrentTab('ABCD');
}
});
return (
<Grid container direction="row" justify="flex-start" alignItems="flex-start">
<Grid item xs={12}>
I have tried -
I need to have the setCurrentTab in some function so as to not render infinitely.
I have tried using the useLayoutEffect but it has the same behavior.
I can write the component code further if required. Please help. Thanks in advance.
If I am understanding right, you only want your useEffect code run once after VarianceGraph render.
Try to add a second argument [] to your useEffect, otherwise the code in useEffect will run after any state change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With