React 框架

React 框架

將 React 加入到網頁

react感覺很好玩,使用react來寫網站的話,可以做到HTML 裡面就一行簡單的DOM DIV,其他都寫在JS裡面,這非常的好玩,react使用到jsx,可以直接將html個是寫在js裡面,不用擔心斷行的問題。

直接使用CDN來掛載React

html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Add React in One Minute</title>
  </head>
  <body>

    <h2>Add React in One Minute</h2>
    <p>This page demonstrates using React with no build tooling.</p>
    <p>React is loaded as a script tag.</p>

    <p>
      This is the first comment.
      <!-- We will put our React component inside this div. -->
      <div class="like_button_container" data-commentid="1"></div>
    </p>

    <p>
      This is the second comment.
      <!-- We will put our React component inside this div. -->
      <div class="like_button_container" data-commentid="2"></div>
    </p>

    <p>
      This is the third comment.
      <!-- We will put our React component inside this div. -->
      <div class="like_button_container" data-commentid="3"></div>
    </p>

    <!-- Load React. -->
    <!-- Note: when deploying, replace "development.js" with "production.min.js". -->
    <script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

    <!-- Load our React component. -->
    <script src="like_button.js"></script>

  </body>
</html>

JS

'use strict';

const e = React.createElement;

class LikeButton extends React.Component {
  constructor(props) {
    super(props);
    this.state = { liked: false };
  }

  render() {
    if (this.state.liked) {
      return 'You liked comment number ' + this.props.commentID;
    }

    return e(
      'button',
      { onClick: () => this.setState({ liked: true }) },
      'Like'
    );
  }
}

// Find all DOM containers, and render Like buttons into them.
document.querySelectorAll('.like_button_container')
  .forEach(domContainer => {
    // Read the comment ID from a data-* attribute.
    const commentID = parseInt(domContainer.dataset.commentid, 10);
    ReactDOM.render(
      e(LikeButton, { commentID: commentID }),
      domContainer
    );
  });

範例: 點我進入

官網連結 : https://zh-hant.reactjs.org/docs/add-react-to-a-website.html

夾尾巴

不知道甚麼端的工程師,低薪努力中

Related Posts

寫sass 新增config.rd 的方便

寫sass 新增config.rd 的方便

Swiper.js 輪播JS 套件

Swiper.js 輪播JS 套件

css-doodle 類CSS系統套件 可用來繪圖

css-doodle 類CSS系統套件 可用來繪圖

fabric.js-HTML5 Canvas 加強套件

fabric.js-HTML5 Canvas 加強套件

No Comment

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *