5
2

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.

Reactの`className`って何?

Posted at

classNameってよく見かけますよね?
今回は、このclassNameについて、初心者の方でもピンとくるように解説します。

1. まず、classclassNameの違いって?

HTMLでスタイルを指定するとき、class属性を使いますよね。でも、ReactのJSX(JavaScriptで書くHTMLみたいなもの)では、classじゃなくてclassNameを使います。なぜかって?JavaScriptの中でclassは特別な意味があるからなんです。

👉 ポイント: Reactでスタイルのクラスを指定するときは、classNameを使う!

2. classNameの使い方

超簡単!文字列でクラス名を書きます。

<div className="my-style">
  こんにちは、React!
</div>

3. 複数のクラスを指定したいときは?

スペースで区切ればOK!

<div className="style1 style2">

4. 条件によってクラスを変えたい!

こんな風に、条件を使って動的にクラス名を変えることもできるんです。

const isBlue = true;

<div className={`box ${isBlue ? 'blue' : 'red'}`}>

👉 ポイント: バッククォートと${}を使うと、変数の中身を文字列に埋め込める!

5
2
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
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?