react中jsx语法注意要点

<body>
    <div id="app"></div>
    <script src="./assets/react16.4.js"></script>
    <script src="./assets/react-dom.js"></script>
    <script src="./assets/babel.min.js"></script>
    <script type="text/babel">
        // 1:jsx语法中只能由一个根标签
        // 2:jsx 只能在( )中编写
        // 3:在html中需要写js,需要使用 单{ }
        // 问题:哪些内容可以在 { } 中编写
        // 3.1 变量引用

         let msg = 'hellow word 渲染变量';
         let handle = ()=>{
            console.log('handle run');
            return 'handle run'
         }


         let data = ['12332','fasdfdf'];
         /**
          * arr.map()
          * 作用:数组重组,返回全新数组
          * 返回:数组
          * 参数:回到函数:
          * 1:回调函数执行次数===数组参数
          * 2:返回值数组中元素为回调函数return的值
          * 3:回参为arr中的元素
          */ 
         let ChildNode = data.map(item=>{
            console.log(item);
            // 输出的是虚拟dom 小括号可以省略,建议添加上
            return (<span>{item}</span>)
         })
         console.log(ChildNode);

    
        //  只有vNode内部才是jsx语法
         let vNode = (
            <div>
              <h1>hellow word</h1>
              <div>每个虚拟dom只能由一个根标签</div>
              {/* sx语法中的注释 */}
              <h2>{ 
                /* 注释,变量引用 */
                msg
               }</h2>
               
               {/* 
                1:jsx中如果由 js运算,先运算js得到【结果】
                2:确认虚拟dom.
                3: 将虚拟dom渲染为真实dom 

                js中运算并且由结果的有哪些???
                1:表达式。
                2:变量引用
                3:函数执行
                */}

                <h1>{ 1+1+'123123' /*写表达式,输出表达式运算结果*/}</h1>
                <h1>{ handle() /* 可以写函数执行 */ }</h1>
                <h1>{ 'aaa' }</h1>
                <h1>{ 123 }</h1>
                <h1>{ undefined }</h1>
                <h1>{ null }</h1>
                <h1>{[123,213,123123,'daffasd']}</h1>
                {/* Objects are not valid as a React child (found: object with keys {a}). If you meant to render a collection of children, use an array instead. */}
                {/*<h1>{ {a:'123'} }</h1>*/}
                <div>
                  {
                    [
                    <span>12332</span>,
                    <span>fasdfdf</span>
                    ]
                  }
                </div>

                <div>
                {
                    ChildNode
                }
                </div>

                {/* 直接在虚拟dom上处理的,语法多 */}
                <div>
                 {
                    data.map(item=>(<span>{item}</span>))
                 }
                 </div>

                 {/* 语法注意: 一个{} 只需要一个运算结果,所以只能写一个表达式,一个变量引用,一个函数执行 */}
                 {/* <h1>{1+1;2+2}</h1> */}


                 <h1 id={'text'}>hellow word</h1>
                { /*<h1 id={'text','aaa'}>语法错误</h1>*/}
                
                 <h1 id={['text','aaa'].join(' ')}>多个id</h1>
                 <h1 id={'text aaa'}>属性动态赋值,本质需要字符串</h1>
                 {/* Invalid DOM property `class`. Did you mean `className`? */}
                 <h1 className={'text aaa'}>注意将class变为className</h1>
                 

 {/* ----------------------为错误写法----------------------------------------------------- */}
                
                  <h1>{
                    // 不能写if for switch...等语句
                    // if(true){
                    //     bug
                    // }
                    // for(var i =0;;i<2;i++){}
                 }</h1>


                 {/* 
                 The tag <wenkai> is unrecognized in this browser. If you meant to render 
                 a React component, start its name with an uppercase letter. */}
                 <wenkai>{ '自定义标签小写' }</wenkai>

                 {/* 
                  bug : Wenkai is not defined
                */}
                
                {/*<Wenkai>{ 'REACT 组件bug' }</Wenkai> */}



                {/* bug:标签必须闭合 */}
                {/*  <input type="text" > */}
            </div>
        )
        // let node = '<p>{str}</p>' // 注意:这是js中字符串类型,不是虚拟dom
        // 注意:虚拟dom是一个json对象
        ReactDOM.render(vNode,document.getElementById('app'))


        // 查看:虚拟dom和真实dom的区别
        // console.log(vNode);
        // console.dir(document.querySelector('#app'));
        

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲤忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值