LZWcc's Blog

Back

1. 静态资源配置#

阅读官方文档可知: 链接可以放在 index.html头部。

index.html<head> 标签中预置一个 idtheme-link 的链接,作为初始主题:

<link id="theme-link" rel="stylesheet" href="/themes/lara-light-blue/theme.css">
html

选择不同主题会改变HTML头中的css文件链接

将所需主题从 ./node_modules/primereact/resources/themes中复制到 ./public/themes

cp -r ./node_modules/primereact/resources/themes/lara-light-cyan ./public/themes/lara-light-cyan
cp -r ./node_modules/primereact/resources/themes/lara-dark-cyan ./public/themes/lara-dark-cyan
bash

2. 自定义 Hook#

创建 useThemeSwicher.jshook

3. 在组件中使用#

现在可以在导航栏(如 NavBar.jsx)中引入该 Hook,并通过 PrimeReact 的 Button 组件触发切换。


参考资料#

https://primereact.org/theming/

https://stackoverflow.com/questions/68327342/how-can-one-have-a-theme-switcher-in-primereact

如何在 primereact 中实现主题切换器
https://lzwcc.xyz/blog/react-primereact/
Author LZWcc
Published at March 3, 2026
Comment seems to stuck. Try to refresh?✨