Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useRoute hook #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/App.jsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don´t modify this file because it is for testing only. Leave it like this:

const AppJs = () => {
  return <p>Test your hook here</p>;
};

export default AppJs;

Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { useBattery } from "./hooks/js/useBattery";
import { useBattery } from "./hooks/js/useBattery"
import { useIsTouchDevice } from "./hooks/js/useIsTouchDevice"
import { useRoute } from "./hooks/js/useRoute"

function AppJs() {
const battery = useBattery();
const isTouchDevice = useIsTouchDevice();
const battery = useBattery()
const isTouchDevice = useIsTouchDevice()
const route = useRoute()

return (
<div>
<p>Battery level:{battery.level * 100}</p>
<p>{battery.charging ? "Battery charging" : "Battery not charging"}</p>

{isTouchDevice ? 'It is a touch device' : 'It is not a touch device'}

<p>Current route: {route.route}</p>
{isTouchDevice ? "It is a touch device" : "It is not a touch device"}
</div>
);
)
}

export default AppJs;
export default AppJs
17 changes: 10 additions & 7 deletions src/App.tsx
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don´t modify this file because it is for testing only. Leave it like this:

const AppTs = () => {
  return <p>Test your hook here</p>;
};

export default AppTs;

Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { useBattery } from "./hooks/ts/useBattery";
import { useIsTouchDevice } from "./hooks/ts/useIsTouchDevice";
import { useBattery } from "./hooks/ts/useBattery"
import { useIsTouchDevice } from "./hooks/ts/useIsTouchDevice"
import { useRoute } from "./hooks/ts/useRoute"

function AppTs() {
const battery = useBattery();
const isTouchDevice = useIsTouchDevice();
const battery = useBattery()
const isTouchDevice = useIsTouchDevice()
const route = useRoute()

return (
<div>
<p>Battery level:{battery.level && battery.level * 100}</p>
<p>{battery.charging ? "Battery charging" : "Battery not charging"}</p>
<p>Current route: {route.route}</p>

{isTouchDevice ? 'It is a touch device' : 'It is not a touch device'}
{isTouchDevice ? "It is a touch device" : "It is not a touch device"}
</div>
);
)
}

export default AppTs;
export default AppTs
20 changes: 20 additions & 0 deletions src/hooks/js/useRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useEffect, useState } from "react"

export const useRoute = () => {
const [route, setRoute] = useState(window.location.pathname)

useEffect(() => {
const handleRouteChange = () => setRoute(window.location.pathname)

window.addEventListener("popstate", handleRouteChange)

return () => window.removeEventListener("popstate", handleRouteChange)
}, [])

const navigate = to => {
window.history.pushState({}, "", to)
setRoute(to)
}

return { route, navigate }
}
25 changes: 25 additions & 0 deletions src/hooks/ts/useRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useEffect, useState } from "react"

interface UseRouteOutput {
route: string
navigate: (newRoute: string) => void
}

export const useRoute = (): UseRouteOutput => {
const [route, setRoute] = useState<string>(window.location.pathname)

useEffect(() => {
const handlePopState = () => setRoute(window.location.pathname)

window.addEventListener("popstate", handlePopState)

return () => window.removeEventListener("popstate", handlePopState)
}, [])

const navigate = (newRoute: string) => {
window.history.pushState({}, "", newRoute)
setRoute(newRoute)
}

return { route, navigate }
}