Konsta UI 提供方便的 useTheme
hook 来检测当前活动主题 (ios
或 material
),该主题由 App 组件 或 KonstaProvider 设置。
/* App.jsx */
import { App } from 'konsta/react';
import { HomePage } from './path/to/HomePage.jsx';
export default function MyApp() {
return (
<>
{/* set theme on App component */}
<App theme="ios">
<HomePage />
</App>
</>
);
}
/* HomePage.jsx */
import { useTheme, Page } from 'konsta/react';
export default function MyApp() {
// get currently set theme
const theme = useTheme();
console.log(theme); // -> 'ios'
return (
<>
<Page>
{theme === 'ios' ? <p>Theme is iOS</p> : <p>Theme is Material</p>}
</Page>
</>
);
}