The problem is that when entering a route that does not exist the does not work, this only happens when I separate the / login and / register routes in another component, is there any way to solve it? Thanks for reading
AppRouter.js
JavaScript
x
47
47
1
import React from 'react'
2
import {
3
BrowserRouter as Router,
4
Route,
5
Switch,
6
Link,
7
Redirect
8
} from 'react-router-dom'
9
10
import JournalScreen from '../screens/JournalScreen'
11
import AuthRoutes from './AuthRoutes'
12
13
export default function AppRouter() {
14
return (
15
<Router>
16
17
<nav>
18
<ul>
19
<li>
20
<Link to="/">Home</Link>
21
</li>
22
<li>
23
<Link to="/noexists">no exists</Link>
24
</li>
25
<li>
26
<Link to="/register">register</Link>
27
</li>
28
<li>
29
<Link to="/login">login</Link>
30
</li>
31
</ul>
32
</nav>
33
34
<Switch>
35
36
<Route exact path="/" component={JournalScreen} />
37
38
<AuthRoutes />
39
40
<Redirect to="/" />
41
42
</Switch>
43
44
</Router>
45
)
46
}
47
AuthRoutes.js
JavaScript
1
20
20
1
import React from 'react'
2
import { Route } from 'react-router-dom'
3
import LoginScreen from '../screens/LoginScreen'
4
import RegisterScreen from '../screens/RegisterScreen'
5
6
export default function AuthRouter() {
7
return (
8
9
<div className="app__auth">
10
11
<Route path="/login" component={LoginScreen} />
12
13
<Route path="/register" component={RegisterScreen} />
14
15
</div>
16
17
18
)
19
}
20
Advertisement
Answer
I found this solution:
AppRouter.js
JavaScript
1
47
47
1
import React from 'react'
2
import {
3
BrowserRouter as Router,
4
Route,
5
Switch,
6
Link,
7
Redirect
8
} from 'react-router-dom'
9
10
import JournalScreen from '../screens/JournalScreen'
11
import AuthRoutes from './AuthRoutes'
12
13
export default function AppRouter() {
14
return (
15
<Router>
16
17
<nav>
18
<ul>
19
<li>
20
<Link to="/">Home</Link>
21
</li>
22
<li>
23
<Link to="/noexists">no exists</Link>
24
</li>
25
<li>
26
<Link to="/register">register</Link>
27
</li>
28
<li>
29
<Link to="/login">login</Link>
30
</li>
31
</ul>
32
</nav>
33
34
<Switch>
35
36
<Route exact path="/" component={JournalScreen} />
37
38
<AuthRoutes />
39
40
<Redirect to="/" />
41
42
</Switch>
43
44
</Router>
45
)
46
}
47
AuthRoutes.js
JavaScript
1
27
27
1
import React from 'react'
2
import { Route, Switch, Redirect } from 'react-router-dom'
3
import LoginScreen from '../screens/LoginScreen'
4
import RegisterScreen from '../screens/RegisterScreen'
5
6
export default function AuthRoutes() {
7
8
return (
9
10
<div className="app__auth">
11
12
<Switch>
13
14
<Route path="/login" component={LoginScreen} />
15
16
<Route path="/register" component={RegisterScreen} />
17
18
<Redirect to="/" />
19
20
</Switch>
21
22
</div>
23
24
25
)
26
}
27