1
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.

【React】JSXの{{}}

Posted at

JSXの{{}}について

ReactのJSX内で二重波括弧 {{}} を使用する場合、これはオブジェクトのリテラルを埋め込むために使用されます。

使用方法

例えば、スタイルをインラインで設定する場合によく使われます。一重の波括弧 {} の中にオブジェクトを記述し、その中でCSSプロパティと値のペアを指定します。

import React from 'react';

function MyComponent() {
  const styles = {
    color: 'red',
    fontSize: '20px',
    fontWeight: 'bold'
  };

  return (
    <div style={styles}>
      This text has custom styles.
    </div>
  );
}

export default MyComponent;

上記の例では、styles という名前のオブジェクトを作成し、colorfontSizefontWeight のプロパティを指定しています。このオブジェクトを style 属性の値として設定することで、その要素に指定されたスタイルが適用されます。

まとめ

二重波括弧を使用することで、外側の波括弧 {} はJavaScriptの式として解釈され、内側の波括弧 {} はオブジェクトのリテラルとして解釈されます。

1
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
1
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?