react-location-handler 1.1.2 → 1.1.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 +18 -8
- package/package.json +1 -1
- package/src/index.js +22 -9
package/README.md
CHANGED
|
@@ -3,29 +3,39 @@
|
|
|
3
3
|
A lightweight React hook to conditionally render components based on current routes.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
|
-
|
|
7
6
|
```bash
|
|
7
|
+
|
|
8
|
+
|
|
8
9
|
npm install react-location-handler
|
|
9
10
|
|
|
11
|
+
|
|
10
12
|
import { useHandleLocation } from 'react-location-handler';
|
|
11
13
|
|
|
14
|
+
|
|
12
15
|
const AppContent = ()=> {
|
|
13
|
-
|
|
16
|
+
|
|
17
|
+
const { Hide , Show } = useHandleLocation(['/login' , '/signup' , '/404']);
|
|
14
18
|
|
|
15
19
|
return (
|
|
16
20
|
<>
|
|
17
|
-
|
|
18
|
-
{Hide([]
|
|
19
|
-
<Navbar/>
|
|
20
|
-
))}
|
|
21
|
+
|
|
22
|
+
{Hide([], <Navbar />)}
|
|
21
23
|
|
|
22
24
|
<Routes>
|
|
25
|
+
|
|
23
26
|
<Route path="/" element={<Home />} />
|
|
27
|
+
|
|
24
28
|
<Route path="/login" element={<Login />} />
|
|
29
|
+
|
|
25
30
|
<Route path="/register" element={<Register />} />
|
|
31
|
+
|
|
26
32
|
</Routes>
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
{Show([], <Footer />)}
|
|
35
|
+
|
|
29
36
|
</>
|
|
37
|
+
|
|
30
38
|
);
|
|
31
|
-
|
|
39
|
+
|
|
40
|
+
};
|
|
41
|
+
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,47 +1,60 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
|
|
2
3
|
import { useLocation } from "react-router-dom";
|
|
4
|
+
|
|
3
5
|
import { useMemo } from "react";
|
|
4
6
|
|
|
5
7
|
const useHandleLocation = (hiddenPath = [], defaultElement = null) => {
|
|
6
|
-
const { pathname } = useLocation();
|
|
7
8
|
|
|
9
|
+
const { pathname } = useLocation();
|
|
8
10
|
const checkHidden = useMemo(() => {
|
|
11
|
+
|
|
9
12
|
if (typeof hiddenPath === 'function') return hiddenPath;
|
|
13
|
+
|
|
10
14
|
if (!Array.isArray(hiddenPath)) return () => false;
|
|
11
15
|
|
|
12
16
|
const pathSet = new Set(hiddenPath.filter((p) => typeof p === "string"));
|
|
17
|
+
|
|
13
18
|
return (path) => pathSet.has(path)
|
|
14
|
-
}, [hiddenPath])
|
|
15
19
|
|
|
20
|
+
}, [hiddenPath])
|
|
16
21
|
|
|
17
22
|
const isHidden = checkHidden(pathname);
|
|
18
23
|
|
|
19
24
|
|
|
20
|
-
|
|
21
25
|
const getRanderContent = (element) => {
|
|
26
|
+
|
|
22
27
|
const content = element || defaultElement;
|
|
28
|
+
|
|
23
29
|
if (typeof content === 'function') {
|
|
30
|
+
|
|
24
31
|
return content();
|
|
32
|
+
|
|
25
33
|
}
|
|
26
34
|
|
|
27
|
-
return content;
|
|
28
|
-
};
|
|
29
35
|
|
|
30
36
|
|
|
37
|
+
return content;
|
|
38
|
+
|
|
39
|
+
};
|
|
31
40
|
const Hide = (overridePath = [], elements) => {
|
|
41
|
+
|
|
32
42
|
const condition = overridePath.length > 0 ? overridePath.includes(pathname) : isHidden;
|
|
43
|
+
|
|
33
44
|
return condition ? null : getRanderContent(elements);
|
|
45
|
+
|
|
34
46
|
}
|
|
35
47
|
|
|
36
48
|
const Show = (overridePath = [], elements) => {
|
|
49
|
+
|
|
37
50
|
const condition = overridePath.length > 0 ? overridePath.includes(pathname) : isHidden;
|
|
38
|
-
return condition ? getRanderContent(elements) : null;
|
|
39
|
-
}
|
|
40
51
|
|
|
52
|
+
return condition ? getRanderContent(elements) : null;
|
|
41
53
|
|
|
54
|
+
}
|
|
42
55
|
|
|
43
56
|
return { isHidden, Hide, Show, getRanderContent }
|
|
44
|
-
|
|
45
57
|
}
|
|
46
58
|
|
|
47
|
-
export{ useHandleLocation };
|
|
59
|
+
export { useHandleLocation };
|
|
60
|
+
|