小编Bik*_*ath的帖子

在 NextJs 中动态应用 TailwindCSS 样式时不会呈现

为了使用 TailwindCSS 设置背景封面,我从 useEffect 内的 bookId(10 位数字)中提取了颜色。颜色会更新,组件会使用更新后的颜色值重新渲染,但渲染页面上的背景颜色仍然与其父 div 相同。

const colors = [
    'from-red-500',
    'from-orange-500',
    'from-yellow-500',
    'from-green-500',
    'from-cyan-500',
    'from-blue-500',
    'from-indigo-500',
    'from-violet-500',
    'from-purple-500',
    'from-pink-500',
]

function BgCover(props) {
    const [color, setColor] = useState(null)
    const router = useRouter()

    useEffect(() => {
        const id = router.query.bookId
        const index = id.slice(-1) //extract the index from bookId
        const bgColor = colors[index]
        setColor(bgColor)
    }, [])

    return (
        <Fragment>
            {color ? (
                <div className='flex-grow scrollbar-hide select-none relative'>
                    <div className={`bg-gradient-to-b ${color} to-black`}>
                        <section
                            className={`flex flex-col md:flex-row items-center justify-center …
Run Code Online (Sandbox Code Playgroud)

reactjs next.js tailwind-css

1
推荐指数
1
解决办法
1347
查看次数

标签 统计

next.js ×1

reactjs ×1

tailwind-css ×1