回到首页

React.js 小书

学习笔记 1

学习笔记 2

学习笔记 3

学习笔记 4

学习笔记 5

学习笔记 6

学习笔记 7

学习笔记 8

学习笔记 9

学习笔记 10

效果

源码

<div id="wrapper"></div>
<script type="text/babel">

class LikeButton extends React.Component {
  static defaultProps = {
    likedText: '取消',
    unlikedText: '点赞'
  }

  constructor () {
    super()
    this.state = { isLiked: false }
  }

  handleClickOnLikeButton () {
    this.setState({
      isLiked: !this.state.isLiked
    })
  }

  render () {
    return (
      <button onClick={this.handleClickOnLikeButton.bind(this)}>
        {this.state.isLiked
          ? this.props.likedText
          : this.props.unlikedText} 👍
      </button>
    )
  }
}

class Index extends React.Component {
  constructor () {
    super()
    this.state = {
      likedText: '已赞',
      unlikedText: '赞'
    }
  }

  handleClickOnChange () {
    this.setState({
      likedText: '取消',
      unlikedText: '点赞'
    })
  }

  render () {
    return (
      <div>
        <LikeButton
          likedText={this.state.likedText}
          unlikedText={this.state.unlikedText} />
        <div>
          <button onClick={this.handleClickOnChange.bind(this)}>
            修改 wordings
          </button>
        </div>
      </div>
    )
  }
}

  const container = document.getElementById('wrapper');
  const root = ReactDOM.createRoot(container);
  root.render(<Index />)
</script>

要点