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?

More than 1 year has passed since last update.

Styledで定義したstyled-componentsにcssが適用されないとき

Posted at

styledを使ってcssを適用したコンポーネントを定義する際、never usedのwarningが出てcssも適用されないときの対処法

問題が出るテストコード

import React from 'react'
import { Paper } from '@mui/material';
import {styled} from '@mui/material/styles';


// styledされた変数の名前が重要
const styledpaper = styled(Paper)(({theme}) => ({
    marginTop: theme.spacing(8),
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
}));

const test = () => {
  return (
    <styledpaper>TestText</styledpaper>
  )
}

export default test

実際のエラー

 Line 7:7:  'styledpaper' is assigned a value but never used  no-unused-vars

解決策

styledで定義した変数名を大文字にする必要がある
エラーの出るコードでは

const styledpaper = styled(Paper)(({theme}) => ({

というふうにstyleを適用しており、この時宣言する変数名styledpaperは先頭を大文字にする必要がある。
今回で言えば"styledpaper" を "Styledpaper" に変更する。

## 変更後

import React from 'react'
import { Paper } from '@mui/material';
import {styled} from '@mui/material/styles';


// styledされた変数の名前をStyledpaperに変更
const Styledpaper = styled(Paper)(({theme}) => ({
    marginTop: theme.spacing(8),
    display: "flex",
    flexDirection: "column",
    alignItems: "center",
}));

const test = () => {
  return (
    <Styledpaper>TestText</Styledpaper>
  )
}

export default test

これでwarningが消えてcssも適用された。

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?