【问题标题】:How to Implement dark mode in React native Classes React navigation 5?如何在 React 本机类 React 导航 5 中实现暗模式?
【发布时间】:2023-04-09 08:36:02
【问题描述】:

我正在尝试为我的应用实现暗模式。如何在 React 导航 V5 的类中做到这一点

【问题讨论】:

    标签: react-native react-navigation-v5 darkmode


    【解决方案1】:

    https://stackoverflow.com/a/68005237/11711280 为 reactjs 回答这个问题

      constructor(props) {
        super(props);//seems to be the same here for reactjs as react-native
        this.darkModeQuery = window.matchMedia(`(prefers-color-scheme: dark)`);
        this.lightModeQuery = window.matchMedia(`(prefers-color-scheme: light)`);
      }
      componentDidMount() {
        this.darkModeListener = navigation.addListener(this.darkModeQuery, this.setDark);
        this.lightModeListener = navigation.addListener(this.lightModeQuery, this.setLight);
      }
      setDark = (darkModeQuery) =>
        this.setState({
          darkMode: darkModeQuery.length !== 0,
          darkModeListener: darkModeQuery
        });
      setLight = (lightModeQuery) =>
        this.setState({
          lightMode: lightModeQuery.length !== 0,
          lightModeListener: lightModeQuery
        });
      componentWillUnmount() {
        const { lightModeListener, darkModeListener } = this.state;
        if (lightModeListener)
          this.lightModeListener.remove();
        if (darkModeListener)
          this.darkModeListener.remove();
      }
    

    https://stackoverflow.com/a/55590338/11711280我发现问如何将window.addEventListener(this.mediaQuery,this.myFunc)翻译成this.darkModeListener = navigator.addEventListener(this.mediaQuery,this.myFunc)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多