I'm trying to make a SPA with react and using styled-component for styling and theme changing. The problem i have when i add classes by Javascript and then try to change theme all the classes added by JavaScript are removed.
Can someone tell me what is the reason for this and how can i solve this problem?
Here is the example, where you'll see that when you scroll down on i add class to header with help of JavaScript to give bottom border but when i toggle theme with button then added class is removed.
const themeToggler = () => {
theme === "light" ? setTheme("dark") : setTheme("light");
};
const StyleHeader = styled.header `
background: ${(props) => props.theme.body};
`;
function scrollPos(maxScrollPos) {
let scrollPos = document.documentElement.scrollTop;
if (scrollPos >= maxScrollPos) {
addClassToElement("glow-shadow", ".header");
} else {
removeClassFromElement("glow-shadow", ".header");
}
}
<ThemeProvider theme={theme==="light" ? light : dark}>
<GlobalStyles />
<StyleHeader className={`header pt-3 pb-3 fixed-top`}>
<button onClick={themeToggler}>Toggle</button>
</StyleHeader>
<div className="content">
scrool down to see border on header then click the toggle button to change the theme and border class will be removed
<h1>h1</h1>
</div>
<h1 className="content">h1</h1>
<h1 className="content">h1</h1>
<h1 className="content">h1</h1>
</ThemeProvider>