美文网首页
react_04jsx的本质(一)

react_04jsx的本质(一)

作者: 小话001 | 来源:发表于2021-07-14 17:57 被阅读0次

理论相关:

  • 实际上jsx只是React.createElement(component,pros,children)函数的语法糖。最终jsx都会被转换成React.createElement函数调用。
    转换网站:在线转换
  • 观察createElement函数在源码中的位置:ReactElement.js


    React.js
    image.png
  • createElement传递三个参数
    1.type(当前的类型,可以是标签元素也可以是组件元素)
    2.config(jsx中的属性都在这以key:value的形式存储)
    3.children(存放在标签中的内容,以children数组的方式存储,如果是多个元素也没事,源码对其有特殊处理)


    createElement函数参数
    源码处理
  • 通过createElement创建出来的ReactElement对象有啥作用?
    1.React利用它组成了JavaScript对象树
    2.JavaScript对象树即虚拟DOM
    可以在render()函数中观察对象,然后打印出来:
render(){
  var elementObj=React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
  className: "header"
}, "header"), /*#__PURE__*/React.createElement("div", {
  className: "content"
}, "content"), /*#__PURE__*/React.createElement("div", {
  className: "footer"
}, "footer"));
 console.log(elementObj);
 return elementObj;
}
流程:

jsx -->createElement函数-->ReactElement(对象树)-->ReactDOM.render-->真实DOM
RN: jsx -->createElement函数-->ReactElement(对象树)-->ReactDOM.render-->原生控件(UIButton/Button)

案例相关:

实现效果:

案例最终效果
注意事项:
  • React中设计原则:state中的数据不可变性
  • splice会直接影响数组,filter不会影响原来数组

案例思路:
1.state中造数据
2.按照图示普通页面table布局和样式修改
3.普通写死数据替换为循环数据
4.价格展示的地方考虑封装成工具放在utils文件中,考虑传进来的若非number类型,不能保留两位小数问题
5.整个页面以render()为界限,上面都是渲染相关的组件,下面都是操作的逻辑
6.reduce高阶函数与ES6扩展运算符的掌握
7.button按钮负数后禁用问题

<script type="text/babel">
        class App extends React.Component{
            constructor(){
              super();
              this.state={
                books:[{name:"《算法导论》",date:"2006-9",price:85,count:1},{name:"《编程艺术》",date:"2003-9",price:59,count:2},{name:"《react入门》",date:"2001-9",price:39,count:3},{name:"《数据结构》",date:"2006-3",price:99,count:1}]
              }
            }
            renderTable(){
              let totalPrice=0
              //方式一:
              // for(let item of this.state.books){
              //   totalPrice+=item.price*item.count
              // }
              //方式二:reduce高级函数,this.state.books.reduce(fun,initialValue) ,其回调函数的参数 ,参数一:上一次回调函数的结果(第一次没有上一次函数的回调函数结果就使用初始化值)
              totalPrice=this.state.books.reduce((preValue,item,index)=>{
               return preValue+item.price*item.count
              },0)
              return(
                <React.Fragment>
                    <table>
                        <thead>
                          <tr>
                            <th></th>
                          <th>书籍名称</th>
                          <th>出版日期</th>
                          <th>价格</th>
                          <th>购买数量</th>
                          <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                          {
                            this.state.books.map((item,index)=>{
                              return( 
                              <tr key={index}>
                                <td>{index+1}</td>
                                <td >{item.name}</td>
                                <td >{item.date}</td>
                                <td >{this.formatePrice(item.price)}</td>
                                <td> 
                                  <button  disabled={item.count<=1}  onClick={e=>this.changeCount(index,"decrate")} >-1</button>
                                  <span style={{padding:5}}>{item.count}</span>
                                  <button onClick={e=>this.changeCount(index,"add")}>+1</button>
                                </td>
                                <td><button onClick={e=>this.deleteBook(index)}>移除</button></td> 
                              </tr>  
                             )
                            })
                          }
                        </tbody>
                    </table>
                     <h2>总价格:{this.formatePrice(totalPrice)}</h2>       
                </React.Fragment>
              ) 
            }
            renderTempTip(){
              return <h2>当前购物车并无任何书籍~</h2>
            }
            render(){
               return  this.state.books.length>0?this.renderTable():this.renderTempTip() 
            }
            deleteBook(index){
                console.log(index)
                //React中设计原则:不能直接修改原来数据,除非通过setState来操作,比如用splice直接修改就不行this.state.books.splice已经直接修改数组,而filter不会影响原来的数组(books)
                this.setState({
                  books:this.state.books.filter((item,indey)=>{return index!=indey})
                })
            }
            //价格处理
            formatePrice(price){
              if(typeof(price)!=="number"){
                price=Number(price)||0
              }
              return "¥"+price.toFixed(2)
            }
            // 加减
            changeCount(index,type){
              //错误的操作!!!因为直接修改了数据
            //  this.state.books[index].count+=1;
            //   this.setState({
            //     books:this.state.books
            //   }) 

            // 正确操作
               let newbooks=[...this.state.books]
              if(type=="add"){
                newbooks[index].count+=1
                 this.setState({
                    books:newbooks
                 })
              }
              else{
                newbooks[index].count-=1
                this.setState({
                    books:newbooks
                 })
              }
            }
        }
        ReactDOM.render(<App/>,document.getElementById("app"))
    </script>

相关文章

  • react_04jsx的本质(一)

    理论相关: 实际上jsx只是React.createElement(component,pros,children...

  • 人的本质和我的本质(一)

    一,问题的提出。 “人”和“我”二字,是我们平时使用最繁的两个字。但二者的本质,目前还是各说各言。 人,常与动物相...

  • 本质的本质

    这个世界为什么会有友谊这种东西?进化心理学认为这是因为在采集-狩猎时期,个人的食物供应充斥着不确定性,如果一个人长...

  • 生活的本质(一)

    受人之托,书中的我就是阿青他本人,为了写起来方便 原本以为生活给了我们甚么,我们就要去享受甚么,然而那只是...

  • 婚姻的本质(一)

    儿子上大学以后,老赵夫妻俩越发的没有交流了,老赵索性搬到了儿子的房间,一开始是想念儿子,毕竟从小到大没离开过身边,...

  • 商业的本质(一)

    商业的本质是什么?(一) 我经常给人聊到一些商业的话题,可是呢? 有时候,就很无趣了,或鸡同鸭讲,或话不投机! 我...

  • 函数的本质(一)

    一.认识汇编语言 要认识汇编语言,还得从编程语言的发展说起,语言有以下几种分类,其发展都是为了让我们更容易去操纵计...

  • 写作的本质(一)

    对于写作的认知是应该持续更新的,每一次升级都是一次可见的提高。 写作的本质是表达,把自己知道的东西通过通俗的话说出...

  • 金钱的本质(一)

    当我们想要追求金钱的时候,我们实际上在追求什么? 从表面上看,当我们拥有金钱,我们就可以购买到我们生活需要的食物、...

  • 贫穷的本质(一)

    1、成功并不总像看上去那样遥远 2、只要有自由市场和恰当的奖励机制,人们就能自己找到解决问题的方法 3、贫穷并不仅...

网友评论

      本文标题:react_04jsx的本质(一)

      本文链接:https://www.haomeiwen.com/subject/fofypltx.html