Skip to content

Commit

Permalink
Add: useClient の導入
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoSquirrels committed Aug 2, 2022
1 parent a132618 commit 4f4f0bd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
32 changes: 16 additions & 16 deletions components/countdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

// next
import React from "react";
// hooks
import useClient from "@/hooks/client";
// styles
import styles from "./countdown.module.scss";

Expand All @@ -10,24 +12,22 @@ export const FES_START = FES_FIRST_DAY + 9 * 3600000; // 09:00
export const FES_END = FES_FIRST_DAY + 86400000 + 18 * 3600000; // 18:00

const Remaining = () => {
const [now, setNow] = React.useState();
const isClient = useClient();
const [now, setNow] = React.useState(Date.now());
React.useEffect(() => {
setInterval(() => setNow(Date.now()), 1000);
}, []);
if (isClient) setInterval(() => setNow(Date.now()), 1000);
}, [isClient]);

if (now === undefined) {
setTimeout(() => setNow(Date.now()), 0);
return <></>;
} else
return (
<>
あと
<span className={styles.number}>
{1 + Math.floor((FES_FIRST_DAY - now) / 86400000)}
</span>
日!
</>
);
if (!isClient) return <></>;
return (
<>
あと
<span className={styles.number}>
{1 + Math.floor((FES_FIRST_DAY - now) / 86400000)}
</span>
日!
</>
);
};

const Countdown = () => {
Expand Down
12 changes: 2 additions & 10 deletions components/layout/header.jsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
// SPDX-License-Identifier: MIT

// react
import React from "react";
// hooks
import { useMediaQuery } from "react-responsive";
import useClient from "@/hooks/client";
// components
import Link from "@/components/base/link";
import Menu from "./menu";
// styles
import styles from "./header.module.scss";

const useClient = () => {
const [isClient, setIsClient] = React.useState(false);
React.useEffect(() => {
if (typeof window !== "undefined") setIsClient(true);
}, []);
return isClient;
};

const Header = ({ setScrollable }) => {
const isClient = useClient();
const isNarrow = useMediaQuery({ query: "(max-width: 600px)" });
Expand Down
19 changes: 19 additions & 0 deletions hooks/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT

// react
import React from "react";

/**
* ブラウザでのレンダリング時かを判定するフック
* window が定義されているかどうかで調べます。
* @return {boolean} レンダリング時かどうか
*/
const useClient = () => {
const [isClient, setIsClient] = React.useState(false);
React.useEffect(() => {
if (typeof window !== "undefined") setIsClient(true);
}, []);
return isClient;
};

export default useClient;
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const customJestConfig = {
"^@/package": "<rootDir>/package",
"^@/next-seo.config": "<rootDir>/next-seo.config",
// Handle module aliases (this will be automatically configured for you soon)
"^@/hooks/(.*)$": "<rootDir>/hooks/$1",
"^@/components/(.*)$": "<rootDir>/components/$1",
"^@/pages/(.*)$": "<rootDir>/pages/$1",
},
Expand Down
1 change: 1 addition & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"paths": {
"@/package": ["package"],
"@/next-seo.config": ["next-seo.config"],
"@/hooks/*": ["hooks/*"],
"@/components/*": ["components/*"],
"@/pages/*": ["pages/*"]
}
Expand Down

0 comments on commit 4f4f0bd

Please sign in to comment.