When applying TailwindCSS styles in Next.js, I encounter the problem that the styles are not rendered.
P粉933003350
P粉933003350 2023-08-27 09:05:26
0
1
465
<p>Using TailwindCSS to set the background cover, I extracted the color from the bookId (10 digits) in useEffect. The colors get updated and the component re-renders with the updated color values, but the background color of the rendered page is still the same color of its parent div. </p> <pre class="brush:php;toolbar:false;">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 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 p-4`}> {props.children} </section> </div> </div> ) : ( <p className='text-2xl'>Loading..</p> )} </Fragment> ) }</pre> <p>However, when I replace the color variable with a color value (say 'from-red-500'), the background color is visible in the rendered page. </p> <p>I also tried replacing the setColor code in useEffect with getStaticProps, but the static version of the code didn't solve the problem (when using the color variable). </p> <p>Thanks for any help. </p>
P粉933003350
P粉933003350

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!