🚀 Day One: Getting Started with React
Day One of React Journey: Converting My Basic Website to React 🖥️
Today, I began my journey of converting my basic HTML/CSS/JS website into a React-based project. I've been curious about React for a while, and today I finally took the first steps. It's quite different from the static websites I'm used to building, but it’s been exciting to learn about its component-based structure and how React can dynamically update the UI. 🌐
What is React?
React is like your trusty sidekick in the world of web development — a JavaScript library that helps you build user interfaces (UIs) for web apps in a much more efficient and scalable way. Imagine creating a dynamic website where things can change without refreshing the whole page. That’s where React shines! 🌟
It lets you build your website in components — which are like LEGO blocks 🧱. Each block (or component) is reusable, which means once you create one, you can plug it in anywhere you want without writing the same code again and again. Awesome, right?
Why Use React?
Now, let’s make it fun. Picture this: You’re running a café ☕. Every day, your customers change their orders, and you need to update the menu dynamically without shutting down the café (aka reloading the page). React helps you do that by using something called the Virtual DOM. Instead of refreshing the entire page (like kicking out all customers and making them come back), React only updates the part of the menu that has changed, saving time and keeping things efficient! 🔥
Here’s a real-time example: You’re browsing Instagram 📸. As you scroll, new posts load without refreshing the whole page, and when you like a post, the heart fills in immediately. That’s React in action — updating just the heart icon, not the entire Instagram feed.
Fun Fact About React!
React was initially created by a Facebook engineer named Jordan Walke who was inspired by XHP, a PHP-based HTML component library. He wanted a better way to handle Facebook’s ever-growing dynamic features. Guess what? The first version of React was used in Facebook’s newsfeed, one of the most visited parts of the site!
1. 🚧 Setting Up the React Environment
The first step in moving my project to React was setting up the development environment. React has its own powerful ecosystem, and the easiest way to get started was by using Create React App.
Command Used:
npx create-react-app my-website
This command set up a new React project with all the necessary files and configurations like Babel and Webpack, which automatically handle bundling and transpiring the code. It made things super easy by providing me with a ready-to-use template that I could dive into! 🛠️
2. 🗂 Project Structure
React projects follow a specific structure, so I spent some time familiarizing myself with the new layout. Some key directories and files include:
public/
: Contains static files likeindex.html
, where React renders everything.src/
: Where all the magic happens! All components and main logic are here.App.js
: The main component that wraps around others.index.js
: Renders the app into the DOM using ReactDOM.
3. 🛠️ Converting My Header to a React Component
One of the first tasks was converting my static HTML header into a React component. React components are reusable pieces of UI, which makes them easier to manage.
Before (HTML):
<header> <nav> <ul> <li><a href="#about-me">About Me</a></li> <li><a href="#projects">Projects</a></li> <li><a href="#contact-me">Contact</a></li> <li><a href="#blog">Blog</a></li> </ul> </nav> </header>
After (React Component):
import React from 'react'; import { Link } from 'react-router-dom'; const Header = () => { return ( <nav> <ul> <li><Link to="/about-me">About Me</Link></li> <li><Link to="/projects">Projects</Link></li> <li><Link to="/contact-me">Contact</Link></li> <li><Link to="/blog">Blog</Link></li> </ul> </nav> ); }; export default Header;
It was fun seeing how JSX allows me to write HTML-like code in JavaScript. 🧠 It felt strange at first but quickly started to make sense as I got more comfortable with the flexibility it offers.
4. 🛤️ Adding React Router for Navigation
I realized that routing between different sections (like "About Me," "Projects") needed a better approach than simple <a>
tags. React Router handles navigation in single-page applications (SPAs), so I installed it and set it up! 🚪
Command Used:
npm install react-router-dom
I added routes to handle navigation between pages:
import React from 'react'; import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; import Header from './components/Header'; import AboutMe from './pages/AboutMe'; import Projects from './pages/Projects'; import ContactMe from './pages/ContactMe'; import Blog from './pages/Blog'; function App() { return ( <Router> <div> <Header /> <Routes> <Route path="/about-me" element={<AboutMe />} /> <Route path="/projects" element={<Projects />} /> <Route path="/contact-me" element={<ContactMe />} /> <Route path="/blog" element={<Blog />} /> </Routes> </div> </Router> ); } export default App;
Navigating between pages became seamless without full-page reloads! ⚡
5. 🎨 Styling with CSS in React
Next, I tackled styling. React components usually have their own CSS files, so I created a header.css
file for the header and imported it inside Header.js
.
import './header.css';
This modular styling keeps things clean and isolated, so no more global CSS messing with my entire site! 🙌
6. ✍️ Font Customization with Google Fonts
To make the site look even cooler, I integrated Fira Code as my font. Adding custom fonts in React is as easy as in basic HTML: just add the link to Google Fonts in the index.html
file and use it in my CSS files. 📄
7. 🚧 Challenges Faced
It wasn’t all smooth sailing, though. 😅 One challenge was understanding how React components interact with JSX. Getting React Router to work correctly and making sure the paths were set up took a bit of trial and error, but I finally figured it out. 💡
8. 🔮 Next Steps
Moving forward, my plan is to:
Convert the rest of my static HTML into React components.
Continue using React Router to handle navigation.
Explore React state management to make things interactive.
Conclusion 🎉
Day One was a success! Though there’s a learning curve transitioning from static HTML/CSS/JS, I can already see the benefits React offers. From its component-based structure to powerful routing, React is proving to be a game-changer. Excited for what’s next on Day Two! 🚀
Technologies I Worked With:
React ⚛️
React Router 🌐
JSX
CSS in React 🎨
Google Fonts (Fira Code)
Key Learnings:
The power of React’s component-based architecture.
How to handle routing using React Router.
How to integrate custom fonts and manage styles efficiently.
That’s a wrap for Day One! Stay tuned for more updates as I continue my React learning journey, one day at a time! 💻🎉