0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ReactとNext.jsでデフォルトと名前付きエクスポートをimport(インポート)する時の注意点

Posted at

名前付きエクスポート (export {})

そのファイルで複数のエクスポート(他のボタンコンポーネントなど)がある時に使います。

PrimaryButton.tsx
const PrimaryBtn = ({children} : PropsType  ) => {
  return (
    <Button 
        bg="lime.800"
        color="white" 
        p="8" 
        borderRadius="lg" 
        fontSize="20" _hover={{ bg: "lime.700" }}>
            { children }
    </Button>
  )
}

export default PrimaryButton;

デフォルトエクスポート(export default)

そのモジュールに1つのエクスポートしかない時に使います。

PrimaryButton.tsx
export const PrimaryBtn = ({children} : PropsType  ) => {
  return (
    <Button 
        bg="lime.800"
        color="white" 
        p="8" 
        borderRadius="lg" 
        fontSize="20" _hover={{ bg: "lime.700" }}>
            { children }
    </Button>
  )
}

importの書き方の違い

名前付きエクスポートをインポート

{}を使ってインポートします。

import { PrimaryButton } from "@/components/atoms/PrimaryButton.tsx";

デフォルトエクスポートをインポート

特に {}などはつけない です。

import  PrimaryButton  from "@/components/atoms/PrimaryButton.tsx";
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?