JSX - Reactを学ぶなら知っておきたい、HTMLの表現手法

たにぐち まことのともすたチャンネル
4 Oct 202320:38

Summary

TLDRIn this informative video, the host, Tomostan Taniguchi, dives into the world of React, focusing on JSX, a syntax extension for JavaScript that allows for the inclusion of HTML-like elements in JavaScript code. The video is structured around a chapter from a book titled 'Starting with React Practical Introduction,' written by Yuta Yamada and published by SoftBank Creative. The host guides viewers through setting up a React project using Next.js, a framework that simplifies React development. He explains the basics of JSX, including its rules and syntax nuances, such as the use of className instead of class due to JavaScript's naming conventions. The video also covers how to dynamically insert variables into JSX, handle attributes, and apply styles. The host emphasizes the importance of understanding JSX for anyone looking to master React and Next.js, suggesting that it's a crucial skill for modern front-end development. He encourages viewers to experiment with JSX and to use available tools to convert HTML to JSX for easier integration into React-based projects. The video concludes with a recommendation to study the subject further, using resources like the mentioned book and the host's channel for ongoing learning.

Takeaways

  • 📚 The speaker introduces JSX, a syntax for embedding HTML in JavaScript, commonly used with React but not exclusive to it.
  • 🌟 JSX allows for a more integrated approach to writing HTML and JavaScript, making it easier to manipulate and interact with HTML elements within JavaScript.
  • 🛠️ JSX requires understanding specific rules, such as enclosing multiple elements within a single parent element like a `div` or using a React Fragment.
  • ⚠️ Be cautious with JSX as it behaves differently from HTML; for instance, self-closing tags in JSX need an explicit closing slash.
  • 🔍 Attributes in JSX are treated as JavaScript, so 'class' becomes 'className' to avoid conflicts with the JavaScript keyword 'class'.
  • 🔄 JSX enables dynamic output of attributes and styles, allowing for more interactive and data-driven UI components.
  • ✅ The use of `{...link}` in JSX allows for the spreading of an object's properties as attributes, simplifying the syntax for complex elements.
  • 💡 JSX comments are written using the JavaScript comment syntax, enclosed within `{/* ... */}` to avoid conflicts with HTML comment syntax.
  • 🎨 The speaker demonstrates embedding variables directly into JSX, showcasing the power and convenience of JSX for dynamic content.
  • ⛔ When embedding HTML within JSX, use the `dangerouslySetInnerHTML` attribute to prevent automatic escaping and allow raw HTML rendering.
  • 📈 The speaker recommends using JSX conversion tools for existing HTML to facilitate the transition to JSX, making it easier to adopt React and Next.js in new projects.

Q & A

  • What is JSX and how is it used in React?

    -JSX is a syntax extension for JavaScript that allows HTML to be written within JavaScript code. It's not exclusive to React but is commonly used with it. JSX makes it easier to manipulate HTML with JavaScript and is a key concept to learn when studying React.

  • What is the main difference between HTML and JSX?

    -While HTML and JSX may look similar, JSX is not strictly HTML. JSX gets transpiled into HTML by the React framework, allowing for the use of JavaScript expressions within the markup.

  • How does one begin setting up a React project with Next.js?

    -To start a React project with Next.js, you can use the command `npx create-next-app@latest` in the terminal. This command sets up a new Next.js application, which includes React as it is built on top of React.

  • What is the significance of the 'class' attribute in JSX?

    -In JSX, the 'class' attribute is not used due to it being a reserved word in JavaScript. Instead, 'className' is used as the attribute name for assigning classes to elements.

  • How can you include multiple elements within a single JSX return statement?

    -In JSX, you can wrap multiple elements with a single parent tag like `div`, or use a React Fragment (`<>...</>` or `<React.Fragment>`) to return multiple elements without adding extra nodes to the DOM.

  • What is the correct way to include comments in JSX?

    -Comments in JSX are written using the JavaScript comment syntax, enclosed within curly braces and wrapped with the HTML comment syntax: `{/* comment text */}`.

  • How can you embed variables directly into JSX?

    -In JSX, you can embed variables directly into the markup by enclosing the variable name in curly braces `{variableName}`. This allows for dynamic content to be rendered based on the variable's value.

  • What is the `dangerouslySetInnerHTML` attribute in JSX used for?

    -The `dangerouslySetInnerHTML` attribute is used in JSX to render raw HTML from variables without escaping. It is used with caution because it can expose the application to cross-site scripting (XSS) attacks if not handled properly.

  • How do you dynamically assign attributes to JSX elements?

    -Attributes in JSX can be dynamically assigned by embedding expressions within curly braces. For example, to dynamically set the `href` attribute of an anchor tag, you can write `{link}` where `link` is a JavaScript variable containing the URL.

  • What is the correct way to define and apply styles to JSX elements?

    -Styles in JSX can be defined using an object where the property names are written in camelCase to match CSS property names. This object can then be applied to a JSX element using the `style` attribute, like so: `style={styles}`.

  • What is the recommended approach for handling CSS in a React application?

    -It is recommended to use CSS classes and separate stylesheets for styling in React applications. While inline styles can be used, they can make the application harder to manage, especially as it grows in size and complexity.

  • What are the benefits of using Next.js for React development?

    -Next.js is a framework built on top of React that provides features like server-side rendering, API routes, dynamic imports, and automatic code splitting. It simplifies the development of complex applications and is considered a fast and efficient way to build React applications.

Outlines

00:00

📚 Introduction to JSX in React

The speaker, Taniguchi from Tomostation, introduces JSX, a syntax for embedding HTML in JavaScript, which is commonly used in React. They reference a book titled 'Starting Practical Introduction to React' by Yamada Yoshihiro, which covers React basics and Next.js app development. The speaker discusses JSX as a way to integrate HTML with JavaScript, making it easier to manipulate and work with in React applications. They also provide a step-by-step guide on setting up a React project using Next.js and the necessary tools like Node.js, ESLint, and Tailwind.

05:00

🔍 Understanding JSX and Its Syntax

The speaker explains the intricacies of JSX, highlighting that it is not pure HTML and has certain rules, such as requiring a single parent element to wrap multiple elements. They discuss the use of React Fragments to avoid adding extra HTML elements and the need to convert certain HTML attributes to their JSX equivalents, like 'class' to 'className'. The speaker also covers the correct way to include comments in JSX and how to handle self-closing tags.

10:00

🎨 JSX for Dynamic Content and Styling

The video demonstrates how JSX allows for the dynamic insertion of variables into the UI, with an example of displaying a user's name. It also covers the use of the 'dangerouslySetInnerHTML' attribute for embedding raw HTML. The speaker further explains how to dynamically assign attributes and styles to JSX elements, showcasing the use of objects and the spread operator to pass multiple attributes at once. They also touch on the need to convert CSS property names to camelCase when defining styles in JavaScript.

15:02

🌐 Practical Uses of JSX in Web Development

The speaker emphasizes the practical benefits of using JSX, such as the ability to integrate JavaScript variables and logic directly into the UI. They provide examples of how to use JSX for dynamic content, including linking to URLs and applying styles. The video also suggests using tools to convert existing HTML to JSX, which can be helpful for transitioning existing websites to use React and Next.js.

20:03

📚 Encouraging Learning of React and Next.js

The speaker recommends the book 'Starting Practical Introduction to React' for a thorough study of React and Next.js, considering their importance in modern front-end development. They invite viewers to subscribe to their channel for continued learning on React, Next.js, Astro, WordPress, and other related topics. The speaker thanks the audience for watching and encourages them to explore React and Next.js through the provided resources.

Mindmap

Keywords

💡JSX

JSX is a syntax extension for JavaScript that allows developers to write HTML-like code within their JavaScript files. It is widely used with React to define the structure and layout of components. In the video, JSX is introduced as a key concept for creating React components, allowing HTML to be embedded within JavaScript code, which simplifies the process of combining UI elements with logic.

💡React

React is an open-source, front-end JavaScript library developed by Facebook for building user interfaces, particularly for single-page applications. It is the basis for many web applications and is central to the video's discussion on JSX. React is mentioned as a technology that JSX complements, allowing for a seamless integration of UI elements within JavaScript code.

💡Next.js

Next.js is an open-source React framework that enables server-side rendering and provides features for building web applications with React. It is referenced in the video as a tool that simplifies the learning process for React by providing a structured environment for development. The script discusses using Next.js to create a new project and how it integrates with React.

💡Components

In React, a component is a modular piece of code that defines a part of the user interface. Components can be reused and combined to create complex UIs. The video emphasizes the importance of understanding components when learning React, as they are fundamental to building applications with React and JSX.

💡HTML

HTML, or HyperText Markup Language, is the standard markup language for documents designed to be displayed in a web browser. It is used in the video to contrast with JSX, illustrating how JSX allows HTML to be written within JavaScript files, which can streamline the development process.

💡CSS

CSS, or Cascading Style Sheets, is a style sheet language used for describing the presentation of a document written in HTML. In the video, CSS is discussed in the context of styling React components. It is shown how CSS can be applied to JSX elements, either through inline styles or by referencing classes defined in a stylesheet.

💡ESLint

ESLint is a pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. It is mentioned in the video as an option to include during the setup of a new Next.js project, indicating its role in maintaining code quality and adhering to coding standards.

💡Tailwind

Tailwind is a utility-first CSS framework for rapidly building custom designs. The video references Tailwind as a tool that can be included in a Next.js project to facilitate styling and layout design, enhancing the development workflow.

💡Variables

Variables in JavaScript are used to store, retrieve, and manipulate data. In the context of the video, variables are shown to be directly embeddable within JSX, allowing dynamic content to be rendered in the UI. This feature is highlighted as a convenience of using JSX over traditional HTML.

💡Attributes

Attributes in HTML are used to provide additional information about elements. The video explains how attributes work within JSX, noting that some HTML attributes have different names in JSX to avoid conflicts with JavaScript reserved words, such as 'class' becoming 'className'. It also shows how to dynamically assign values to attributes using JavaScript variables.

💡Fragment

A React Fragment is a component that lets you group a list of children without adding extra nodes to the DOM. The video discusses the use of fragments in JSX to wrap multiple elements, allowing them to be returned from a component function without creating unnecessary DOM elements.

Highlights

Introduction to JSX, a specification that allows embedding HTML within JavaScript, commonly used with React.

JSX is not exclusive to React but is fundamental to it, making it essential for anyone learning React.

JSX enables the combination of HTML and JavaScript, which traditionally were separate and had poor compatibility.

Demonstration of setting up a React project using Next.js, a framework that simplifies React learning.

Explanation of JSX syntax and how it differs from HTML, including the use of `className` instead of `class`.

JSX allows for the direct embedding of variables into the markup, making it highly versatile.

The use of `dangerouslySetInnerHTML` in JSX to embed raw HTML without escaping.

Attributes in JSX can be dynamically output, unlike static HTML attributes.

Introduction of React Fragments, which allow multiple elements to be returned without adding extra nodes to the DOM.

The special syntax for comments in JSX, which differs from HTML comment syntax.

The ability to define and apply styles dynamically within JSX, using JavaScript objects.

The importance of using tools to convert HTML to JSX for easier integration into React projects.

The convenience of JSX for developers familiar with HTML, despite initial learning curve.

The presentation of the book 'Starting with React Practical Introduction' by Yamada Yoshihiro, covering React basics to Next.js app development.

Emphasis on React and Next.js as essential subjects for front-end developers.

Encouragement for viewers to experiment with JSX and integrate it into their projects for practical experience.

The presenter's intention to continue providing information on React, Next.js, Astro, and WordPress in future content.

A call to action for viewers to subscribe to the channel for more updates on React and related technologies.

Transcripts

play00:05

こんばんは、ともすたのたにぐちです。

play00:06

今日はReactの話をしていきたいと思うんですけれども

play00:09

その中でもですねこちらJSXというものについて

play00:13

ご紹介していきたいと思います

play00:15

今日はですねこちらの書籍

play00:16

これから始めるReact実践入門

play00:19

コンポーネントの基本からNext.jsによるアプリ開発まで

play00:22

というですね

play00:23

山田 祥寛さんが執筆された

play00:25

ソフトバンククリエイティブさんの書籍

play00:27

こちらが発売になったんですけれども

play00:29

こちらですね一冊いただきましたので

play00:32

参考にさせていただいて

play00:33

今日はですねその中でチャプター2のところでですね

play00:36

JSXの基本というところがあるので

play00:39

そちらをですね参考にさせていただきながら

play00:41

こちらの講座作らせていただきました

play00:43

ありがとうございます

play00:44

ということでこの JSXというものなんですけれども

play00:46

これ何かと言いますとJavaScriptの中に

play00:49

HTMLを組み込むことができるという規格になります

play00:52

Reactだけのものではないんですけれども

play00:54

基本的にはこのReactでよく使われる文法になるので

play00:59

Reactを学習するときにはこのJSXについてもですね

play01:02

慣れていかないといけないよということになります

play01:05

要は、従来はHTMLとJavaScriptというのは

play01:08

別々のものですので非常にこう相性が悪くてですね

play01:12

HTMLを書いてJavaScriptを書いてということをしていたんですけれども

play01:16

それをこの中に組み込むことができちゃいますよ

play01:19

というのが JSX というものですね

play01:21

ですので非常にこう扱いやすくなりますし

play01:23

JavaScriptで操作しやすくなるというのが特徴ということですね

play01:28

ではちょっとさっそく触りながらですねやっていきたいと思うので

play01:31

まずReactを準備していきましょう

play01:35

こちらですねターミナルを準備していきます

play01:37

macOSの方はアプリケーションの中の

play01:39

ユーティリティのターミナルですね

play01:40

Windowsの方はスタートボタンを右クリックして

play01:43

Microsoft Terminalこちらを起動しておいてください

play01:46

Node.jsが必要なので、Node.jsのインストールが終わったところから

play01:50

スタートしていきたいと思います

play01:52

こちら np m の -v としてバージョン番号が出れば OK ですね

play01:55

まだインストールしてない方は「nodejs インストール」とか

play01:58

検索していただければすぐ出てくるかと思います

play02:01

ではやっていきましょう

play02:02

npx create-next-app@latest と入力していきます

play02:09

今ですねReactを学習する時に

play02:12

ここnext-appとしていまして

play02:14

これNext.jsっていうフレームワークを使ったものなんですけれども

play02:17

今、Reactを学習する時には

play02:20

基本的にはこのNext.jsを入れて学習していただくのが

play02:23

手っ取り早いかなというところがありますので

play02:26

ReactとNext.jsは同じようなものだと

play02:28

考えていただければと思います

play02:30

ではこれでEnterしていきましょう

play02:32

で OK ですかということですね

play02:34

まずプロジェクトの名前を入れていきます

play02:36

next-appとかとしましょうか

play02:38

好きな名前で結構です

play02:40

TypeScript使いますかということですね

play02:42

今日はどっちでも大丈夫ですマ

play02:43

Noのままいきましょうか

play02:44

ESLint使いますかですね

play02:46

これ Yes にしておきましょうかね

play02:48

Tailwind使いますかですね

play02:49

まあこれも Yes にしておきましょう

play02:51

srcディレクトリー使いますかですね

play02:53

これも Yes にしておきましょうか

play02:55

Appルーター使いますかは Yes にしておきます

play02:57

さあこんな感じですかね

play02:58

customize the default import aliases

play03:01

これは No のままで結構です

play03:02

こんな感じでですね質問に答えていって

play03:05

プロジェクトを作っていきます

play03:06

これで準備できましたら next-app という

play03:10

ディリクトリーができあがっていますので

play03:12

これをですね Visual Studio Code で開いていきましょう

play03:14

code . ですね

play03:17

これで今作ったディリクトリーを

play03:20

Visual Studio Codeで開くことができました

play03:22

さあそしたらですねこちら見ていきましょう

play03:25

src フォルダーの中のappフォルダーの中ですね

play03:28

この中に page.js というファイルがあるんですけれども

play03:32

ここがですねトップページの HTML を司っているところになります

play03:36

これをブラウザーで見ていきましょう

play03:38

ターミナルを起動して

play03:39

ここで npm run dev と入力していきます

play03:47

そうするとですねこちら

play03:48

ローカルサーバーですね起動するので

play03:50

これをですね見ていきましょう

play03:53

こんな感じでページが表示されました

play03:56

こちらの page.js が表示されている状態ということですね

play04:01

さあではですねちょっとここでやっていきたいと思うんですけれども

play04:04

今ですねまあここに書かれているのが

play04:06

実際には JSX なんですが

play04:08

ちょっと一旦全部消しちゃいたいと思います

play04:10

この5行目のところですね main タグというのが始まっているので

play04:13

この辺全部消していっちゃいましょう

play04:15

ざーっと消していきます

play04:19

ここですね main タグ閉じるのところまで消していきましょう

play04:23

あと CSSがちょっとかかっているんですね

play04:26

これで今ちょっと何もないですよということで

play04:28

エラーが出ていますけれども

play04:29

あとこちらですね globals.css というところでですね

play04:33

CSS がかかっているので

play04:34

これもちょっと消していきたいと思います

play04:37

4行目から下ですね

play04:38

これを全部消しちゃいましょう。

play04:40

さあではこれでですね実際にやっていきましょう

play04:43

page.js の方ですねこちらにこんなふうに書いていきます

play04:47

p タグですね

play04:48

で例えば「こんにちは」としていきましょう

play04:51

で保存します

play04:52

さあそうするとですね、これだけで

play04:53

ページに表示されました

play04:55

今ですねここよく見ていただくとわかるんですけれども

play04:58

これ HTML のファイルではありません

play05:00

そもそもが .js のファイルですよね

play05:02

かつ3行目にですね

play05:03

export default function Home() などと書かれていて

play05:06

その関数というのが始まっていますけれども

play05:10

JavaScriptの内部なんですよね

play05:12

ですが、ここにこのように普通に HTML を書くと

play05:15

そのままですね画面に表示されるよというのが

play05:18

JSX というものになります

play05:20

実際ですね生成される HTML もこんな感じ

play05:23

きれいな p タグが生成されています

play05:26

なんでこんなことができるのかというのは

play05:28

普通にですね JavaScript を作っていたら

play05:30

こんなことは、もちろんできないんですけれども

play05:31

今 Next.js というフレームはこう使ってですね

play05:34

中に React が入っていますので

play05:37

それがですねこちらを処理してくれているよということになります

play05:40

で JSX はですね HTML とは厳密には違うので

play05:44

ちょっとですね差があるんですね

play05:46

例えば、ではここにですねもう1つ p タグを入れまして

play05:50

「ともすたへ」などとしましょう

play05:52

保存します

play05:54

そうするとですねこれエラーになっちゃうんですね

play05:56

何かと言いますと

play05:57

Unterminated regexp literalと書かれているんですけれども

play06:02

何のこっちゃかもしれないんですが

play06:03

これ終わってないですよということですね

play06:06

どういうことかと言いますと

play06:07

まずですね JSX の基本的なルールとして

play06:10

この return の中にかける HTML の要素は1つだけ

play06:13

というルールがあります

play06:15

今ですねこれ p タグ1つあるんですけれども

play06:17

1つの時はこれルール違反ではありません

play06:20

問題ありません

play06:21

なんですが2つ書くとですね

play06:23

これ2つの要素がリターンの中に入ってしまっているので

play06:27

これはダメですよということになります

play06:29

いくつかやり方が

play06:30

どうするかなんですけれどもあります

play06:32

例えば1つのやり方としては

play06:33

このdiv というタグですね

play06:36

こういったレイアウトに影響しないタグでですね

play06:39

全体を囲ってあげる

play06:40

そうするとこのようにきちんと表示されるようになります

play06:43

ただこれはですねちょっとデメリットもありまして

play06:46

実際に生成される HTML の方ですね

play06:48

こちらにもこの div タグというのができてしまうんですね

play06:51

特にレイアウトとかに影響はないんですけれども

play06:53

ただですねちょっとこう気持ち悪いところがあったりですとか

play06:56

まあCSSによっては影響が出てきてしまう可能性があります

play07:00

こういう場合どうするかなんですけれども

play07:02

1つのやり方としてですね

play07:04

React Fragmentというんですけれども

play07:06

こういうですね非常に特殊なタグが使えます

play07:09

これですね、 、 という形で

play07:13

HTML のタグとも言えないようなものなんですが

play07:16

これで囲ってあげると

play07:18

ちゃんとですねエラーにならないですね

play07:20

これはですね HTML のソースを見ても

play07:22

何もですね出力されていません

play07:24

JavaScript 特に React の方でですね

play07:26

これはちゃんと処理してから出力してくれるので

play07:29

こういうふうにきれいな HTML になってくれます

play07:32

これですね実はあるものの省略でして

play07:34

っていうですねちょっと特殊なタグがあって

play07:39

これのですね略称なんですけれども

play07:41

Next.jsの場合ですね

play07:43

この React っていうオブジェクトが標準ではないので

play07:45

not defined ということで

play07:47

エラーになってしまうんですね

play07:48

Reactっていうのをここでロードしてあげればいいんですけれども

play07:51

この省略系の方が使いやすいかなと思いますので

play07:55

省略系を使っていただいていいかなと思います

play07:58

ということで複数のですね要素を JSX で表現したいときには

play08:02

この React Fragment ですね

play08:03

それか divタグとかarticleタグとか

play08:06

そういったレイアウト系のタグをですね

play08:07

1つ入れてあげるといいかなというところがあります

play08:10

さあもう1つがですね

play08:12

例えばこう hr というタグを使ってですね

play08:14

罫線ですねを引きたいという場合なんですけれども

play08:17

これもですねエラーになってしまいます

play08:19

これ何かと言いますと

play08:21

このいわゆる空要素ってやつですかね

play08:23

終了タグがないタグがあるんですけれども

play08:25

その場合ですねこの HTML の普通の書き方ではできません

play08:29

XHTML って昔の規格があるんですけれども

play08:33

その書き方をしないといけないんですね

play08:34

/> でこうするとですね

play08:37

ちゃんとこのように罫線が出ているんですけれども

play08:39

空要素の時はですね

play08:41

この /> 記号で閉じてあげるという

play08:44

XHTML の書き方をしてあげましょうねということになります

play08:48

続いてがですね属性にもいくつかですね変化があるんですね

play08:51

例えばよく使うのがこの class っていう属性よく使うかと思います

play08:55

例えば greeting とかとしましょう

play08:57

でじゃあこの greeting というものに対してですね

play08:59

こう文字を赤くしたいなということであれば

play09:02

globals.css の方にですね

play09:04

こちら greeting というクラスを

play09:08

これですね color を red にしますよとかって書きました

play09:12

そうするとこう赤が反映されるわけなんですけれども

play09:15

実はですねこれちょっと正しくありません

play09:18

正しく画面には表示されているんですけれども

play09:20

デベロッパーツールの方で見るとですね

play09:22

ちょっとおかしいなことが起こります

play09:24

デベロッパーツールでここですねエラーが1件起こってます

play09:27

見ていくとこんなエラーですね

play09:30

DOM property 'class' が invalid 正しくないですよということですね

play09:35

でもしかしたら className のことですかと

play09:37

書かれているんですけれども

play09:38

これもですね JSX の特徴の1つですね

play09:41

class っていうのが JavaScript の用語と被ってしまっているので

play09:45

使えません

play09:46

なので JSX では class は className という名前に変わっています

play09:50

なので普通に className に変えてあげるだけなんですけれども

play09:56

こうですねで、赤いままきちんと表示されていて

play09:58

これだと CSS のですね

play10:00

エラーも表示されなくなるよということになります

play10:02

画面がですねうまく表示されちゃうので

play10:04

ちょっとですね見落としがちなんですけれども

play10:07

プログラムとか組んでいくとですね

play10:08

エラーになったりしますので気をつけてください

play10:11

他にもですねいくつかありまして

play10:12

for 属性が使えないので

play10:14

htmlFor になったりとかですね

play10:16

いくつか変化する場所があります

play10:18

でこのあたりはですね HTML で普通に組んでしまったものを

play10:22

JSX にしたいなという場合には

play10:24

これをですねそれぞれ変更しないといけないんですけれども

play10:27

こういったものはですね結構ツールが提供されています

play10:30

例えばこれ transform というちょっと適当なですね

play10:32

サービスを探しただけなんですけれども

play10:34

HTMLをこちらに貼り付けてあげると

play10:37

こっちにですね JSX として吐き出したものというのが出力されます

play10:40

例えば class のところが className に変わったりですね

play10:43

style が書き方が変わったりですとか htmlFor に変わったりですとか

play10:47

そういった形でですね JSX で正しい書式に書き換えてくれるので

play10:52

こういったツールを使ってあげるのが

play10:54

一番早いかなと思いますね

play10:55

さあそしてコメントの入れ方もですね

play10:58

ちょっと違うんですね

play10:59

HTML ではコメントをこのようにですね

play11:06

コメントを入れることができるんですけれども

play11:09

これですね残念ながらこちらではエラーになってしまいます

play11:13

なぜかと言いますとこの ! とかがですね

play11:16

JSX では使えないので

play11:17

じゃあどうするかですが

play11:18

こんなコメントになります

play11:20

{/* そしてこちらは */} という形になります

play11:28

これ JavaScript のですねコメントですね

play11:31

こちら HTML に見えてるんですが

play11:33

今この中って実は JavaScript ですので

play11:35

JavaScript のコメントを書くようということになります

play11:39

ちょっとユニークなのが

play11:40

実はこのタグの中とかにですね

play11:42

このようにコメントを書くことができるんですね

play11:44

// とかとして p タグとかって書いてあげると

play11:48

これですねコメントとしてきちんと機能しています

play11:51

こうですね特に変化はないんですけれども

play11:53

こんな書き方ができるのも JSX っていうのが

play11:56

HTML に似ているんだけれども

play11:58

実はちょっと違うよというところの一つかもしれません

play12:02

ということでまあ JSX ですね

play12:03

いくつかちょっと違いがあるので

play12:05

HTML に慣れている方だとですね

play12:07

JSX ちょっとこう違和感があったり

play12:09

気持ち悪いなって感じるところはあると思うんですけれども

play12:12

JSX をですね React ですとかこの Next.js で使えるようになると

play12:15

どんなところが便利かといいますと

play12:17

まず1 つが変数をそのまま埋め込むことができるよということですね

play12:22

例えばちょっとやってみましょう

play12:23

こちらですね上のところで、これいらないですね消しちゃって

play12:27

const としていきますね

play12:29

で name として自分の名前例えば入れたとしましょう

play12:34

そしたらこの name っていう変数をですね

play12:37

画面に出力したいなと思いました

play12:39

その場合ですね

play12:40

これまでの HTML を JavaScript で操作するとかですと

play12:44

まあここに id 属性を振ってですね

play12:46

で getElementById とかでですね

play12:48

いろいろこう書いていかないといけなかったんですけれども

play12:51

こちら JSX をですね使っている場合は

play12:53

こんな風に書くことができます

play12:55

これで画面を出力すると

play12:58

ここがちゃんと たにぐちまこと さんになっていますよね

play13:01

この {name}、マスタッシュ構文とかよく言いますけれども

play13:05

{ } で変数の名前を囲ってあげると

play13:07

文章の中にこの準備したですね

play13:10

変数をそのまま埋め込むことができますよというのが

play13:14

JSX の非常に便利なところかなと思います

play13:17

ただですねここもちょっと注意が必要なのが

play13:19

例えばですねこの中に HTML を組み込んだとします

play13:22

ここで として太字にしましょうということですね

play13:27

そうするとですねこちら埋め込んだ状態だと

play13:30

ここですね まこと さんということで

play13:33

タグがですねそのまま出力されます

play13:35

これいわゆるエスケープというのがされているという状態ですね

play13:38

HTMLをエスケープしないでそのままですね

play13:42

表示したいなという場合にはこんな風に書くことができます

play13:45

例えばここですね {name} さんのところを

play13:47

タグで囲っていきましょうか

play13:51

dangerouslySetInnerHTML

play13:57

ちょっと長いですね

play13:58

このように dangerouslySetInnerHTML というものにですね

play14:03

入れることができるんですけれども

play14:05

__html: name と入れていきます

play14:11

とこういう形で書いていきます

play14:15

そうするとですねこれ今保存すると

play14:17

ここ まこと というところがですね

play14:19

太字になっているのでお分かりの通り

play14:20

この タグのですね

play14:21

この中に HTML がそのまま入って

play14:24

これはエスケープされないよということになります

play14:26

このですね dangerouslySetInnerHTML という属性の部分ですね

play14:31

このように書く必要があるよということです

play14:33

これはもう決まり文句としてですね

play14:35

覚えてしまうと早いかなと思います

play14:37

また逆に言うとですね

play14:38

基本的にはここでエスケープされて表示されるので

play14:41

安全性が高いよということでもあります

play14:43

どんどん行きましょう

play14:44

続いて属性もですね動的に出力することができます

play14:48

例えばではここではですね const link としましょうか

play14:52

https://tomosta.jp と入れていきますね

play14:55

このリンクという変数ですね

play14:58

これを使っていきたいと思うんですけれども

play15:00

例えばこの「ともすたへ」というところにですね

play15:02

リンクを貼っていきましょう

play15:03

href ですねこの属性のところに

play15:06

今のものを埋め込んでいきます

play15:07

linkですね「ともすたへ」のところを タグで囲っていきましょう

play15:11

今ちょっと色が変わらないので分かりにくいんですけれども

play15:14

「ともすたへ」 のところにリンクが張られていて

play15:17

ともすたのサイトにリンクが張られています

play15:19

さあこれちょっともう一度見ていきますと

play15:21

このリンクというところに URL が入っていますよね

play15:24

属性のところなんですけれども

play15:28

でここに {link} を埋め込んでいると

play15:31

気をつけないといけないのがここですね

play15:33

HTML の属性なので " で囲いたくなってしまうんですが

play15:37

これ囲うとですねうまく動かなくなります

play15:40

リンクがこう張られてない状態ですね

play15:42

なので変数を埋め込むときには

play15:44

" で囲わないというのが約束になります

play15:47

これですね囲わなくても HTML 出力されている時は

play15:50

" が自動的に入っているのでご安心ください

play15:53

このようにですね属性を動的に入れることができるんですけれども

play15:57

JSX はですねもっと便利な機能があります

play15:59

まとめて入れることができるんですね

play16:01

例えばちょっとここを変えていきましょう

play16:03

リンクをですねオブジェクト形式にしていきます

play16:06

例えば href属性これにですね https://tomosta.jp を入れる

play16:11

そして例えばtarget属性ですね

play16:14

これブラウザーを新しく開くかどうかみたいのを

play16:17

決めることができるんですけれども

play16:18

これを _blank ということで新しいタブを開きますということですね

play16:22

例えばtitle属性をですね

play16:23

ともすた などにしておきましょうということで

play16:26

さあこれでですね

play16:27

今この link っていう中にたくさんのですね

play16:29

属性が一気に入った状態になります

play16:32

でこれをですね

play16:33

ここに出力していきたいんですけれども

play16:34

この時ですねこんな書き方ができます

play16:37

{...link} としていきましょう

play16:41

さあこれで見ていきます

play16:42

そうするとここですね

play16:44

リンクは張られています

play16:45

HTML のソースを見ていきましょう

play16:47

そうするとhref属性 target属性と title属性ですね

play16:51

それぞれこのようにきちんと入っています

play16:53

今のですねちょっともう一度見ていきますと

play16:55

この書き方ですね {...link} というふうに書いて

play17:00

このリンクというのが

play17:01

上のところでオブジェクトとして定義されています

play17:04

hrefとtargetとtitleそれぞれを定義しているんですけれども

play17:08

これによってですね

play17:09

それをまとめてここに書き出すことができるよということになります

play17:12

例えばですねいわゆる論理属性ですかね

play17:15

属性名だけを書くっていうのがあったかと思うんですけれども

play17:18

例えば disabled というやつですね

play17:21

書き出したい時はこれ true というのを値を入れてあげます

play17:24

そうするとですねこちらの出力されるタグの方も

play17:27

ここですね disabled属性がこのように表示されています

play17:31

当たり前ですけれどもここですね false にしてあげると

play17:34

生成される HTML からは属性名も外れていくよということですね

play17:38

ここでですね属性も動的に制御することができますので

play17:42

まあこの辺でですねプログラムであらかじめまとめておいて

play17:44

まあそれを HTML の方に出力するという形ができます

play17:48

あとスタイルシートもですねできます

play17:50

const styles = {

play17:53

例えば color をここでは blue にしましょうと

play17:57

fontSize を例えば 1.5rem などとしましょうと

play18:02

ここまでですねまたちょっと書き方が特殊なので

play18:05

気をつけていただきたいんですけれども

play18:07

まずですねここですね fontSizeですね

play18:09

CSS のプロパティだとこれ font-size と - 区切りなんですけれども

play18:14

- 区切りがですね JSX の中では使えません

play18:17

fontSize とこのキャメルケースっていうんですけれども

play18:19

二文字目が大文字になるという書き方ですね

play18:22

このように書いていく必要があります

play18:24

そうするとこの styles というところでですね

play18:26

スタイルシートが定義されているので

play18:28

先ほどのこの部分ですかね

play18:30

ここに style= として styles というふうに書いていきます

play18:35

さあそうするとこちら CSS が効きましたね

play18:37

検証するときちんとですねここの p タグのところに

play18:40

color: blue だよと

play18:41

で font-size= 1.5rem だよと

play18:43

font-size というプロパティもちゃんと - 区切りに変わっています

play18:46

こんな風にですねスタイルシートも

play18:48

動的に吐き出すことができますよということですね

play18:51

実際にはですねこちらスタイルシートですね

play18:53

インラインで書いてしまうと

play18:54

色々と扱いにくくなってしまいますので

play18:56

先ほどみたいにですね

play18:57

この className の方で class を割り振って

play19:00

それを CSS のファイルでですね

play19:01

こう書いていくというのがいいかなと思いますけれども

play19:04

とはいえなんかプログラムでですね

play19:05

色を変えたりするときにはこういった書き方もいいかなと思います

play19:09

ということでいかがだったでしょうか

play19:10

JSX ちょっとですね最初は非常に扱いにくいなと

play19:13

いうふうに感じるところはあるとは思うんですけれども

play19:16

とはいえまこの JavaScript ですね HTML をいじれるというのは

play19:19

非常にですねメリット大きいかなと思いますので

play19:22

まずはですね少しいじってみていただいて

play19:24

慣れてみていただくといいかなと思います

play19:26

HTMLで組んでしまったものは

play19:28

先ほどの通りこの HTML を JSX に変換するツールですね

play19:31

こういったものを使っていただくと変換できますので

play19:34

それで既存のですねウェブサイトとかも

play19:36

こちらの JSX に持ってきて

play19:37

Next.js 化するというような

play19:39

プロジェクトもやってみるといいかもしれません

play19:41

ということでですね今回改めてご紹介すると

play19:44

ここでから始める React 実践入門

play19:46

コンポーネントの基本から Next.js によるアプリ開発まで という書籍

play19:50

今日はですねもう本当に一部ですねご紹介をしました

play19:53

書籍ですね今私の手元に届いているのが

play19:56

非常に分厚い書籍となっていて

play19:57

かなりですねじっくりと勉強できる書籍になっています

play20:01

まあ React と Next.js はですね

play20:03

今フロントエンドをやるには

play20:04

結構必須の科目になっていますので

play20:06

ぜひですねちょっとこの機会に手に取っていただいて

play20:08

学習してみていただければなと思います

play20:10

僕の動画でもですね

play20:12

引き続き React ですとか Next.js

play20:13

あとまあ Astro とかですね

play20:15

WordPress とかそういった情報もお届けできればと思いますので

play20:18

興味ありましたらぜひチャンネル登録していただければと思います

play20:21

ということで本日もご視聴ありがとうございました

Rate This

5.0 / 5 (0 votes)

Related Tags
ReactJSXWeb DevelopmentNext.jsFrontendHTML IntegrationJavaScriptCSSCoding TutorialDeveloper ToolsProgramming