Building a Dynamic Web Application: A Comprehensive Guide to Using React, Node.js, and Modern Tools - Day 2
Building a Dynamic Web Application: A Comprehensive Guide to Using React, Node.js, and Modern Tools - Day 1
Day 2: Project Structure and Basic Layout
Task 1: Organize the Project Structure
Objective: Create a clean and organized folder structure for your React project.
Steps:
- Create the Following Folders in the
src Directory:components/: For reusable UI components.pages/: For different pages/views of your application.assets/: For images, icons, and other static assets.styles/: For global styles and CSS files.utils/: For utility functions, constants, and helpers.
Here's a sample structure:
src/
├── assets/
├── components/
├── pages/
├── styles/
├── utils/
├── App.tsx
├── index.tsx
└── ...
Objective: Create a clean and organized folder structure for your React project.
Steps:
- Create the Following Folders in the
srcDirectory:components/: For reusable UI components.pages/: For different pages/views of your application.assets/: For images, icons, and other static assets.styles/: For global styles and CSS files.utils/: For utility functions, constants, and helpers.
Here's a sample structure:
src/ ├── assets/ ├── components/ ├── pages/ ├── styles/ ├── utils/ ├── App.tsx ├── index.tsx └── ...
Task 2: Set Up Basic Routing
- Objective: Implement routing to manage different pages in your app.
- Steps:
Install React Router:
- Run the following command to install React Router:
yarn add react-router-dom yarn add @types/react-router-dom
Update App.tsx:
- Import
BrowserRouter, Routes, and Route from react-router-dom. - Create basic routes for your application (e.g., Home, About, and Contact pages).
Example App.tsx:
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Router>
);
}
export default App;
Create Placeholder Pages:
- In the
pages/ directory, create simple placeholder components for Home, About, and Contact pages.
Example Home.tsx:
import React from 'react';
const Home: React.FC = () => {
return <h1>Home Page</h1>;
};
export default Home;
Repeat similar steps for About.tsx and Contact.tsx.
Install React Router:
- Run the following command to install React Router:
yarn add react-router-dom yarn add @types/react-router-dom
- Run the following command to install React Router:
Update
App.tsx:- Import
BrowserRouter,Routes, andRoutefromreact-router-dom. - Create basic routes for your application (e.g., Home, About, and Contact pages).
- Import
Example
App.tsx:import React from 'react'; import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'; import Home from './pages/Home'; import About from './pages/About'; import Contact from './pages/Contact'; function App() { return ( <Router> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="/contact" element={<Contact />} /> </Routes> </Router> ); } export default App;Create Placeholder Pages:
- In the
pages/directory, create simple placeholder components forHome,About, andContactpages.
Example
Home.tsx:import React from 'react'; const Home: React.FC = () => { return <h1>Home Page</h1>; }; export default Home;Repeat similar steps for
About.tsxandContact.tsx.- In the
Task 3: Implement Global Styles
- Objective: Set up global styles and a basic CSS reset.
- Steps:
- Install
styled-components or Sass (if you prefer):- For styled-components:
yarn add styled-components yarn add @types/styled-components - For Sass:
yarn add sass
- Create a
GlobalStyle Component (if using styled-components):- In the
styles/ directory, create a GlobalStyle.ts file and define global styles.
- Example:
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
body {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
`;
export default GlobalStyle;
- Then, import and use
GlobalStyle in App.tsx:
import GlobalStyle from './styles/GlobalStyle';
function App() {
return (
<>
<GlobalStyle />
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Router>
</>
);
}
- Add Basic CSS/Sass Reset (if using CSS/Sass):
- Create a
reset.scss or reset.css file in the styles/ directory to include a CSS reset.
- Install
styled-componentsorSass(if you prefer):- For styled-components:
yarn add styled-components yarn add @types/styled-components - For Sass:
yarn add sass
- For styled-components:
- Create a
GlobalStyleComponent (if using styled-components):- In the
styles/directory, create aGlobalStyle.tsfile and define global styles.
- In the
- Example:
import { createGlobalStyle } from 'styled-components'; const GlobalStyle = createGlobalStyle` body { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Arial', sans-serif; } `; export default GlobalStyle;- Then, import and use
GlobalStyleinApp.tsx:import GlobalStyle from './styles/GlobalStyle'; function App() { return ( <> <GlobalStyle /> <Router> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="/contact" element={<Contact />} /> </Routes> </Router> </> ); }
- Then, import and use
- Add Basic CSS/Sass Reset (if using CSS/Sass):
- Create a
reset.scssorreset.cssfile in thestyles/directory to include a CSS reset.
- Create a
Task 4: Set Up a Navigation Bar
- Objective: Implement a basic navigation bar for easy page access.
- Steps:
Create a NavBar Component in the components/ directory:
- Example
NavBar.tsx:import React from 'react';
import { Link } from 'react-router-dom';
const NavBar: React.FC = () => {
return (
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/about">About</Link></li>
<li><Link to="/contact">Contact</Link></li>
</ul>
</nav>
);
};
export default NavBar; - Include the
NavBar Component in App.tsx:
- Add the
NavBar component above the Routes in your App.tsx:
import NavBar from './components/NavBar';
function App() {
return (
<>
<GlobalStyle />
<Router>
<NavBar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</Router>
</>
);
}
Create a
NavBarComponent in thecomponents/directory:- Example
NavBar.tsx:import React from 'react'; import { Link } from 'react-router-dom'; const NavBar: React.FC = () => { return ( <nav> <ul> <li><Link to="/">Home</Link></li> <li><Link to="/about">About</Link></li> <li><Link to="/contact">Contact</Link></li> </ul> </nav> ); }; export default NavBar; - Include the
NavBarComponent inApp.tsx: - Add the
NavBarcomponent above theRoutesin yourApp.tsx: import NavBar from './components/NavBar'; function App() { return ( <> <GlobalStyle /> <Router> <NavBar /> <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> <Route path="/contact" element={<Contact />} /> </Routes> </Router> </> ); }
Day 2 Summary
Today, you’ve:
- Organized your project structure with essential folders.
- Set up basic routing using React Router.
- Implemented global styles and a basic layout.
- Created placeholder pages for navigation.
- Built a simple navigation bar.
These steps provide a solid foundation for your app. Tomorrow, we'll focus on integrating a CMS to manage content dynamically.
Let me know when you're ready for Day 3!
Join the conversation