如何从react-navigation获取主题类型(Typescript)

问题描述 投票:0回答:1

我想创建一个钩子 useThemeStyled 在我的样式中用React-Native获得react-navigation主题。

import { useTheme } from "@react-navigation/native";
import { Theme } from "@react-navigation/native/lib/typescript/src/types";

export const HomeScreen = () => {
  const sylings = useThemeStyled(styles);
  return (
    <View>
      <Text style={sylings.title}>HomeScreen</Text>
    </View>
  );
};

const styles = (theme: Theme) => ({
  title: {
    color: theme.colors.text
  }
});

const useThemeStyled = (style: (theme: Theme) => any) => {
  const theme = useTheme();
  return StyleSheet.create(style(theme));
};

import { Theme } from "@react-navigation/native/lib/typescript/src/types"; 正确吗?

有没有一种方法可以不用定义 :Theme 类型为主题参数,当声明 const styles ...?

react-native react-navigation typescript-typings
1个回答
0
投票

使用 typeof

type Theme = typeof DefaultTheme;
© www.soinside.com 2019 - 2024. All rights reserved.