• <small id='LobJ7'></small><noframes id='LobJ7'>

    <i id='LobJ7'><tr id='LobJ7'><dt id='LobJ7'><q id='LobJ7'><span id='LobJ7'><b id='LobJ7'><form id='LobJ7'><ins id='LobJ7'></ins><ul id='LobJ7'></ul><sub id='LobJ7'></sub></form><legend id='LobJ7'></legend><bdo id='LobJ7'><pre id='LobJ7'><center id='LobJ7'></center></pre></bdo></b><th id='LobJ7'></th></span></q></dt></tr></i><div id='LobJ7'><tfoot id='LobJ7'></tfoot><dl id='LobJ7'><fieldset id='LobJ7'></fieldset></dl></div>

      <legend id='LobJ7'><style id='LobJ7'><dir id='LobJ7'><q id='LobJ7'></q></dir></style></legend>

          <bdo id='LobJ7'></bdo><ul id='LobJ7'></ul>
      1. <tfoot id='LobJ7'></tfoot>
      2. 反应原生导航 useTheme()

        时间:2023-11-29

            <tbody id='OC7rF'></tbody>

          <small id='OC7rF'></small><noframes id='OC7rF'>

          • <tfoot id='OC7rF'></tfoot>

              • <i id='OC7rF'><tr id='OC7rF'><dt id='OC7rF'><q id='OC7rF'><span id='OC7rF'><b id='OC7rF'><form id='OC7rF'><ins id='OC7rF'></ins><ul id='OC7rF'></ul><sub id='OC7rF'></sub></form><legend id='OC7rF'></legend><bdo id='OC7rF'><pre id='OC7rF'><center id='OC7rF'></center></pre></bdo></b><th id='OC7rF'></th></span></q></dt></tr></i><div id='OC7rF'><tfoot id='OC7rF'></tfoot><dl id='OC7rF'><fieldset id='OC7rF'></fieldset></dl></div>

              • <legend id='OC7rF'><style id='OC7rF'><dir id='OC7rF'><q id='OC7rF'></q></dir></style></legend>
                  <bdo id='OC7rF'></bdo><ul id='OC7rF'></ul>

                • 本文介绍了反应原生导航 useTheme()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试直接从样式中访问 useTheme()但到目前为止,我的解决方案似乎不起作用.我不会出错.有没有办法做我想做的事?

                  I'm trying to access useTheme() directly from the styles But so far my solution doesn't seem to work. I'm not getting in error back. Is there a way to do what I'm trying to do?

                   import { StyleSheet } from "react-native";
                  
                      import { useTheme } from '@react-navigation/native'
                      
                      export default function () {
                              const { colors } = useTheme();
                              const styles = GlobalStyles({ colors: colors })
                              return styles
                          }
                          
                          const GlobalStyles = (props) => StyleSheet.create({
                          
                              container: {
                                  flex: 1,
                                  backgroundColor: props.colors.backgroundColor,
                          
                              },
                          })
                  

                  在组件中访问样式

                   import React from "react";
                  import GlobalStyles from "../globalStyles.js"
                      
                      class Inventory extends React.Component {
                      
                      render() {
                      
                              return (
                                  <View style={globalStyles.container}>
                              )
                      
                       }
                  

                  推荐答案

                  你有几个问题:你只能在钩子或函数组件中使用钩子.所以你可以将你的全局样式表转换成一个钩子:

                  You have a couple of issues: you can only use a hook within a hook or a function component. So you could convert your global stylesheet into a hook:

                  import { StyleSheet } from "react-native";
                  import { useTheme } from '@react-navigation/native'
                      
                  const getGlobalStyles = (props) => StyleSheet.create({
                    container: {
                      flex: 1,
                      backgroundColor: props.colors.backgroundColor,
                    },
                  });
                  
                  function useGlobalStyles() {
                    const { colors } = useTheme();
                  
                    // We only want to recompute the stylesheet on changes in color.
                    const styles = React.useMemo(() => getGlobalStyles({ colors }), [colors]);
                  
                    return styles;
                  }
                  
                  export default useGlobalStyles;
                  

                  然后,您可以通过将 Inventory 类组件转换为函数组件并使用新的 useGlobalStyles 挂钩来使用该挂钩.

                  Then you can use the hook by converting your Inventory class component into a function component and using the new useGlobalStyles hook.

                  import React from "react";
                  import useGlobalStyles from "../globalStyles.js"
                      
                  const Inventory = () => {
                    const globalStyles = useGlobalStyles();
                  
                    return (
                      <View style={globalStyles.container}>
                    )
                  }
                  

                  这篇关于反应原生导航 useTheme()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:按键排序和分组 React Native SectionList 下一篇:REACT NATIVE:有办法识别设备类型(智能手机、平板电脑、笔记本电脑)吗?

                  相关文章

                    1. <legend id='F7jE9'><style id='F7jE9'><dir id='F7jE9'><q id='F7jE9'></q></dir></style></legend>
                        <bdo id='F7jE9'></bdo><ul id='F7jE9'></ul>
                    2. <small id='F7jE9'></small><noframes id='F7jE9'>

                    3. <tfoot id='F7jE9'></tfoot>

                      <i id='F7jE9'><tr id='F7jE9'><dt id='F7jE9'><q id='F7jE9'><span id='F7jE9'><b id='F7jE9'><form id='F7jE9'><ins id='F7jE9'></ins><ul id='F7jE9'></ul><sub id='F7jE9'></sub></form><legend id='F7jE9'></legend><bdo id='F7jE9'><pre id='F7jE9'><center id='F7jE9'></center></pre></bdo></b><th id='F7jE9'></th></span></q></dt></tr></i><div id='F7jE9'><tfoot id='F7jE9'></tfoot><dl id='F7jE9'><fieldset id='F7jE9'></fieldset></dl></div>