P粉1913232362023-08-28 00:23:31
This is a known issue with tailwindcss and dynamic classes, because the class is applied after rendering, its effect will not be generated by tailwind unless there is another element with the same class as the static class.
Therefore, you can use tailwind's "safelist" to solve this problem. In your tailwind.config, define a safelist array containing all the tailwind classes you need to generate that do not exist as static classes in your code.
tailwind.config.js:
module.exports = { content: [ './pages/**/*.{html,js}', './components/**/*.{html,js}', ], safelist: [ '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', ] // ... }
Now these classes will always be generated, so when you apply them dynamically, they will change accordingly.
Please note that you need to restart the server after adding to safelist.
Another manual solution is to create a hidden element and add all required classes to it so that they are generated even if you get them dynamically after rendering.
<div className="hidden from-red-500"></div>
But I think safelist is better.