Skip to content

Commit 52600eb

Browse files
committed
Working with Query Parameters
1 parent 9c41b27 commit 52600eb

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

frontend/src/components/AuthForm.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import React, { useState } from 'react';
2-
import { Form } from 'react-router-dom';
1+
import React, { Fragment } from 'react';
2+
import { Form, Link, useSearchParams } from 'react-router-dom';
33

44
import classes from './AuthForm.module.css';
55

66
function AuthForm() {
7-
const [isLogin, setIsLogin] = useState(true);
7+
const [searchParams] = useSearchParams();
88

9-
function switchAuthHandler() {
10-
setIsLogin(isCurrentlyLogin => !isCurrentlyLogin);
11-
}
9+
const isLogin = searchParams.get('mode') === 'login';
1210

1311
return (
14-
<>
12+
<Fragment>
1513
<Form method="post" className={classes.form}>
1614
<h1>{isLogin ? 'Log in' : 'Create a new user'}</h1>
1715
<p>
@@ -23,13 +21,13 @@ function AuthForm() {
2321
<input id="password" type="password" name="password" required />
2422
</p>
2523
<div className={classes.actions}>
26-
<button onClick={switchAuthHandler} type="button">
24+
<Link to={`?mode=${isLogin ? 'signup' : 'login'}`}>
2725
{isLogin ? 'Create new user' : 'Login'}
28-
</button>
29-
<button>Save</button>
26+
</Link>
27+
<button>{isLogin ? 'Login' : 'Save'}</button>
3028
</div>
3129
</Form>
32-
</>
30+
</Fragment>
3331
);
3432
}
3533

frontend/src/components/MainNavigation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function MainNavigation() {
4242
</li>
4343
<li>
4444
<NavLink
45-
to="/auth"
45+
to="/auth?mode=login"
4646
className={({ isActive }) =>
4747
isActive ? classes.active : undefined
4848
}

0 commit comments

Comments
 (0)