@arielgonzaguer/michi-router 1.0.2 → 1.0.4
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 +84 -26
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
# Michi Router
|
|
2
2
|
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
3
7
|
El router minimalista y simple para React.
|
|
4
8
|
El objetivo principal de esta herramienta es proporcionar la funcionalidad básica de enrutamiento.
|
|
5
9
|
|
|
6
10
|
## Características
|
|
7
11
|
|
|
8
|
-
- Es
|
|
12
|
+
- Es ideal para proyectos pequeños o que solo necesitan una funcionalidad básica de enrutamiento.
|
|
9
13
|
- No requiere de ninguna configuración adicional.
|
|
10
14
|
- No requiere de ninguna dependencia externa.
|
|
11
|
-
- Es compatible con React 16.8 y versiones posteriores.
|
|
12
15
|
- Creado con TypeScript.
|
|
13
|
-
- Ultra ligero. Menos de
|
|
16
|
+
- Ultra ligero. Menos de 20kb (más ligero y básico que otras librerías).
|
|
17
|
+
|
|
18
|
+
## Compatibilidad
|
|
19
|
+
|
|
20
|
+
- React 16.8+.
|
|
21
|
+
- Funciona con proyectos creados con Create React App, Vite, Next.js, etc.
|
|
22
|
+
- Totalmente tipado con TypeScript.
|
|
14
23
|
|
|
15
24
|
## Installation
|
|
16
25
|
|
|
@@ -41,6 +50,8 @@ function App() {
|
|
|
41
50
|
```
|
|
42
51
|
|
|
43
52
|
### Usando Link
|
|
53
|
+
|
|
54
|
+
#### Usando el componente Link solo con prop to
|
|
44
55
|
```jsx
|
|
45
56
|
function NavigationComponent() {
|
|
46
57
|
return (
|
|
@@ -52,8 +63,26 @@ function NavigationComponent() {
|
|
|
52
63
|
}
|
|
53
64
|
```
|
|
54
65
|
|
|
66
|
+
#### Usando el componente Link con todas las props
|
|
67
|
+
```jsx
|
|
68
|
+
<Link
|
|
69
|
+
to="/contact"
|
|
70
|
+
className="nav-link"
|
|
71
|
+
target="_blank"
|
|
72
|
+
rel="noopener noreferrer"
|
|
73
|
+
aria-label="Contacto"
|
|
74
|
+
>
|
|
75
|
+
Contacto
|
|
76
|
+
</Link>
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
55
81
|
### Navegación programática (Hook useNavigate)
|
|
82
|
+
|
|
56
83
|
```jsx
|
|
84
|
+
import { useNavigate } from "@arielgonzaguer/michi-router";
|
|
85
|
+
|
|
57
86
|
function NavigateButton() {
|
|
58
87
|
const navigate = useNavigate();
|
|
59
88
|
return <button onClick={() => navigate("/about")}>Ir a About</button>;
|
|
@@ -66,10 +95,10 @@ Para aplicaciones más grandes, puedes organizar tus rutas en archivos separados
|
|
|
66
95
|
|
|
67
96
|
```jsx
|
|
68
97
|
// routes.js
|
|
69
|
-
import Home from
|
|
70
|
-
import About from
|
|
71
|
-
import Products from
|
|
72
|
-
import NotFound from
|
|
98
|
+
import Home from "./pages/Home";
|
|
99
|
+
import About from "./pages/About";
|
|
100
|
+
import Products from "./pages/Products";
|
|
101
|
+
import NotFound from "./pages/NotFound";
|
|
73
102
|
|
|
74
103
|
export const routes = [
|
|
75
104
|
{ path: "/", component: <Home /> },
|
|
@@ -83,14 +112,10 @@ export const notFoundPage = <NotFound />;
|
|
|
83
112
|
```jsx
|
|
84
113
|
// App.jsx
|
|
85
114
|
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
86
|
-
import { routes, notFoundPage } from
|
|
115
|
+
import { routes, notFoundPage } from "./routes";
|
|
87
116
|
|
|
88
117
|
function App() {
|
|
89
|
-
return
|
|
90
|
-
<RouterProvider routes={routes}>
|
|
91
|
-
{notFoundPage}
|
|
92
|
-
</RouterProvider>
|
|
93
|
-
);
|
|
118
|
+
return <RouterProvider routes={routes}>{notFoundPage}</RouterProvider>;
|
|
94
119
|
}
|
|
95
120
|
```
|
|
96
121
|
|
|
@@ -102,18 +127,20 @@ import { useAuth } from "./auth-context";
|
|
|
102
127
|
|
|
103
128
|
function App() {
|
|
104
129
|
const { isAuthenticated } = useAuth();
|
|
105
|
-
|
|
130
|
+
|
|
106
131
|
// Definimos rutas condicionalmente basadas en autenticación
|
|
107
132
|
const routes = [
|
|
108
133
|
{ path: "/", component: <Home /> },
|
|
109
134
|
{ path: "/login", component: <Login /> },
|
|
110
135
|
// Solo agregamos estas rutas si el usuario está autenticado
|
|
111
|
-
...(isAuthenticated
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
136
|
+
...(isAuthenticated
|
|
137
|
+
? [
|
|
138
|
+
{ path: "/profile", component: <Profile /> },
|
|
139
|
+
{ path: "/admin", component: <Admin /> },
|
|
140
|
+
]
|
|
141
|
+
: []),
|
|
115
142
|
];
|
|
116
|
-
|
|
143
|
+
|
|
117
144
|
return (
|
|
118
145
|
<RouterProvider routes={routes}>
|
|
119
146
|
<div>404: Page not found</div>
|
|
@@ -168,9 +195,9 @@ Componente principal para el enrutamiento.
|
|
|
168
195
|
|
|
169
196
|
**Props:**
|
|
170
197
|
|
|
171
|
-
- `routes`: Array de objetos con `path` y `component
|
|
172
|
-
- `children`: Elemento a mostrar cuando no hay ruta coincidente
|
|
173
|
-
- `layout`: Componente opcional que funciona como layout general para todas las rutas
|
|
198
|
+
- `routes`: Array de objetos con `path` y `component`.
|
|
199
|
+
- `children`: Elemento a mostrar cuando no hay ruta coincidente.
|
|
200
|
+
- `layout`: Componente opcional que funciona como layout general para todas las rutas.
|
|
174
201
|
|
|
175
202
|
### `<Link>`
|
|
176
203
|
|
|
@@ -178,9 +205,10 @@ Componente para navegación declarativa.
|
|
|
178
205
|
|
|
179
206
|
**Props:**
|
|
180
207
|
|
|
181
|
-
- `to`: Ruta destino
|
|
182
|
-
- `children`: Contenido del enlace
|
|
183
|
-
- `className`: Clases CSS opcionales
|
|
208
|
+
- `to`: Ruta destino.
|
|
209
|
+
- `children`: Contenido del enlace.
|
|
210
|
+
- `className`: Clases CSS opcionales.
|
|
211
|
+
- `...rest`: Cualquier otra prop válida para elementos `<a>` de HTML.
|
|
184
212
|
|
|
185
213
|
### `useNavigate()`
|
|
186
214
|
|
|
@@ -188,7 +216,7 @@ Hook para navegación programática.
|
|
|
188
216
|
|
|
189
217
|
**Retorna:**
|
|
190
218
|
|
|
191
|
-
- Función navigate que acepta la ruta como parámetro
|
|
219
|
+
- Función navigate que acepta la ruta como parámetro.
|
|
192
220
|
|
|
193
221
|
## Próximas características
|
|
194
222
|
|
|
@@ -208,12 +236,42 @@ Hook para navegación programática.
|
|
|
208
236
|
```
|
|
209
237
|
|
|
210
238
|
- [ ] Transiciones entre rutas.
|
|
239
|
+
|
|
211
240
|
```jsx
|
|
212
241
|
<RouterProvider routes={routes} transitions={true} transitionDuration={300}>
|
|
213
242
|
<NotFound />
|
|
214
243
|
</RouterProvider>
|
|
215
244
|
```
|
|
216
245
|
|
|
246
|
+
## Solución de problemas comunes
|
|
247
|
+
|
|
248
|
+
### El componente no se renderiza después de navegar
|
|
249
|
+
|
|
250
|
+
Asegúrate de que la ruta en el array `routes` coincida exactamente con la URL, incluyendo barras diagonales.
|
|
251
|
+
|
|
252
|
+
### Errores con TypeScript
|
|
253
|
+
|
|
254
|
+
Si estás usando TypeScript, asegúrate de importar los tipos correctamente:
|
|
255
|
+
|
|
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
|
+
|
|
265
|
+
## Contribuir
|
|
266
|
+
|
|
267
|
+
Las contribuciones son bienvenidas. Por favor, sigue estos pasos:
|
|
268
|
+
|
|
269
|
+
1. Fork el repositorio.
|
|
270
|
+
2. Crea una rama para tu feature (`git checkout -b feature/amazing-feature`).
|
|
271
|
+
3. Haz commit de tus cambios (`git commit -m 'Add some amazing feature'`).
|
|
272
|
+
4. Push a la rama (`git push origin feature/amazing-feature`).
|
|
273
|
+
5. Abre un Pull Request.
|
|
274
|
+
|
|
217
275
|
## Licencia
|
|
218
276
|
|
|
219
277
|
MIT
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arielgonzaguer/michi-router",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "1.0.
|
|
5
|
-
"description": "
|
|
4
|
+
"version": "1.0.4",
|
|
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",
|
|
8
8
|
"files": [
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"router",
|
|
21
21
|
"typescript",
|
|
22
22
|
"michi",
|
|
23
|
-
"cats"
|
|
23
|
+
"cats",
|
|
24
|
+
"gatos"
|
|
24
25
|
],
|
|
25
26
|
"peerDependencies": {
|
|
26
27
|
"react": ">=16.8.0",
|