-
HI all, I am going to implement custom hook in my application, so i have something like But when i open the application on browser by It would prompt me See the example code below export default function App() {
const time = useTimer()
return <>
<div>{time}</div>
<Outlet/>
</>;
} In my hook my file (named import {useEffect, useState} from "react"
export function useTimer() {
const [time, setTime] = useState(new Date().toLocaleTimeString())
useEffect(() => {
const interval = setInterval(() => {
setTime(new Date().toLocaleTimeString())
}, 1000)
return () => clearInterval(interval)
}, [])
return time
} If i add typecheck in the component , it would work perfectly. function App() {
if (typeof useTimer !== "function") {
console.log("useTimer is not a function", {
useTimer,
}); // useTimer will undefined for once
return null;
}
const time = useTimer()
return <>
<div>{time}</div>
<Outlet/>
</>;
} Or if rename my hook file to Do i misunderstand of https://remix.run/docs/en/main/file-conventions/-client ? the client module should used for wrapping of some dependency that used Browser only API on the top level ? Example repo here: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You only need to mark a file as client-only (add |
Beta Was this translation helpful? Give feedback.
You only need to mark a file as client-only (add
.client
) if it breaks the server when importing it, otherwise it's not an issue and will you will need to first check if the hook is defined to call it since server-side the hook will be undefined.