useTheme
useTheme
フックは、現在アクティブなテーマにアクセスできます。このフックは独自コンポーネントで使用して、テーマの変化に応答させることができます。
import * as React from 'react';
import { TouchableOpacity, Text } from 'react-native';
import { useTheme } from '@react-navigation/native';
// Black background and white text in light theme, inverted on dark theme
function MyButton() {
const { colors } = useTheme();
return (
<TouchableOpacity style={{ backgroundColor: colors.card }}>
<Text style={{ color: colors.text }}>Button!</Text>
</TouchableOpacity>
);
}
テーマの設定方法の詳細と使用方法のガイドについては、テーマガイドを参照してください。
クラスコンポーネントでの使用
クラスコンポーネントを関数コンポーネントでラップしてフックを使用できます
class MyButton extends React.Component {
render() {
// Get it from props
const { theme } = this.props;
}
}
// Wrap and export
export default function (props) {
const theme = useTheme();
return <MyButton {...props} theme={theme} />;
}