-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfrontend_src_components_App.js.html
191 lines (161 loc) · 4.77 KB
/
frontend_src_components_App.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: frontend/src/components/App.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: frontend/src/components/App.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/*import React, { useState, useEffect } from "react";
const App = () => {
const [notes, setNotes] = useState([]);
const [newNote, setNewNote] = useState("");
useEffect(() => {
fetch("/api/notes", { method: "GET" })
.then((res) => res.json())
.then((data) => setNotes(data.notes))
.catch((err) => console.error(err));
}, []);
const handleAddNote = () => {
fetch("/api/notes", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: newNote }),
})
.then((res) => res.json())
.then((data) => {
console.log(data.message);
setNewNote("");
// Refresh notes after adding a new one
fetch("/api/notes")
.then((res) => res.json())
.then((data) => setNotes(data.notes))
.catch((err) => console.error(err));
})
.catch((err) => console.error(err));
};
return (
<div>
<h1>Express and React Example</h1>
<ul>
{notes.map((note) => (
<li key={note._id}>{note.content}</li>
))}
</ul>
<input
type="text"
placeholder="Add a new note"
value={newNote}
onChange={(e) => setNewNote(e.target.value)}
/>
<button onClick={handleAddNote}>Add Note</button>
</div>
);
};
export default App;
*/
import React, { useState, useEffect } from "react";
/**
* Functional component representing the main application.
* @function
* @component
*/
const App = () => {
/**
* State to store the array of notes.
* @type {Array<Object>}
*/
const [notes, setNotes] = useState([]);
/**
* State to store the content of a new note.
* @type {string}
*/
const [newNote, setNewNote] = useState("");
/**
* Effect hook to fetch notes from the server on component mount.
* @function
* @name useEffect
* @param {Function} effect - The function to execute.
* @param {Array} dependencies - The dependencies to watch for changes.
*/
useEffect(() => {
fetch("/api/notes", { method: "GET" })
.then((res) => res.json())
.then((data) => setNotes(data.notes))
.catch((err) => console.error(err));
}, []);
/**
* Handler function to add a new note.
* @function
* @name handleAddNote
*/
const handleAddNote = () => {
fetch("/api/notes", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: newNote }),
})
.then((res) => res.json())
.then((data) => {
console.log(data.message);
setNewNote("");
// Refresh notes after adding a new one
fetch("/api/notes")
.then((res) => res.json())
.then((data) => setNotes(data.notes))
.catch((err) => console.error(err));
})
.catch((err) => console.error(err));
};
/**
* Render function.
* @returns {JSX.Element} The JSX representation of the component.
*/
return (
<div>
<h1>Express and React Example</h1>
<ul>
{notes.map((note) => (
<li key={note._id}>{note.content}</li>
))}
</ul>
<input
type="text"
placeholder="Add a new note"
value={newNote}
onChange={(e) => setNewNote(e.target.value)}
/>
<button onClick={handleAddNote}>Add Note</button>
</div>
);
};
export default App;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Global</h3><ul><li><a href="global.html#App">App</a></li><li><a href="global.html#handleAddNote">handleAddNote</a></li><li><a href="global.html#renderApp">renderApp</a></li><li><a href="global.html#useEffect">useEffect</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.2</a> on Mon Jan 01 2024 21:06:33 GMT+0530 (India Standard Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>