React Course: Diary App Example, Preparing for the Project

React is a JavaScript library for building user interfaces, helping developers create web applications more efficiently and intuitively. In this course, we will learn the basic concepts of React by creating a simple diary application, and we will detail the process of preparing for an actual project.

1. What is React?

React is a library that enables the development of declarative and component-based web applications. Developed by Facebook, it is now used by many companies and developers. The main advantages of React are as follows:

  • Reusable Components: React allows developers to break the UI into smaller units called components. These components can easily be reused in different places.
  • Virtual DOM: React uses a Virtual DOM to minimize actual DOM manipulations. This greatly enhances performance.
  • One-way Data Flow: Since data flows in one direction only, it makes predicting the state of data easier and code maintenance more manageable.

2. Overview of the Diary App Project

In this project, we will create a simple diary application that allows users to write, view, and delete diary entries. The basic functionalities are as follows:

  • View diary list
  • Write a new diary entry
  • Delete diary entries

To implement these functionalities, we will learn about React’s state management and the way data is passed between components.

3. Setting Up the Development Environment

Before starting the project, we need to set up the React development environment. The following tools need to be installed:

  • Node.js: A server-side JavaScript runtime used to run and manage React applications.
  • npm or yarn: JavaScript package managers used to install React-related libraries.
  • Code Editor: Choose an editor for coding tasks, such as VSCode or Atom.

If Node.js is installed, use the following command to create a project with create-react-app:

npx create-react-app diary-app

When the command is executed, a folder named diary-app will be created, and the basic React structure will be set up. Move to the created folder and run the development server:

cd diary-app
npm start

Opening http://localhost:3000 in a browser will show the default page.

4. Designing the Component Structure

The app’s component structure will be designed as follows:


    - App
        - DiaryList
        - DiaryForm

The App component is the top-level component that manages the state. The DiaryList component displays the list of diary entries, and the DiaryForm component provides a form for writing a new diary entry.

5. Managing State

In React components, state is a way to manage the data of a component. We will use the useState hook to manage diary data in the App component. For example, we can set the initial state to an empty array:


import React, { useState } from 'react';

const App = () => {
    const [diaries, setDiaries] = useState([]);
    return (
        <div>
            {/* Components */}
        </div>
    );
};

6. Implementing the Diary Entry Form

We will create the DiaryForm component to allow users to write diary entries. We will create an input tag to handle user input and manage the input value using the useState hook:


const DiaryForm = ({ onAdd }) =&gt; {
    const [text, setText] = useState('');

    const handleSubmit = (e) =&gt; {
        e.preventDefault();
        onAdd(text);
        setText('');
    };

    return (
        <form onsubmit="{handleSubmit}">
            <input onchange="{(e) => setText(e.target.value)}" type="text" value="{text}" placeholder="Write your diary">
            <button type="submit">Add</button>
        </form>
    );
};

7. Displaying the Diary List

We will create the DiaryList component to display the list of diary entries that have been added. The entries will be rendered using the map function:


const DiaryList = ({ diaries, onDelete }) =&gt; {
    return (
        <ul>
            {diaries.map((diary, index) =&gt; (
                <li key="{index}">
                    {diary}
                    <button onclick="{() => onDelete(index)}">Delete</button>
                </li>
            ))}
        </ul>
    );
};

8. Integrating into the App Component

Now we will integrate each component into the App component and connect the functionalities for adding and deleting diary entries:


const App = () =&gt; {
    const [diaries, setDiaries] = useState([]);

    const addDiary = (text) =&gt; {
        setDiaries([...diaries, text]);
    };

    const deleteDiary = (index) =&gt; {
        setDiaries(diaries.filter((_, i) =&gt; i !== index));
    };

    return (
        <div>
            <diaryform onadd="{addDiary}"></diaryform>
            <diarylist diaries="{diaries}" ondelete="{deleteDiary}"></diarylist>
        </div>
    );
};

9. CSS Styling

To make the diary app more visually appealing, we can add CSS styles. Create a src/App.css file to set up basic styles:


body {
    font-family: Arial, sans-serif;
}

form {
    margin-bottom: 20px;
}

input {
    margin-right: 10px;
    padding: 5px;
}

button {
    padding: 5px 10px;
}

Now when you refresh the app, you will see the diary app with basic styles applied.

10. Additional Considerations

This course only covers basic functionality, but you may consider adding the following features while actually working on the project:

  • Recording the date and time of diary entries
  • Adding editing functionality for diary entries
  • Implementing user authentication
  • Communicating with a server (REST API)

11. Project Deployment

Once the project is complete, you can deploy it to the web to use the app in a real environment. You can easily deploy using services like Vercel or Netlify. First, I will explain the process of building and deploying the app.

npm run build

When the command is executed, a build folder will be created, and the files inside this folder will be the ones actually deployed. You just need to upload these files to Vercel or Netlify for deployment.

Conclusion

In this course, we explored the process of developing a diary app using React. We looked at the basic component structure design, state management, styling, and deployment process. While working on real projects, you will be able to deepen your understanding of React by adding your own features. We hope you will continue to explore and develop more React functionalities!

References

React Course: Diary Application Example and Page Routing

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 (
        <router>
            <switch>
                <route component="{Home}" exact="" path="/"></route>
                <route component="{DiaryEntry}" path="/entry"></route>
                <route component="{DiaryDetail}" path="/diary/:id"></route>
            </switch>
        </router>
    );
}

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 (
        <div>
            <h2>Diary</h2>
            <link to="/entry">Write a new diary
            {/* Add the diary list here */}
        </div>
    );
}

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) =&gt; {
        e.preventDefault();
        // Logic to save the diary
        console.log({ title, content });
    };

    return (
        <div>
            <h2>Write a Diary</h2>
            <form onsubmit="{handleSubmit}">
                <input placeholder="Title" type="text" value="{title}" onchange="{(e)" ==""> setTitle(e.target.value)} required /&gt;
                <textarea placeholder="Content" value="{content}" onchange="{(e)" ==""> setContent(e.target.value)} required /&gt;
                &lt;button type="submit"&gt;Save&lt;/button&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    );
}

export default DiaryEntry;
    &lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;DiaryDetail Component&lt;/h3&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
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 (
        &lt;div&gt;
            &lt;h2&gt;Diary Detail (ID: {id})&lt;/h2&gt;
            {/* Output diary content here */}
        &lt;/div&gt;
    );
}

export default DiaryDetail;
    &lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;6. Saving and loading data&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h3&gt;Managing diary data&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;
import React, { useState } from 'react';

// Manage diary data in the App component.
function App() {
    const [diaries, setDiaries] = useState([]);
    
    const addDiary = (diary) =&gt; {
        setDiaries([...diaries, diary]);
    };

    return (
        &lt;Router&gt;
            &lt;DiaryContext.Provider value={{ diaries, addDiary }}&gt;
                &lt;Switch&gt;
                    &lt;Route component="{Home}" exact path="/"&gt;&lt;/Route&gt;
                    &lt;Route component="{DiaryEntry}" path="/entry"&gt;&lt;/Route&gt;
                    &lt;Route component="{DiaryDetail}" path="/diary/:id"&gt;&lt;/Route&gt;
                &lt;/Switch&gt;
            &lt;/DiaryContext.Provider&gt;
        &lt;/Router&gt;
    );
}
    &lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;7. Conclusion&lt;/h2&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2&gt;8. Additional study materials&lt;/h2&gt;
&lt;p&gt;To better understand React, please refer to the following materials:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://reactjs.org/docs/getting-started.html"&gt;Official React Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://reactrouter.com/web/guides/quick-start"&gt;React Router Guide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redux.js.org/introduction/getting-started"&gt;Official Redux Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;9. Questions and Answers&lt;/h2&gt;
&lt;p&gt;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!&lt;/p&gt;
&lt;p&gt;&lt;/body&gt;&lt;/p&gt;
	&lt;/div&gt;&lt;!-- .entry-content --&gt;

	&lt;footer class="entry-footer"&gt;
		&lt;span class="byline"&gt;&lt;span class="author vcard"&gt;&lt;img alt='' src='https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=49&amp;d=mm&amp;r=g' srcset='https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=98&amp;d=mm&amp;r=g 2x' class='avatar avatar-49 photo' height='49' width='49' decoding='async'/&gt;&lt;span class="screen-reader-text"&gt;Author &lt;/span&gt; &lt;a class="url fn n" href="https://atmokpo.com/w/en/author/root/"&gt;root&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="posted-on"&gt;&lt;span class="screen-reader-text"&gt;Posted on &lt;/span&gt;&lt;a href="https://atmokpo.com/w/32929/" rel="bookmark"&gt;&lt;time class="entry-date published" datetime="2024-11-01T09:12:32+00:00"&gt;2024/11/01&lt;/time&gt;&lt;time class="updated" datetime="2024-11-01T11:21:26+00:00"&gt;2024/11/01&lt;/time&gt;&lt;/a&gt;&lt;/span&gt;&lt;span class="cat-links"&gt;&lt;span class="screen-reader-text"&gt;Categories &lt;/span&gt;&lt;a href="https://atmokpo.com/w/category/react-basics-course/" rel="category tag"&gt;React basics course&lt;/a&gt;&lt;/span&gt;			&lt;/footer&gt;&lt;!-- .entry-footer --&gt;
&lt;/article&gt;&lt;!-- #post-32929 --&gt;

&lt;article id="post-32927" class="post-32927 post type-post status-publish format-standard hentry category-react-basics-course"&gt;
	&lt;header class="entry-header"&gt;
		
		&lt;h2 class="entry-title"&gt;&lt;a href="https://atmokpo.com/w/32927/" rel="bookmark"&gt;React Course: Diary App Example and Optimization&lt;/a&gt;&lt;/h2&gt;	&lt;/header&gt;&lt;!-- .entry-header --&gt;

	
	
	&lt;div class="entry-content"&gt;
		&lt;p&gt;In recent years, React has established itself as one of the most popular libraries for web application development. With React, you can create fast, efficient, and reusable UI components. In this tutorial, we will examine how to create a simple diary app using React. Additionally, we will discuss performance optimization.&lt;/p&gt;
&lt;h2&gt;1. Project Setup&lt;/h2&gt;
&lt;p&gt;Before getting started, you need to have Node.js and npm (Node Package Manager) installed. These tools are essential for creating and managing React projects. Once the installation is complete, you can create a new React project using the following command.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npx create-react-app diary-app&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command creates a basic React project template. After moving into the project folder, you can install the required libraries.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;cd diary-app&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we will install the libraries needed for state management and styling in the diary app.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npm install axios bootstrap&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;2. Designing the Basic Structure&lt;/h2&gt;
&lt;p&gt;Now, let’s design the basic project structure. The diary app is divided into the following main components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;DiaryList&lt;/strong&gt;: A component that displays the list of diary entries.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;DiaryEntry&lt;/strong&gt;: A component for creating or editing a new diary entry.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Header&lt;/strong&gt;: A header component that includes the app title and navigation links.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These components will be created as individual files within the &lt;code&gt;src&lt;/code&gt; folder.&lt;/p&gt;
&lt;h2&gt;3. Implementing the DiaryList Component&lt;/h2&gt;
&lt;p&gt;Create a &lt;strong&gt;DiaryList.js&lt;/strong&gt; file and add the following code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import React from 'react';

const DiaryList = ({ diaries }) =&gt; {
    return (
        &lt;div className="diary-list"&gt;
            &lt;h2&gt;My Diary List&lt;/h2&gt;
            {diaries.length === 0 ? (
                &lt;p&gt;No diaries available.&lt;/p&gt;
            ) : (
                diaries.map(diary =&gt; (
                    &lt;div key="{diary.id}"&gt;
                        &lt;h3&gt;{diary.title}&lt;/h3&gt;
                        &lt;p&gt;{diary.content}&lt;/p&gt;
                        &lt;hr/&gt;
                    &lt;/div&gt;
                ))
            )}
        &lt;/div&gt;
    );
};

export default DiaryList;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This component displays the title and content of each diary entry through the &lt;code&gt;diaries&lt;/code&gt; array received as a prop. If there are no diaries, it outputs an appropriate message.&lt;/p&gt;
&lt;h2&gt;4. Implementing the DiaryEntry Component&lt;/h2&gt;
&lt;p&gt;Now, create a &lt;strong&gt;DiaryEntry.js&lt;/strong&gt; file and implement a form that allows the user to write a diary entry.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import React, { useState } from 'react';

const DiaryEntry = ({ onSubmit }) =&gt; {
    const [title, setTitle] = useState('');
    const [content, setContent] = useState('');

    const handleSubmit = (e) =&gt; {
        e.preventDefault();
        onSubmit({ title, content });
        setTitle('');
        setContent('');
    };

    return (
        &lt;form onSubmit="{handleSubmit}"&gt;
            &lt;input onChange="{(e) =&gt; setTitle(e.target.value)}" placeholder="Title" type="text" value="{title}" required /&gt;
            &lt;textarea onChange="{(e) =&gt; setContent(e.target.value)}" placeholder="Content" value="{content}" required /&gt;
            &lt;button type="submit"&gt;Save&lt;/button&gt;
        &lt;/form&gt;
    );
};

export default DiaryEntry;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This component uses the &lt;code&gt;useState&lt;/code&gt; hook to manage the title and content, and it calls the function passed via the &lt;code&gt;onSubmit&lt;/code&gt; prop when the user submits the form to save the diary entry.&lt;/p&gt;
&lt;h2&gt;5. Implementing the Header Component&lt;/h2&gt;
&lt;p&gt;Create a &lt;strong&gt;Header.js&lt;/strong&gt; file and implement the app’s header.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import React from 'react';

const Header = () =&gt; {
    return (
        &lt;header&gt;
            &lt;h1&gt;My Diary&lt;/h1&gt;
        &lt;/header&gt;
    );
};

export default Header;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;6. Configuring the App Component&lt;/h2&gt;
&lt;p&gt;Now, we will modify the &lt;strong&gt;App.js&lt;/strong&gt; file to integrate the components we created earlier.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import React, { useState } from 'react';
import Header from './Header';
import DiaryList from './DiaryList';
import DiaryEntry from './DiaryEntry';
import './App.css';

function App() {
    const [diaries, setDiaries] = useState([]);

    const addDiary = (diary) =&gt; {
        setDiaries([...diaries, { id: diaries.length + 1, ...diary }]);
    };

    return (
        &lt;div className="App"&gt;
            &lt;Header /&gt;
            &lt;DiaryEntry onSubmit="{addDiary}" /&gt;
            &lt;DiaryList diaries="{diaries}" /&gt;
        &lt;/div&gt;
    );
}

export default App;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, the basic implementation of the diary app is complete. Users can enter a title and content to create diary entries and view the entries in a list.&lt;/p&gt;
&lt;h2&gt;7. Optimization&lt;/h2&gt;
&lt;p&gt;When building a React application, performance optimization is also important. Here are some optimization techniques:&lt;/p&gt;
&lt;h3&gt;7.1. React.memo&lt;/h3&gt;
&lt;p&gt;React components are re-rendered by default when props change. However, by using &lt;code&gt;React.memo&lt;/code&gt;, you can prevent the component from re-rendering unless the props change.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const DiaryList = React.memo(({ diaries }) =&gt; {
    // Component content
});&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;7.2. useCallback and useMemo&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;useCallback&lt;/code&gt; hook memoizes functions so that the same function is returned unless the dependency array changes. &lt;code&gt;useMemo&lt;/code&gt; memoizes computed values. This can help prevent unnecessary re-renders.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const addDiary = useCallback((diary) =&gt; {
    setDiaries((prevDiaries) =&gt; [...prevDiaries, { id: prevDiaries.length + 1, ...diary }]);
}, []);&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;7.3. Lazy Loading&lt;/h3&gt;
&lt;p&gt;In React, you can use Lazy Loading through code splitting. This allows you to load components only when needed, improving initial load speed. You can utilize React’s &lt;code&gt;React.lazy&lt;/code&gt; and &lt;code&gt;Suspense&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;const DiaryList = React.lazy(() =&gt; import('./DiaryList'));

return (
    &lt;Suspense fallback="{&lt;div&gt;Loading...&lt;/div&gt;}"&gt;
        &lt;DiaryList diaries="{diaries}" /&gt;
    &lt;/Suspense&gt;
);&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;8. Conclusion&lt;/h2&gt;
&lt;p&gt;In this tutorial, we created a basic diary app using React. We also discussed performance optimization techniques to explore more efficient ways of application development. When applying this code and optimization techniques to real projects, refer to them to create better-performing apps.&lt;/p&gt;
&lt;p&gt;React is an easy-to-learn and powerful tool. I hope you gain a deeper understanding by creating various applications. Happy Coding!&lt;/p&gt;
	&lt;/div&gt;&lt;!-- .entry-content --&gt;

	&lt;footer class="entry-footer"&gt;
		&lt;span class="byline"&gt;&lt;span class="author vcard"&gt;&lt;img alt='' src='https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=49&amp;d=mm&amp;r=g' srcset='https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=98&amp;d=mm&amp;r=g 2x' class='avatar avatar-49 photo' height='49' width='49' decoding='async'/&gt;&lt;span class="screen-reader-text"&gt;Author &lt;/span&gt; &lt;a class="url fn n" href="https://atmokpo.com/w/en/author/root/"&gt;root&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="posted-on"&gt;&lt;span class="screen-reader-text"&gt;Posted on &lt;/span&gt;&lt;a href="https://atmokpo.com/w/32927/" rel="bookmark"&gt;&lt;time class="entry-date published" datetime="2024-11-01T09:12:32+00:00"&gt;2024/11/01&lt;/time&gt;&lt;time class="updated" datetime="2024-11-01T11:21:26+00:00"&gt;2024/11/01&lt;/time&gt;&lt;/a&gt;&lt;/span&gt;&lt;span class="cat-links"&gt;&lt;span class="screen-reader-text"&gt;Categories &lt;/span&gt;&lt;a href="https://atmokpo.com/w/category/react-basics-course/" rel="category tag"&gt;React basics course&lt;/a&gt;&lt;/span&gt;			&lt;/footer&gt;&lt;!-- .entry-footer --&gt;
&lt;/article&gt;&lt;!-- #post-32927 --&gt;

&lt;article id="post-32925" class="post-32925 post type-post status-publish format-standard hentry category-react-basics-course"&gt;
	&lt;header class="entry-header"&gt;
		
		&lt;h2 class="entry-title"&gt;&lt;a href="https://atmokpo.com/w/32925/" rel="bookmark"&gt;React Course: Diary App Example&lt;/a&gt;&lt;/h2&gt;	&lt;/header&gt;&lt;!-- .entry-header --&gt;

	
	
	&lt;div class="entry-content"&gt;
		&lt;p&gt;&lt;body&gt;&lt;/p&gt;
&lt;p&gt;In this course, we will learn in detail how to develop a diary app using React.&lt;/p&gt;
&lt;h2&gt;What is React?&lt;/h2&gt;
&lt;p&gt;React is an open-source JavaScript library developed by Facebook for building user interfaces (UIs). React enables reusable UI components through a component-based architecture. It is primarily used in single-page applications (SPAs) and helps manage state and data flow efficiently.&lt;/p&gt;
&lt;h2&gt;Overview of the Diary App&lt;/h2&gt;
&lt;p&gt;The diary app is a simple application that allows users to record and manage their diaries. This app provides functionality for users to add new diary entries, edit or delete existing ones. Additionally, users can search or sort diaries by date.&lt;/p&gt;
&lt;h2&gt;Project Setup&lt;/h2&gt;
&lt;p&gt;To start a React app, Node.js and npm must be installed. Create a new React project using the command below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npx create-react-app diary-app&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Project Structure&lt;/h3&gt;
&lt;p&gt;The folder structure of the project is as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;diary-app/
&lt;ul&gt;
&lt;li&gt;public/
&lt;ul&gt;
&lt;li&gt;index.html&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;src/
&lt;ul&gt;
&lt;li&gt;components/
&lt;ul&gt;
&lt;li&gt;DiaryEntry.js&lt;/li&gt;
&lt;li&gt;DiaryList.js&lt;/li&gt;
&lt;li&gt;DiaryForm.js&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;App.js&lt;/li&gt;
&lt;li&gt;index.js&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Main Feature Implementation&lt;/h2&gt;
&lt;h3&gt;1. Diary Adding Functionality&lt;/h3&gt;
&lt;p&gt;To implement the functionality for adding a diary, we will use the &lt;code&gt;DiaryForm&lt;/code&gt; component. This component takes diary content input from the user, updates the state, and passes it to the parent component.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
import React, { useState } from 'react';

const DiaryForm = ({ addDiary }) =&gt; {
    const [content, setContent] = useState('');

    const handleSubmit = (e) =&gt; {
        e.preventDefault();
        if (!content) return;
        addDiary(content);
        setContent('');
    };

    return (
        &lt;form onsubmit="{handleSubmit}"&gt;
            &lt;textarea onchange="{(e) =&gt; setContent(e.target.value)}"
                placeholder="Enter your diary here."
            /&gt;
            &lt;button type="submit"&gt;Add&lt;/button&gt;
        &lt;/form&gt;
    );
};

export default DiaryForm;
    &lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;2. Diary List Display Functionality&lt;/h3&gt;
&lt;p&gt;To display the diaries added by the user, we will implement the &lt;code&gt;DiaryList&lt;/code&gt; component. This component maps the diary data received from the parent component and renders it on the screen.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
import React from 'react';
import DiaryEntry from './DiaryEntry';

const DiaryList = ({ diaries }) =&gt; {
    return (
        &lt;div&gt;
            {diaries.map((diary, index) =&gt; (
                &lt;DiaryEntry diary="{diary}" key="{index}"&gt;&lt;/DiaryEntry&gt;
            ))}
        &lt;/div&gt;
    );
};

export default DiaryList;
    &lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;3. Diary Deleting Functionality&lt;/h3&gt;
&lt;p&gt;To implement the functionality for deleting a diary, we will add the &lt;code&gt;DiaryEntry&lt;/code&gt; component. This component renders individual diaries and provides a delete button.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
import React from 'react';

const DiaryEntry = ({ diary, deleteDiary }) =&gt; {
    return (
        &lt;div&gt;
            &lt;p&gt;{diary}&lt;/p&gt;
            &lt;button onclick="{deleteDiary}"&gt;Delete&lt;/button&gt;
        &lt;/div&gt;
    );
};

export default DiaryEntry;
    &lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;State Management&lt;/h2&gt;
&lt;p&gt;For state management, we will use React’s &lt;code&gt;useState&lt;/code&gt; hook. This allows components to re-render with the latest state whenever the data changes. Below is a simple example of &lt;code&gt;App.js&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
import React, { useState } from 'react';
import DiaryForm from './components/DiaryForm';
import DiaryList from './components/DiaryList';

const App = () =&gt; {
    const [diaries, setDiaries] = useState([]);

    const addDiary = (content) =&gt; {
        setDiaries([...diaries, content]);
    };

    const deleteDiary = (index) =&gt; {
        const newDiaries = diaries.filter((_, i) =&gt; i !== index);
        setDiaries(newDiaries);
    };

    return (
        &lt;div&gt;
            &lt;h1&gt;My Diary&lt;/h1&gt;
            &lt;DiaryForm addDiary="{addDiary}"&gt;&lt;/DiaryForm&gt;
            &lt;DiaryList deleteDiary="{deleteDiary}" diaries="{diaries}"&gt;&lt;/DiaryList&gt;
        &lt;/div&gt;
    );
};

export default App;
    &lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In this course, we learned how to create a simple diary app using React. This app provides the basic functionalities needed for users to write and manage their diaries, leveraging React’s component-based architecture. Based on this example, more functionalities can be added to expand your personal diary app.&lt;/p&gt;
&lt;h2&gt;Next Steps&lt;/h2&gt;
&lt;p&gt;You can enhance the diary app by adding the following features:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Diary search function&lt;/li&gt;
&lt;li&gt;Diary editing function&lt;/li&gt;
&lt;li&gt;Diary sorting by date function&lt;/li&gt;
&lt;li&gt;Building a backend to save diary data&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Challenge yourself with more diverse projects using React!&lt;/p&gt;
&lt;p&gt;&lt;/body&gt;&lt;/p&gt;
	&lt;/div&gt;&lt;!-- .entry-content --&gt;

	&lt;footer class="entry-footer"&gt;
		&lt;span class="byline"&gt;&lt;span class="author vcard"&gt;&lt;img alt='' src='https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=49&amp;d=mm&amp;r=g' srcset='https://secure.gravatar.com/avatar/5b7f47db621d1eab02540d35048be506?s=98&amp;d=mm&amp;r=g 2x' class='avatar avatar-49 photo' height='49' width='49' loading='lazy' decoding='async'/&gt;&lt;span class="screen-reader-text"&gt;Author &lt;/span&gt; &lt;a class="url fn n" href="https://atmokpo.com/w/en/author/root/"&gt;root&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="posted-on"&gt;&lt;span class="screen-reader-text"&gt;Posted on &lt;/span&gt;&lt;a href="https://atmokpo.com/w/32925/" rel="bookmark"&gt;&lt;time class="entry-date published" datetime="2024-11-01T09:12:31+00:00"&gt;2024/11/01&lt;/time&gt;&lt;time class="updated" datetime="2024-11-01T11:21:27+00:00"&gt;2024/11/01&lt;/time&gt;&lt;/a&gt;&lt;/span&gt;&lt;span class="cat-links"&gt;&lt;span class="screen-reader-text"&gt;Categories &lt;/span&gt;&lt;a href="https://atmokpo.com/w/category/react-basics-course/" rel="category tag"&gt;React basics course&lt;/a&gt;&lt;/span&gt;			&lt;/footer&gt;&lt;!-- .entry-footer --&gt;
&lt;/article&gt;&lt;!-- #post-32925 --&gt;

&lt;article id="post-32923" class="post-32923 post type-post status-publish format-standard hentry category-react-basics-course"&gt;
	&lt;header class="entry-header"&gt;
		
		&lt;h2 class="entry-title"&gt;&lt;a href="https://atmokpo.com/w/32923/" rel="bookmark"&gt;React Course: Diary App Example, Page Routing with React Router&lt;/a&gt;&lt;/h2&gt;	&lt;/header&gt;&lt;!-- .entry-header --&gt;

	
	
	&lt;div class="entry-content"&gt;
		&lt;p&gt;&lt;body&gt;&lt;/p&gt;
&lt;p&gt;In this course, we will explain step by step how to create a simple diary web application using React. This application allows users to write, edit, and delete diaries. Additionally, we will cover how to implement routing to various pages using React Router. This course will be explained in detail so that beginners can easily follow along.&lt;/p&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#intro"&gt;1. Project Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#setup"&gt;2. Setting Up the Development Environment&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#create-app"&gt;3. Creating a React App&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#add-router"&gt;4. Adding React Router&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#build-components"&gt;5. Building Components&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#state-management"&gt;6. State Management&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#test"&gt;7. Testing and Debugging&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#deploy"&gt;8. Deploying&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#conclusion"&gt;9. Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="intro"&gt;1. Project Introduction&lt;/h2&gt;
&lt;p&gt;The diary app provides users with a space to write personal diaries. Users can create new diaries, view previously written diaries in a list, and edit or delete them if necessary. In this process, we will learn the basic concepts of React and how to use React Router.&lt;/p&gt;
&lt;h2 id="setup"&gt;2. Setting Up the Development Environment&lt;/h2&gt;
&lt;p&gt;To use React, you must first install Node.js and npm (Node Package Manager). Once you have installed Node.js and npm, you can easily create and manage React applications. Please download and install it from the official website.&lt;/p&gt;
&lt;h3&gt;Installing Node.js&lt;/h3&gt;
&lt;p&gt;Download and install the installation file suitable for your operating system from the &lt;a href="https://nodejs.org/"&gt;official Node.js website&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Updating the Package Manager&lt;/h3&gt;
&lt;p&gt;After installation, check if npm is up to date and update it if necessary. Enter the following command in the console:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npm install -g npm@latest&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="create-app"&gt;3. Creating a React App&lt;/h2&gt;
&lt;p&gt;Now let’s create a new React project using create-react-app. Enter the following command in the console:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npx create-react-app my-diary-app&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Navigate to the project folder:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;cd my-diary-app&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then, start the development server:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npm start&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Navigate to &lt;strong&gt;http://localhost:3000&lt;/strong&gt; in your browser to check if the default React page is displayed.&lt;/p&gt;
&lt;h2 id="add-router"&gt;4. Adding React Router&lt;/h2&gt;
&lt;p&gt;Now, let’s install React Router to add routing functionality to the application. Enter the following command to install React Router:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;npm install react-router-dom&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Open the app/src/index.js file and set up the router:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import { BrowserRouter as Router } from 'react-router-dom';&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wrap the app with the Router:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
    ReactDOM.render(
      &lt;Router&gt;
        &lt;App&gt;&lt;/App&gt;
      &lt;/Router&gt;,
      document.getElementById('root')
    );&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="build-components"&gt;5. Building Components&lt;/h2&gt;
&lt;p&gt;Now, let’s create components to set up the pages for routing. We will create a diary list page and a diary creation page.&lt;/p&gt;
&lt;h3&gt;Diary List Component&lt;/h3&gt;
&lt;p&gt;Create app/src/components/DiaryList.js and enter the following code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
    import React from 'react';

    const DiaryList = () =&gt; {
        return (
            &lt;div&gt;
                &lt;h2&gt;Diary List&lt;/h2&gt;
                &lt;ul&gt;
                    &lt;li&gt;Title: Day One, Content: I felt good today.&lt;/li&gt;
                    &lt;li&gt;Title: Day Two, Content: The weather was nice.&lt;/li&gt;
                    {/* You can add more diaries */}
                &lt;/ul&gt;
            &lt;/div&gt;
        );
    };

    export default DiaryList;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Diary Creation Component&lt;/h3&gt;
&lt;p&gt;Create app/src/components/CreateDiary.js and enter the following code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
    import React from 'react';

    const CreateDiary = () =&gt; {
        return (
            &lt;div&gt;
                &lt;h2&gt;Create Diary&lt;/h2&gt;
                &lt;form&gt;
                    &lt;label&gt;Title: &lt;/label&gt;
                    &lt;input type="text"/&gt;
                    &lt;label&gt;Content: &lt;/label&gt;
                    &lt;textarea&gt;</textarea>
                    <button type="submit">Save</button>
                </form>
            </div>
        );
    };

    export default CreateDiary;

6. State Management

To manage the diaries written in the diary app, you need to use React’s useState hook. Open the App.js file and set up the state.


    import React, { useState } from 'react';
    import DiaryList from './components/DiaryList';
    import CreateDiary from './components/CreateDiary';

    const App = () =&gt; {
        const [diaries, setDiaries] = useState([]);

        // Add diary entry functions and more example code here
            
        return (
            <div>
                <h1>My Diary</h1>
                {/* Add routing settings */}
            </div>
        );
    };

    export default App;

7. Testing and Debugging

Test each component and functionality of the application to ensure they work properly. If the desired results do not appear, use console.log for debugging. For instance, check if the state updates as expected.

8. Deploying

To deploy, you first need to build the app. You can do this by entering the following command in the console:

npm run build

You can then deploy the generated build folder to a server. Various deployment services such as Firebase, Vercel, and Netlify can be used.

9. Conclusion

In this course, we learned how to create a simple diary application using React and how to implement routing to multiple pages using React Router. Through practical exercises, we were able to grasp the basic principles of React, state management, and routing. We hope you gain more experience by implementing additional features and deploying your application.

Thank you!