I recently had a project with a requirement to support theming on the website. Theme Color change in React JS
We can change the background color of the body tag using react. It is a very easy process in which we can change the color of our project. I am using bootstrap to design the layout, You can use material UI or tailwind according to your use.
Packages & Project Installation
Create React Project using command
npx creact-react-app theme_color
I am using the CDN link of Bootstrap but you should always install bootstrap in your project.
CDN link for the Bootstrap
<!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <!-- Latest compiled and minified JavaScript --> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
How to Install Bootstrap in React App
npm install bootstrap --save
Now Code part is here
import 'bootstrap/dist/css/bootstrap.min.css'; import {useState} from 'react' function ProfileCards() { const [darkTheme,setDarkTheme] = useState(false) return ( <div className={darkTheme == false ? 'light-theme' : 'dark-theme' }> <nav class="navbar navbar-default" style={{backgroundColor:"black"}}> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand text-white" href="#">Theme Change</a> </div> <ul class="nav navbar-nav text-white"> <button className="btn btn-success mt-6" onClick={() => setDarkTheme(prevTheme => !prevTheme)}> Change Theme </button> <li></li> </ul> </div> </nav> <div className="row p-5"> <div className="col-sm-4 mt-5 p-5"> <div class="card shadow-lg p-3 mb-5 bg-white rounded" style={{width: "18rem",backgroundColor:"{color}"}}> <div class="card-body"> <h5 class="card-title">Card title</h5> <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6> <p class="card-text">Some quick example text to build on the card title.</p> <a href="#" class="card-link">Card link</a> <a href="#" class="card-link">Another link</a> </div> </div> </div> <div className="col-sm-4 mt-5 p-5"> <div class="card shadow-lg p-3 mb-5 bg-white rounded" style={{width: "18rem",backgroundColor:"{color}"}}> <div class="card-body"> <h5 class="card-title">Card title</h5> <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6> <p class="card-text">Some quick example text to build on the card title.</p> <a href="#" class="card-link">Card link</a> <a href="#" class="card-link">Another link</a> </div> </div> </div> <div className="col-sm-4 mt-5 p-5"> <div class="card shadow-lg p-3 mb-5 bg-white rounded" style={{width: "18rem",backgroundColor:"{color}"}}> <div class="card-body"> <h5 class="card-title">Card title</h5> <h6 class="card-subtitle mb-2 text-muted">Card subtitle</h6> <p class="card-text">Some quick example text to build on the card title.</p> <a href="#" class="card-link">Card link</a> <a href="#" class="card-link">Another link</a> </div> </div> </div> </div> </div> ) } export default ProfileCards;
Now run the project using the command npm start and by default, this app will run on port no 3000
http://localhost:3002/
See our More Tutorials on React
Records display in Material UI Table