English 中文(简体)
React Themechangeing Issue: CSS don t Update on subsequently Themechanges
原标题:React Theme Switching Issue: CSS doesn t Update on Subsequent Theme Switches

我会遇到一个问题,即在我康复申请中转调主题。 申请支持两个主题,我称之为主题一和主题二。 I m 利用zy装,根据选定主题积极装上相应的主题组成部分。

每一个主题都界定了自己对共同的纽顿阶级的束缚。 例如,在“主题一”中, but有红色,而在“主题二”中,则有色。

当我用一个子子把主题转换成时,问题就会出现。 最初,在从主题一向主题二过渡时, but子的彩色从红土改写。 然而,在转向“主题一”之后,纽伦肤色未能像预期的那样重回红。 随后的主题交换也并未反映纽特的任何变化。

我的法典结构简化版:

// App.js
import React, { useState, Suspense } from  react ;
import ThemeContext from  ./ThemeContext ;
import  ./App.css ;

export const MawbThemeContext = createContext({ theme: "" })

const ThemeOne = React.lazy(() => import( ./ThemeOne ));
const ThemeTwo = React.lazy(() => import( ./ThemeTwo ));

const App = () => {
  const [theme, setTheme] = useState( one );

const handleChangeTheme = () => {
  startTransition(() => {
    if (theme ===  one ) {
      setTheme( two );
    } else {
      setTheme( one );
    }
  });
};


  return (
    <ThemeContext.Provider value={{ theme, handleChangeTheme }}>
      <Suspense fallback={<div>Loading...</div>}>
        {theme ===  one  ? <ThemeOne /> : <ThemeTwo />}
      </Suspense>
    </ThemeContext.Provider>
  );
};

export default App;
// ThemeOne.js
import React, { useContext } from  react ;
import ThemeContext from  ./ThemeContext ;
import  ./ThemeOne.css ;

const ThemeOne = () => {
  const { handleChangeTheme } = useContext(ThemeContext);

  return (
    <>
      <button className="common-btn-one" onClick={handleChangeTheme}>
        Theme One Button
      </button>
    </>
  );
};

export default ThemeOne;
// ThemeTwo.js
import React, { useContext } from  react ;
import ThemeContext from  ./ThemeContext ;
import  ./ThemeTwo.css ;

const ThemeTwo = () => {
  const { handleChangeTheme } = useContext(ThemeContext);

  return (
    <>
      <button className="common-btn-two" onClick={handleChangeTheme}>
        Theme Two Button
      </button>
    </>
  );
};

export default ThemeTwo;

问题回答

这里的情况是:如果你想要改变一个共同的纽芬兰的主题,你是否真正需要两个不同的组成部分? 我对你的建议是,只建立一个纽芬兰部分,只改变这个纽州的主题:

//App.jsx
import { useState } from "react"
import Button from  ./components/Button 
import ThemeContext from "./context/ThemeContext"

function App() {
  const [theme, setTheme] = useState("light")
  const value = { theme, setTheme }

  return (
    <div className="App">
      <ThemeContext.Provider value={value}>
        <Button />
      </ThemeContext.Provider>
    </div>
  )
}

export default App
//ThemeContext.js
import React from "react"

const ThemeContext = React.createContext({
  theme: "light",
  setTheme: () => {},
})

export default ThemeContext
import { useContext } from "react"
import ThemeContext from "../context/ThemeContext.js"

const Button = () => {
  const { theme, setTheme } = useContext(ThemeContext)
  
  return (
    <button
      onClick={() => setTheme(theme == "dark" ? "light" : "dark")}
      style={{backgroundColor: theme === "dark" ? "red" : "white"}}
    >
      Change theme
    </button>
  )
}

export default Button;

希望能帮助你们! 亲爱!





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签