@arielgonzaguer/michi-router 1.2.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +117 -64
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,12 +14,8 @@ Si tienes alguna de estas versiones instaladas (1.1.x), actualízala a la versi
|
|
|
14
14
|
npm install @arielgonzaguer/michi-router@latest
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
O:
|
|
18
|
-
```bash
|
|
19
|
-
npm install @arielgonzaguer/michi-router@1.2.2
|
|
20
|
-
```
|
|
21
17
|
|
|
22
|
-
Gracias por
|
|
18
|
+
Gracias por su comprensión 😸
|
|
23
19
|
|
|
24
20
|
# Michi Router
|
|
25
21
|
|
|
@@ -33,13 +29,11 @@ El objetivo principal de esta herramienta es proporcionar la funcionalidad bási
|
|
|
33
29
|
- No requiere de ninguna configuración adicional.
|
|
34
30
|
- No requiere de ninguna dependencia externa.
|
|
35
31
|
- Creado con TypeScript.
|
|
36
|
-
- Ultra ligero.
|
|
37
|
-
|
|
38
32
|
|
|
39
33
|
## Compatibilidad
|
|
40
34
|
|
|
41
35
|
- React 16.8+.
|
|
42
|
-
- Funciona con proyectos creados con
|
|
36
|
+
- Funciona con proyectos creados con Vite.
|
|
43
37
|
- Totalmente tipado con TypeScript.
|
|
44
38
|
|
|
45
39
|
## Instalación
|
|
@@ -50,9 +44,9 @@ npm install @arielgonzaguer/michi-router
|
|
|
50
44
|
|
|
51
45
|
## Uso básico
|
|
52
46
|
|
|
53
|
-
```
|
|
47
|
+
```tsx
|
|
54
48
|
// src/App.jsx //
|
|
55
|
-
import { RouterProvider} from "@arielgonzaguer/michi-router";
|
|
49
|
+
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
56
50
|
|
|
57
51
|
// puede crear rutas en un archivo separado o en el mismo archivo
|
|
58
52
|
const routes = [
|
|
@@ -73,8 +67,9 @@ function App() {
|
|
|
73
67
|
### Usando Link
|
|
74
68
|
|
|
75
69
|
#### Usando el componente Link solo con prop to
|
|
76
|
-
|
|
77
|
-
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { Link } from "@arielgonzaguer/michi-router";
|
|
78
73
|
|
|
79
74
|
function NavigationComponent() {
|
|
80
75
|
return (
|
|
@@ -87,13 +82,12 @@ function NavigationComponent() {
|
|
|
87
82
|
```
|
|
88
83
|
|
|
89
84
|
#### Usando el componente Link con todas las props
|
|
90
|
-
```jsx
|
|
91
|
-
import { Link} from "@arielgonzaguer/michi-router";
|
|
92
85
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
86
|
+
```tsx
|
|
87
|
+
<Link
|
|
88
|
+
to="/contact"
|
|
89
|
+
className="nav-link"
|
|
90
|
+
target="_blank"
|
|
97
91
|
rel="noopener noreferrer"
|
|
98
92
|
aria-label="Contacto"
|
|
99
93
|
>
|
|
@@ -101,25 +95,25 @@ import { Link} from "@arielgonzaguer/michi-router";
|
|
|
101
95
|
</Link>
|
|
102
96
|
```
|
|
103
97
|
|
|
104
|
-
|
|
105
|
-
|
|
106
98
|
### Navegación programática (Hook useNavigate)
|
|
107
99
|
|
|
108
|
-
```
|
|
100
|
+
```tsx
|
|
109
101
|
import { useNavigate } from "@arielgonzaguer/michi-router";
|
|
110
102
|
|
|
111
103
|
function NavigateButton() {
|
|
112
104
|
const navigate = useNavigate();
|
|
113
|
-
return
|
|
105
|
+
return;
|
|
106
|
+
<button onClick={() => navigate("/gatos")}>Ir a Gatos</button>;
|
|
114
107
|
}
|
|
115
108
|
```
|
|
116
109
|
|
|
117
110
|
### Organización de rutas en archivos separados
|
|
118
111
|
|
|
119
|
-
|
|
112
|
+
Puede organizar sus rutas en archivos separados.
|
|
113
|
+
Primero cree un archivo con las rutas:
|
|
120
114
|
|
|
121
|
-
```
|
|
122
|
-
// routes.
|
|
115
|
+
```tsx
|
|
116
|
+
// routes.tsx
|
|
123
117
|
import Home from "./pages/Home";
|
|
124
118
|
import About from "./pages/About";
|
|
125
119
|
import Products from "./pages/Products";
|
|
@@ -134,8 +128,10 @@ export const routes = [
|
|
|
134
128
|
export const notFoundPage = <NotFound />;
|
|
135
129
|
```
|
|
136
130
|
|
|
137
|
-
|
|
138
|
-
|
|
131
|
+
Después utilice este archivo en su componente que maneja el enrutado:
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
// App.tsx
|
|
139
135
|
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
140
136
|
import { routes, notFoundPage } from "./routes";
|
|
141
137
|
|
|
@@ -144,31 +140,90 @@ function App() {
|
|
|
144
140
|
}
|
|
145
141
|
```
|
|
146
142
|
|
|
147
|
-
###
|
|
143
|
+
### Protección de rutas
|
|
144
|
+
|
|
145
|
+
Si necesita que sus rutas se mantengan protegidas, puede usar un componente que cumpla este propósito, como el siguiente:
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
// Protected.tsx
|
|
149
|
+
import { useEffect } from "react";
|
|
150
|
+
import { useNavigate } from "@arielgonzaguer/michi-router";
|
|
151
|
+
import useAuthStore from "../store/useAuthStore"; // esta sería la store que maneja la autenticacación en su proyecto
|
|
152
|
+
import { ReactNode } from "react"; // tipado
|
|
153
|
+
|
|
154
|
+
export default function Protected({ children }: { children: ReactNode }) {
|
|
155
|
+
const navigate = useNavigate();
|
|
156
|
+
const { user } = useAuthStore();
|
|
157
|
+
|
|
158
|
+
useEffect(() => {
|
|
159
|
+
if (!user) {
|
|
160
|
+
navigate("/login"); // si no hay usuario autenticado en la store, redirige al login
|
|
161
|
+
}
|
|
162
|
+
}, [user, navigate]);
|
|
163
|
+
|
|
164
|
+
return user ? children : null; // Devuelve los hijos solo si la persona usuaria está autenticada
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Después, puede usar ese componente en el archivo dónde maneja sus rutas:
|
|
148
169
|
|
|
149
|
-
```
|
|
170
|
+
```tsx
|
|
171
|
+
// MichiRouter.tsx
|
|
150
172
|
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
151
|
-
import
|
|
173
|
+
import Home from "../pages/Home";
|
|
174
|
+
import ActualizarGatos from "../pages/ActualizarGatos";
|
|
175
|
+
import VerGatos from "../pages/VerGatos";
|
|
176
|
+
import AgregarGatos from "../pages/AgregarGatos";
|
|
177
|
+
import NotFoud404 from "../components/NotFound404";
|
|
178
|
+
import Index from "../pages/Index";
|
|
152
179
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
{ path: "/", component: <Home /> },
|
|
159
|
-
{ path: "/login", component: <Login /> },
|
|
160
|
-
// Solo agregamos estas rutas si el usuario está autenticado
|
|
161
|
-
...(isAuthenticated
|
|
162
|
-
? [
|
|
163
|
-
{ path: "/profile", component: <Profile /> },
|
|
164
|
-
{ path: "/admin", component: <Admin /> },
|
|
165
|
-
]
|
|
166
|
-
: []),
|
|
167
|
-
];
|
|
180
|
+
// protección de rutas
|
|
181
|
+
import Protected from "./Protected";
|
|
182
|
+
|
|
183
|
+
// layout
|
|
184
|
+
import BaseLayout from "../layouts/BaseLayout";
|
|
168
185
|
|
|
186
|
+
const rutas = [
|
|
187
|
+
{ path: "/", component: <Home /> },
|
|
188
|
+
// encapsule los componente a elegir dentro de <Protected>
|
|
189
|
+
{
|
|
190
|
+
path: "/index",
|
|
191
|
+
component: (
|
|
192
|
+
<Protected>
|
|
193
|
+
<Index />
|
|
194
|
+
</Protected>
|
|
195
|
+
),
|
|
196
|
+
},
|
|
197
|
+
{
|
|
198
|
+
path: "/actualizar-gatos",
|
|
199
|
+
component: (
|
|
200
|
+
<Protected>
|
|
201
|
+
<ActualizarGatos />
|
|
202
|
+
</Protected>
|
|
203
|
+
),
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
path: "/ver-gatos",
|
|
207
|
+
component: (
|
|
208
|
+
<Protected>
|
|
209
|
+
<VerGatos />{" "}
|
|
210
|
+
</Protected>
|
|
211
|
+
),
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
path: "/agregar-gatos",
|
|
215
|
+
component: (
|
|
216
|
+
<Protected>
|
|
217
|
+
<AgregarGatos />
|
|
218
|
+
</Protected>
|
|
219
|
+
),
|
|
220
|
+
},
|
|
221
|
+
];
|
|
222
|
+
|
|
223
|
+
export default function MichiRouter() {
|
|
169
224
|
return (
|
|
170
|
-
<RouterProvider routes={
|
|
171
|
-
<
|
|
225
|
+
<RouterProvider routes={rutas} layout={BaseLayout}>
|
|
226
|
+
<NotFoud404 />
|
|
172
227
|
</RouterProvider>
|
|
173
228
|
);
|
|
174
229
|
}
|
|
@@ -176,15 +231,15 @@ function App() {
|
|
|
176
231
|
|
|
177
232
|
### Usando un Layout General
|
|
178
233
|
|
|
179
|
-
|
|
234
|
+
Puede aplicar un layout común a todas tus rutas:
|
|
180
235
|
|
|
181
|
-
```
|
|
236
|
+
```tsx
|
|
182
237
|
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
183
238
|
import MainLayout from "./layouts/MainLayout";
|
|
184
239
|
|
|
185
240
|
const routes = [
|
|
186
241
|
{ path: "/", component: <Home /> },
|
|
187
|
-
{ path: "/
|
|
242
|
+
{ path: "/felinos-ferales", component: <Ferales /> },
|
|
188
243
|
];
|
|
189
244
|
|
|
190
245
|
function App() {
|
|
@@ -202,7 +257,7 @@ function MainLayout({ children }) {
|
|
|
202
257
|
<header>
|
|
203
258
|
<nav>
|
|
204
259
|
<Link to="/">Home</Link>
|
|
205
|
-
<Link to="/
|
|
260
|
+
<Link to="/felinos-ferales">Ferales</Link>
|
|
206
261
|
</nav>
|
|
207
262
|
</header>
|
|
208
263
|
<main>{children}</main>
|
|
@@ -243,24 +298,22 @@ Hook para navegación programática.
|
|
|
243
298
|
|
|
244
299
|
- Función navigate que acepta la ruta como parámetro.
|
|
245
300
|
|
|
246
|
-
##
|
|
301
|
+
## Próximas características
|
|
247
302
|
|
|
248
|
-
|
|
303
|
+
- [ ] Transiciones entre rutas.
|
|
249
304
|
|
|
250
|
-
|
|
305
|
+
```jsx
|
|
306
|
+
<RouterProvider routes={routes} transitions={true} transitionDuration={300}>
|
|
307
|
+
<NotFound />
|
|
308
|
+
</RouterProvider>
|
|
309
|
+
```
|
|
251
310
|
|
|
252
|
-
|
|
311
|
+
## Solución de problemas comunes
|
|
312
|
+
|
|
313
|
+
### El componente no se renderiza después de navegar
|
|
253
314
|
|
|
254
|
-
|
|
315
|
+
Asegúrese de que la ruta en el array `routes` coincida exactamente con la URL, incluyendo barras diagonales.
|
|
255
316
|
|
|
256
|
-
```typescript
|
|
257
|
-
import {
|
|
258
|
-
RouterProvider,
|
|
259
|
-
Link,
|
|
260
|
-
useNavigate,
|
|
261
|
-
} from "@arielgonzaguer/michi-router";
|
|
262
|
-
import type { RouterProviderProps } from "@arielgonzaguer/michi-router";
|
|
263
|
-
```
|
|
264
317
|
|
|
265
318
|
## Contribuir
|
|
266
319
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arielgonzaguer/michi-router",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"description": "El router minimalista y simple para React.\nEl objetivo principal de esta herramienta es proporcionar la funcionalidad básica de enrutamiento.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|