You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A slightly odd looking, but commonly used [React trick](https://react.dev/learn/conditional-rendering#logical-and-operator-) is used to render the forms conditionally:
255
250
256
251
```js
257
-
{
258
-
user ===null&&loginForm()
259
-
}
252
+
{!user &&loginForm()}
260
253
```
261
254
262
255
If the first statement evaluates to false or is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), the second statement (generating the form) is not executed at all.
263
256
264
-
We can make this even more straightforward by using the [conditional operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator):
265
-
266
-
```js
267
-
return (
268
-
<div>
269
-
<h1>Notes</h1>
270
-
271
-
<Notification message={errorMessage}/>
272
-
273
-
{user ===null?
274
-
loginForm() :
275
-
noteForm()
276
-
}
277
-
278
-
<h2>Notes</h2>
279
-
280
-
// ...
281
-
282
-
</div>
283
-
)
284
-
```
285
-
286
-
If _user === null_ is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), _loginForm()_ is executed. If not, _noteForm()_ is.
287
-
288
257
Let's do one more modification. If the user is logged in, their name is shown on the screen:
289
258
290
259
```js
291
260
return (
292
261
<div>
293
262
<h1>Notes</h1>
294
-
295
263
<Notification message={errorMessage} />
296
264
297
-
{user ===null?
298
-
loginForm() :
265
+
{!user &&loginForm()}
266
+
// highlight-start
267
+
{user && (
299
268
<div>
300
-
<p>{user.name} logged-in</p>
269
+
<p>{user.name} loggedin</p>
301
270
{noteForm()}
302
271
</div>
303
-
}
304
-
305
-
<h2>Notes</h2>
272
+
)}
273
+
// highlight-end
306
274
275
+
<div>
276
+
<button onClick={() =>setShowAll(!showAll)}>
307
277
// ...
308
-
309
-
</div>
310
-
)
311
278
```
312
279
313
280
The solution isn't perfect, but we'll leave it like this for now.
@@ -358,10 +325,10 @@ const getAll = () => {
358
325
returnrequest.then(response=>response.data)
359
326
}
360
327
361
-
// highlight-start
362
328
constcreate=asyncnewObject=> {
329
+
// highlight-start
363
330
constconfig= {
364
-
headers: { Authorization: token },
331
+
headers: { Authorization: token }
365
332
}
366
333
// highlight-end
367
334
@@ -384,16 +351,14 @@ The event handler responsible for login must be changed to call the method <code
0 commit comments