Hello! In this lesson, we will create a simple diary application using React. This application will implement features that allow users to write, save, and read their diaries. We will also learn how to structure various pages through page routing.
1. What is React?
React is a JavaScript library for building web applications. It focuses on building user interfaces and is designed to be component-based, making it highly reusable. React uses a virtual DOM to optimize performance and helps developers create dynamic web applications more easily.
2. Setting up the project environment
The first step in creating the diary application is to set up the development environment. Let’s use create-react-app
to generate a basic template.
npx create-react-app diary-app
cd diary-app
npm start
By executing the above commands, a React project will be created and a basic development server will start. Now, let’s install the necessary packages for the basic project.
npm install react-router-dom
3. Designing the component structure
Now, let’s design the basic structure of the application. The diary application can consist of the following components:
- Home: The main page that displays a list of previous diaries.
- DiaryEntry: The page where users can write a diary.
- DiaryDetail: The page that shows the details of the selected diary.
4. Implementing page routing
To create an application composed of multiple pages using React, we will implement routing using react-router-dom
. First, let’s set up the router.
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './components/Home';
import DiaryEntry from './components/DiaryEntry';
import DiaryDetail from './components/DiaryDetail';
function App() {
return (
);
}
export default App;
5. Implementing components
Home Component
The Home component is the page where users can view their previous diaries. It provides a link to write a new diary along with the diary list.
import React from 'react';
import { Link } from 'react-router-dom';
function Home() {
return (
Diary
Write a new diary
{/* Add the diary list here */}
);
}
export default Home;
DiaryEntry Component
The DiaryEntry component is the page where a user writes a new diary. Users can input a title and content, and when they click the save button, the diary is saved.
import React, { useState } from 'react';
function DiaryEntry() {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
// Logic to save the diary
console.log({ title, content });
};
return (
Write a Diary
);
}
export default DiaryEntry;
DiaryDetail Component
The DiaryDetail component is the page that shows the detailed content of the selected diary. It displays the diary’s title, content, and other metadata.
import React from 'react';
import { useParams } from 'react-router-dom';
function DiaryDetail() {
const { id } = useParams();
// Logic to fetch diary data corresponding to the id is needed here.
return (
Diary Detail (ID: {id})
{/* Output diary content here */}
);
}
export default DiaryDetail;
6. Saving and loading data
To save the diary written by the user, state management is needed. In this example, we will implement data saving simply using React’s state. For more complex applications, it is advisable to use Redux or Context API for state management.
Managing diary data
import React, { useState } from 'react';
// Manage diary data in the App component.
function App() {
const [diaries, setDiaries] = useState([]);
const addDiary = (diary) => {
setDiaries([...diaries, diary]);
};
return (
);
}
7. Conclusion
In this lesson, we explored how to create a diary application using React and implement page routing. With React’s component structure and data management, it is easy to add various features and facilitate smoother interactions with users.
Based on the example above, you can add more diverse functions. For example, you can add features to edit or delete diaries or introduce user authentication to expand it into a personal diary.
8. Additional study materials
To better understand React, please refer to the following materials:
9. Questions and Answers
If you have any questions about this lesson, please leave a comment. I hope this can be a place for us to think and grow together!