跳到主要內容

第一次學 React 的簡單範例

第一次學 React 難免會覺得有點不習慣,寫個範例來幫忙記憶 & 了解吧.........

Result(結果):
Your Name

HTML:
<div class="content"></div>

JS (記得額外載入 react.js & ReactDOM.js)

// 第一個元件 CommentList (父元件)
var CommentParent = React.createClass(
{
  render:function(){
     return(<CommentSon author="Your Name"></CommentSon >) // 這裡是實作呼叫子元件
}})

// 第二個元件 CommentSon (子元件)
var CommentSon = React.createClass({
  render:function(){
    return(
       <h2>{this.props.author}</h2>  // 利用 props 來取得父元件中的 author 屬性內容
      )
  }
});

ReactDOM.render(
   <CommentParent />,   document.querySelector('.content')
)

留言