react基本属性 和 css类名 的绑定


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>React</title>
  <script src="./lib/react-development.js"></script>
  <script src="./lib/react-dom-development.js"></script>
  <script src="./lib/babel-min.js"></script>
</head>


<body>
  <div id="app"></div>
</body>


<script type="text/babel">
  class App extends React.Component {
    constructor() {
      super()
      this.state = {
        title: '哈哈哈',
        img: 'https://img1.baidu.com/it/u=1546227440,2897989905&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500',
        href: 'https://www.baidu.com',
        isActive: true,
        objStyle: { color: 'red', fontSize: '14px' }
      }
    }

    render() {
      const { title, img, href, isActive, objStyle } = this.state

      // `需求:isActive为true时,绑定类名active`
      
      // `绑定class:字符串模式`
      const className = `c1 c2 ${isActive ? 'active' : ''}`
      
      // `绑定class:数组模式`
      const classList = ['c1', 'c2']
      if (isActive) classList.push('active')
      
	    // `使用第三方库classNames(npm i classNames)`这个后边说

      return (
        <div>
          `1、基本属性的绑定`
          <h2 title={title}> 我是h2元素 </h2>
          <img src={img} alt="" />
          <a href={href}> 打开百度 </a>

{ /* ------------------------------------------------------------------ */ }

          { /* `绑定class:基本模式` */ }
          <h2 className="c1 c2"> 哈哈哈哈哈哈 </h2>
	
          { /* `绑定class:字符串模式` */ }
          <h2 className={className}> 嘎嘎嘎嘎 </h2>

          { /* `绑定class:数组模式` */ }
          <h2 className={classList.join(' ')}>嘎嘎嘎嘎</h2>

{ /* ------------------------------------------------------------------ */ }

         { /* `绑定 style 方式一:这里的第一个{},表示,它里面写的是js代码` */ }
          <p style={{ color: "red", fontSize: "14px" }}>黑黑黑黑黑黑</p>

          { /* `绑定 style 方式二` */ }
          <p style={objStyle}>呵呵呵呵呵呵</p>
        </div>
      )
    }
  }

  const root = ReactDOM.createRoot(document.querySelector('#app'))
  root.render(<App />)

</script>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值