JSX属性绑定

  1. <script src="format_utils.js"></script>
  2. <script type="text/babel">
  3. class App extends React.Component {
  4. constructor(){
  5. super()
  6. this.state = {
  7. title: '标题',
  8. imgUrl: 'https://p1.music.126.net/gacBlG1n5qDYaheqsemMNA==/3250156380424409.jpg',
  9. link: 'https://www.baidu.com',
  10. active: true
  11. }
  12. }
  13. render(){
  14. const {title, imgUrl, link, active} = this.state
  15. return (
  16. <div>
  17. {/* 1.绑定普通属性 */}
  18. <h2 title={title}>标题</h2>
  19. <img src={getImgUrl(imgUrl)} alt="" />
  20. <a href={link} target="_blank">百度</a>
  21. {/* 2.绑定class */}
  22. <div className={"box title " + (active ? "active" : "")}>我是div</div>
  23. <label htmlFor=""></label>
  24. {/* 3.绑定style */}
  25. <div style={{color: 'red', fontSize: "50px"}}>我是div</div>
  26. </div>
  27. )
  28. }
  29. }
  30. ReactDOM.render(<App />, document.querySelector('#app'))
  31. </script>

视频 03 第 33 分钟