Penghua Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
###### tags: `React` > Update: 2020/09/16 [name=鵬化] [toc] # 什麼是 JSX? 今天 React 好朋友要帶我看看在 React 中很常使用的語法: **JSX**,這個語法是一個 JavaScript 的語法擴充,而且是在寫 React 的時候,官方推薦使用的語法。 JSX 語法的重點在於**允許我們在 JS 的檔案中使用 HTML 的標籤,並且使用 JSX 語法建立的是「==一個 React 的 element==」,此外這樣的標籤語法比起 HTML,更貼近於 JavaScript。** 趕緊來看看怎麼使用吧! ## 怎麼使用 JSX 語法 ### 使用 JSX 的基本方式 相關測試範例,[點擊前往](https://codesandbox.io/s/jsx-demo-jvwfr)。 在 React 中,我們可以這麼寫: ```jsx= const element = <p>Hello World!</p>; ``` 而上面方式就是一個簡單使用 JSX 語法的方式。 ### 將變數傳入 JSX 語法中 接著,由於我們是在 JavaScript 中撰寫,所以我們可以**在 JSX 語法撰寫的程式碼中寫入 JavaScript 表達式**,如同下面程式碼中的 `name`: 以下擷取範例來自官方文件: [在 JSX 中嵌入 Expression ](https://zh-hant.reactjs.org/docs/introducing-jsx.html#embedding-expressions-in-jsx) ```jsx= // 將變數 name 傳入至 element 中的方式,在 JSX 中是被允許的。 const name = 'Josh Perez'; const element = <p>Hello, {name}</p>; ReactDom.render( element, document.getElementById('root') ); ``` ### 在 JSX 中執行函式 當然也可以在 JSX 中執行一個函式並得到一個回傳的結果值: ```jsx= function formatName(user) { return user.firstName + ' ' + user.lastName; } const user = { firstName: 'Harper', lastName: 'Perez' }; const element = ( <h1> Hello, {formatName(user)}! </h1> ); ReactDom.render( element, document.getElementById('root') ); ``` 而這邊需要提醒的是變數 `element` 的值被 `()` 括起來,但 `()` 並不是 JSX 語法的一環,而是當我們有多行 JSX 的語法時,需要**透過括號的方式來避免自動分好補足的麻煩。** ### 在條件判斷回傳中使用 JSX 在某些條件下,我們需要不同的 DOM,而 JSX 語法也允許我們將其作為參數並透由函式回傳。 ```jsx= function formatName(user) { return user.firstName + ' ' + user.lastName; } function getGreeting(user) { return user ? <p>Hello, {formatName(user)}!</p> : <p>Hello, Stranger.</p>; } const user = { firstName: 'Harper', lastName: 'Perez' }; const element = getGreeting(user); ReactDom.render( element, document.getElementById('root') ); ``` 例如 `const element = getGreeting(user);` 的 `user` 傳入為 `undefind`,所以會取得 `getGreeting` 回傳值為 `<p>Hello, Stranger.</p>`。 關於 JSX 的使用就先到這裡,接著要來看看使用 JSX 的一些限制 ## 使用 JSX 時需要注意的限制 ### HTML 屬性在 JSX 中的表示方式 A、首先第一點是由於 **React DOM 使用 camelCase 作為屬性命名規範**,這代表像是說在 HTML 中的 `tabindex`,在 JSX 中則必須寫成 `tabIndex`。 B、接著第二點是像是在 HTML 中的 **`class`, `label` 的 `for`,由於在 JS 中是保留字(preserve word)**,所以需要額外改寫成 **`className` 與 `htmlFor`**。 C、但是凡事總是會有例外,諸如 `aria-*`, `data-*` 則**不需要使用 camelCase 作為屬性命名規範** 以上 React 會幫我們將上述提到的部分在編譯時轉換成在 HTML DOM 上其對應的屬性。 ### JSX 一定要有一個根元素(one single root element) 在使用 JSX 的時候,**是至少需要一個根元素的**,否則就會報錯。 ## 副檔名要用 .js 還是 .jsx? 關於 JSX 部分最後要提的是一個也許在研究過程中會遇到的困惑: **「在 React 中為什麼有些檔案的副檔名除了 .js 之外還有 .jsx 呢?」** 這個問題我也是很困惑,索性就爬文查看看差異處在哪,然後就找到了這一篇 [stackoverflow](https://stackoverflow.com/questions/46169472/reactjs-js-vs-jsx/46169521#46169521) 的發問,摘錄出最佳回答的回覆: > There are however some other considerations when deciding what to put into a .js or a .jsx file type. Since JSX isn't standard JavaScript one could argue that anything that is not "plain" JavaScript should go into its own extensions ie., .jsx for JSX and .ts for TypeScript for example 大致重點在於**使用 .jsx 作為副檔名的判斷依據在於認為是否為標準的 Javascript。** 由於 JSX 並不是標準的 Javascript,所以就透過以 .jsx 為副檔名來管理這類的檔案。 就好像使用 Typescript 會用 .ts 的副檔名一樣。 所以在實作上都可以成功運作,但怎麼管理就讓大家自己思考囉! ## `React.createElement()` 今天最後要提的部分是 `React.createElement()` , **JSX 可以說是這個語法的語法糖**, 因為在官方文件的描述中有提到: > Babel 將 JSX 編譯為呼叫 React.createElement() 的程式。 此外,這個語法提供了三個參數可以使用: ```jsx= React.createElement( type, [props], [...children] ); ``` | 參數 |說明| 是否必填 | |------|------|------| | type | 設定 HTML 標籤 | 必填 | | props | 設定屬性 | 選填 | | children | 設定子節點 | 選填 | 接著讓我們來將上面的程式碼透過 `React.createElement()` 改寫: 首先是剛剛在上方透過 JSX 語法方式所寫的程式碼: ```jsx= const element = <p>Hello World!</p>; ``` 接著是透過 `React.createElement()` 所寫的程式碼: ```jsx= React.createElement( 'p', null, 'Hello World' ); ``` 那如果今天想要呈現如下的 DOM 結構呢? ```htmlmixed= <div class="container"> <p>這是一個子節點</p> </div> ``` 這樣的話,如果以使用 `React.createElement()` 達成目的的話,就需要調用 `React.createElement()` 兩次來建立: ```jsx= // 建立一個 p 標籤 const element = React.createElement( 'p', null, '這是一個子節點' ); // 建立一個 div 標籤,並有一個 p 標籤的子節點 const element2 = React.createElement( 'div', { className: 'container' }, element ); ``` 今天的學習應該可以解惑昨天部分程式碼了吧! 透過 JSX 語法的方式可以讓我們寫出更好維護的 HTML 架構,輕鬆許多呢! 明天 React 好朋友要帶我們了解關於「React Element」與「React Component」的相異之處。 鐵人賽文章與程式碼同步發佈於: 1. [個人部落格](https://penghuachen.github.io/) 2. [Github](https://github.com/penghuachen/React_30) 明天見~ ## 相關資源 - [在 JSX 中嵌入 Expression ](https://zh-hant.reactjs.org/docs/introducing-jsx.html#embedding-expressions-in-jsx) :::danger ### 後續回過頭學習 - [深入 JSX](https://zh-hant.reactjs.org/docs/jsx-in-depth.html) :::

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully