今日の忙しい:uiライブラリを作成するときに注意しなければならないこと(共通)

2786 ワード

ブラウザオブジェクト(docuemntnavigatorなど)を防止するには
サーバ上では以下のように
typeof docuemnt === 'undefined'
typeof navigator === 'undefined'
typeof window === 'undefined'
次のhookを作成して使用します.
import React from 'react';

const useBrowserEffect: typeof React.useEffect = (effect, deps = []) => {
  React.useEffect(() => {
    if (typeof document !== 'undefined') {
      effect();
    }
  }, [...deps]);
};

export default useBrowserEffect;