One way of conditionally load a CSS file in a React component is by using react-helmet
. It will manage all of your changes to the document head.
1 | import React, { useState } from "react"; |
In this case, the CSS file dark.css
will only be loaded when dark
state is true
, otherwise it will not be found in the <head>
tag.