12、React系列之--微博 Demo 01

版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。

PS:轉(zhuǎn)載請注明出處
作者:TigerChain
地址:http://www.itdecent.cn/p/f4e6ecfd52fd
本文出自TigerChain簡書

React 教程系列

教程簡介

  • 1、閱讀對象

本篇教程適合有React基礎(chǔ)的朋友閱讀(基礎(chǔ)知道 state,props, 組件化思想,webpack+yarn 等),老鳥直接略過,如果有誤,歡迎指出,謝謝。

React 系列之--微博 Demo 02 終結(jié)篇:http://www.itdecent.cn/p/9fddf666b718

正文

經(jīng)過前面幾個章節(jié)的學(xué)習(xí),我們基本上把 React 的一些基本知識掌握了,那么我們把以前掌握的知識綜合起來做一個小案例。來對前面知識做一個歸納和復(fù)習(xí),本節(jié)知識需要前面知識學(xué)習(xí),如果沒有學(xué)習(xí),建議先去學(xué)習(xí)前面的章節(jié)比如 webpak yarn babel等。

1、無圖無真相,效果圖

weibo-demo.gif

如圖所示,今天要寫的 Demo 就長上面的樣子

2、可以學(xué)到什么

通過本 Demo 我們可以學(xué)習(xí)到組件的組合,內(nèi)聯(lián) css 樣式使用,flexbox布局,組件化思想以及組件之間的交互等內(nèi)容。

3、Demo分析

首先我們把微博組件化,如何劃分組件呢,我們以下圖為劃分組件的標(biāo)準(zhǔn),當(dāng)然這不是唯一標(biāo)準(zhǔn),只是我覺得比較好的組件分的方式。

weibo-component.png

4、開始擼碼

1、微博的雛形

接下來我們就手把手從0開始開發(fā)微博 demo,我們使用 webpack+yarn 來構(gòu)建項目,在 mac 環(huán)境下(由于本人電腦是 mac ),win 下基本差不多.

1、進(jìn)入命令行在指定目錄中新建 weibodemo

mkdir weibodemo

2、使用 yarn 初始化項目

yarn-init.png

3、添加依賴

yarn add babel-core babel-loader babel-preset-es2015 babel-preset-react babel-preset-stage-0  react react-dom webpack webpack-dev-server --dev

經(jīng)過前面幾節(jié)的學(xué)習(xí),我們都知道安裝這些依賴是什么意思,這里不再多說,如果明折,可以看 webpack 這節(jié)http://www.itdecent.cn/p/732c4d501668。

通過以上操作,如果沒有什么問題,就會把所需要的依賴添加進(jìn)去了。

weibo-adddepend.png

4、在項目根目錄新建 .babelrc 并輸入以下內(nèi)容

{
  "presets": ["react", "es2015","stage-0"]
}

5、在項目根目錄中新建 webpack.config.js 并配置

module.exports = {
  entry:  __dirname + "/app/main.js",
  output: {
    path: __dirname + "/public",
    filename: "bundle.js"
  },
  module: {
    loaders: [
      //babel配置
    {
      test:/\.js$/,
      exclude: /node_modules/,
      loader: 'babel-loader'
    }
    ]
  },

  devServer:{
    contentBase: "./public",//本地服務(wù)器所加載的頁面所在的目錄
    // colors: true,//終端中輸出結(jié)果為彩色
    historyApiFallback: true,//不跳轉(zhuǎn)
    inline: true//實時刷新
  }
}

6、在根目錄中新建 public 和 app 目錄,并且分別新建index.html 和 ?main.js

#  index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>WeiBo demo</title>
</head>
<body>
<div id="container"></div>
<script src='bundle.js'></script>
</body>
</html>

#  main.js

import React from 'react' ;
import ReactDOM from 'react-dom' ;

//模擬微博列表數(shù)據(jù)
var datas = {
  "data":[
    {
        "myUrl":"../img/tiger.jpg",
        "headUrl":"../img/tiger.jpg",
        "nickName":"小東",
        "content":"今天天氣不錯呀,我可以好好的出去玩玩了!?。?,
        "NoCollect":"13",
        "NoForward":"20",
        "NoComment":"14",
        "NoPointGreat":"42",
        "sendTime":"11月10日"
    },
    {
        "myUrl":"../img/tiger.jpg",
        "headUrl":"../img/tiger.jpg",
        "nickName":"小高",
        "content":"今天去了了家新開的超市,里面東西真多呀",
        "NoCollect":"12",
        "NoForward":"2",
        "NoComment":"12",
        "NoPointGreat":"23",
        "sendTime":"10月8日",
        "contentImgUrls":[
            {"img":"../img/qiche.jpg"},
            {"img":"./img/qiche.jpg"},
            {"img":"../img/qiche.jpg"},
            {"img":"./img/qiche.jpg"}
        ]
    },
    {
        "myUrl":"../img/me.png",
        "headUrl":"../img/ruobin.png",
        "nickName":"Michelle不想讓人看見",
        "content":"看了一部電視,感覺非學(xué)不錯,給大家推薦一下",
        "NoCollect":"132",
        "NoForward":"202",
        "NoComment":"142",
        "NoPointGreat":"423",
            "sendTime":"11月6日"
    }
  ]
  }

//定義微博列表組件
 class WeiboList extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    //遍歷微博列表
    var ItemView = datas.data.map(function(item,index) {
    return (<div key={index}>
      {item.nickName} <br/>
        
      {item.content}
      <hr />
    </div>) ;
    //渲染列表
    return (<div>{ItemView}</div>);
  }
}


ReactDOM.render(
 <WeiboList />,
  document.getElementById('container')
) ;

接下來我們在 package.json 中配置腳本

"scripts":{
    "start":"webpack-dev-server --progress --port 8899"
}

從上面的代碼我們可以知道,我們創(chuàng)建一個微博列表組件,然后渲染在界面上,并且我們模擬了一些微博列表的數(shù)據(jù),那么我們看看效果:

在命令行中輸入 yarn start,此過程如果沒有什么問題,我們在瀏覽器輸入 loaclhost:8899 會看到以下效果:

weibo-first.png

從圖中我們可以看到微博的雛形出來了。

2、樣式和組件化微博

通過上面我們知道,我們把微博列表和數(shù)據(jù)都寫在同一個界面中了,React 玩的就是組件化,所以我們接下來把微博列表定義成一個組件并且把數(shù)據(jù)抽出來。以下步驟都是在上面 demo 的基礎(chǔ)上去改的。

1、在 app 目錄中新建一個 WeiBoList.js

# WeiBoList.js

import React, { Component, PropTypes } from 'react';

//定義一個微博列表組件
export default class WeiBoList extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    //遍歷微博列表
    var ItemView = datas.data.map(function(item,index) {
    return (<div key={index}>
      {/* 昵稱 */}
      {item.nickName}
    <br />
      {/* 內(nèi)容 */}
      {item.content}
        <hr />
    </div>)
  }) ;
    //渲染列表
    return (<div>{ItemView}</div>);
  }
}

WeiBoList.propTypes = {
};

2、將數(shù)據(jù)抽取在 json 文件件中

在 weibodemo 根目錄中新建 data/data.json 文件

{"data":[
    {
        "myUrl":"../img/tiger.jpg",
        "headUrl":"../img/tiger.jpg",
        "nickName":"小東",
        "content":"今天天氣不錯呀,我可以好好的出去玩玩了?。?!",
        "NoCollect":"13",
        "NoForward":"20",
        "NoComment":"14",
        "NoPointGreat":"42",
        "sendTime":"11月10日"
    },
    {
        "myUrl":"../img/tiger.jpg",
        "headUrl":"../img/tiger.jpg",
        "nickName":"小高",
        "content":"今天去了了家新開的超市,里面東西真多呀",
        "NoCollect":"12",
        "NoForward":"2",
        "NoComment":"12",
        "NoPointGreat":"23",
        "sendTime":"10月8日",
        "contentImgUrls":[
            {"img":"../img/qiche.jpg"},
            {"img":"./img/qiche.jpg"},
            {"img":"../img/qiche.jpg"},
            {"img":"./img/qiche.jpg"}
        ]
    }
    
    ]
}


PS:這里我為了說明,json 微博列表數(shù)據(jù)給出了兩個,實際以 demo 中為準(zhǔn),或者自行添加。

3、修改 main.js

# main.js

import React from 'react' ;
import ReactDOM from 'react-dom' ;
//引入微博列表組件
import WeiBoList from './WeiBoList.js' ;
//導(dǎo)入數(shù)據(jù)
import datas from '../data/data.json' ;

//渲染微博列表 并且把數(shù)據(jù) 通過props傳遞到 WeiBoList中
ReactDOM.render(
 <WeiBoList data={datas.data}/>,
  document.getElementById('container')
) ;

我們可以看到 main.js 我們修改的還是比較大的,把模擬數(shù)據(jù)和微博組件都從 main.js 中移除了,并且導(dǎo)入數(shù)據(jù)和微博列表組件( WeiBoList ),并且以 props 形式把數(shù)據(jù)傳遞給 WeiBoList 組件

4、再次修改 WeiBoList.js

我們使用 props 屬性拿到微博列表數(shù)據(jù)然后遍歷,所以這里有一點小修改,代碼就不貼出來了,直接上個圖吧。

weibolist-props.png

其中黃色的部分就是我們修改過的。

5、 運(yùn)行看效果,如果沒有什么問題則會顯示如下:

weibo-second.png

我們成功以的把數(shù)據(jù)抽取出來,并且邁開了組件人的第一步。

6、引入 css 樣式

以上我們都是在進(jìn)行無樣式開發(fā),現(xiàn)在我們就引用 flexbox 和 css 給界面添加一些色彩。

  • 1、在項目根目錄添加 css/ListStyle.css
.listRootViewStyle {
  background-color: #eee; /**背景色*/
  padding: 10px;  /**設(shè)置內(nèi)邊距**/
}
  • 2、修改WeiBoList.js
modify-weibolist.png

其中黃色的部分是我們新添加的。

  • 3、我們添加 style-loader css-loader

如果完成上面的部分,那還沒有完,我們必須添加 style-loader css-loader 否則 2 中的修改會報錯。

yarn add style-loader css-loader --dev

PS:其中 css-loader 是用來讀取 css 文件的
style-loader 將css插入到頁面中

我們運(yùn)行一下項目,不出什么問題會報如下錯

not-config-css-loader.png

這是什么原因呢,看過webpack這一章的朋友應(yīng)該知道我們安裝一個 loader 就要在 webpack.config.js 中去配置。

接下來我們在 webpack.config.js 中去配置 css-loader 和 style-loader 。

add-css-loader.png

從圖中可知我們添加了黃色部分

#  style-loader css-loader

{
    test: /\.css$/,
    loader: 'style-loader!css-loader?modules'
}

7、運(yùn)行一下看效果

weibo-bgstyle.png

我們看到 css 樣式成功表現(xiàn)在微博上了。

3、完善微博列表

上面我們給微博添加了樣式,但是這看起來還不是很舒心,我們把微博列表抽成一個組件,如下:

weibo-footview.png

由圖可以知道,這個組件基本上是上下分隔的,以分隔線為主??梢宰龀蓛蓚€ view ,當(dāng)然也可以自定義成兩個組件。我們這里就分成圖片列表組件 CommentListImgs.js 評論組件 CommentForm.js 和微博列表組件 WeiBoListItem.js,我們一步一步來,先做 WeiBoListItem.js,后面再一步步的添加 CommentForm.js 和 CommentListImgs.js

以下功能都是在上面的基礎(chǔ)上完成的。

1、在app中新建 WeiBoListItem.js 組件

import React, { Component, PropTypes } from 'react';

import styles from '../css/ListItemStyle.css' ;

/**
 * 微博評論列表組件
 */
export default class WeiBoListItem extends Component {
  constructor(props) {
    super(props);
    this.state = {
      //是否點擊評論按鈕標(biāo)志
      isComment:false,
      //默認(rèn)的條目數(shù)據(jù)
      itemData:this.props.itemData,
      //默認(rèn)的點贊數(shù)
      zanNum:this.props.itemData.NoCollect
    }
  }

  render() {
    //取得傳遞過來的數(shù)據(jù)
    let data = this.props.itemData ;


    return (
      <div>
          {this._renderHeadView(data)}

          <hr className={styles.hrStyle}/>

          {this._renderFooterView(data)}
          
        </div>
     );
  }

  /**
   * 渲染頂部View
   */
  _renderHeadView(data){
    return(
      <div className={styles.item}>
     
     
        <div className={styles.topRightView}>
          <div className={styles.nickNameAndSendTime}>
            <span>{data.nickName}</span>
            <span>{data.sendTime}</span>
          </div>
          
          <p>{data.content}</p>
        </div>
      </div>
    )
  }

  /**
   * 渲染底部View
   */
   _renderFooterView(data){
       return(
         <div className={styles.commentViewStyle}>
           <ul className={styles.ulStyle}>
             <li className={styles.liStyle} >點贊:{this.state.zanNum}</li><div className={styles.shuxian}></div>
             <li className={styles.liStyle} >評論:{data.NoComment}</li><div className={styles.shuxian}></div>
             <li className={styles.liStyle} >轉(zhuǎn)發(fā):{data.NoPointGreat}</li>
           </ul>
         </div>
       );
   }
 }

2、在 css 目錄中新建 ListItemStyle.css


.item{
  display: flex;
  background-color: #fff;
  padding: 20px;
}
/*頂部view樣式*/
.topRightView{
  display: flex;
  flex-direction: column;
  flex:1
}
/*昵稱和發(fā)送時間樣式*/
.nickNameAndSendTime{
  display: flex;
  justify-content: space-between; /**主軸的對齊方式*/
}


/*橫線樣式*/
.hrStyle{
  margin-top: -1px;
}
.ulStyle{
  display:flex;
  height: 40px;
  align-items: center;
  margin-left: -40px;
}
/*點贊 轉(zhuǎn)發(fā)*/
.liStyle{
  display: flex;
  color: orange;
  flex: 1.0;
  font-size: 16px;
  justify-content: center;
}
.commentViewStyle{
  background-color: #fff;
  margin-top: -17px;
}

.shuxian{
  height: 20px;
  width: 1px;
  background-color: grey;
}

3、修改WeiBoList.js

modify-weibolist2.png

如圖所示:我們修改的部分為黃色部分,最終WeiBoList.js為


import React, { Component, PropTypes } from 'react';

import styles from '../css/ListStyle.css' ;

//導(dǎo)入微博列表組件
import WeiBoListItem from './WeiBoListItem.js'

//定義一個微博列表組件
export default class WeiBoList extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    //遍歷渲染每個條目
    var ItemView = this.props.data.map(function(item,index) {
      return <WeiBoListItem itemData= {item} key = {index}/>
    }) ;

    return(
      <div className={styles.listRootViewStyle}>
       {ItemView}
      </div>
    ) ;
}
}

WeiBoList.propTypes = {
};

4、運(yùn)行看效果,就會看到我上面的效果圖

component-weibo-1.gif

到此為止,我們就把微博的基本雛形搞出來了,下一節(jié)我們把評論組件和頭像等做出來,并且添加事件交互 。

Demo地址

https://github.com/tigerchain/react-lesson/tree/master/lesson02/10-weibodemo

在React 系列之--微博 Demo 02 終結(jié)篇中我們會繼續(xù)本篇來完成最終的效果。以下終結(jié)篇的地址。

React 系列之--微博 Demo 02 終結(jié)篇:http://www.itdecent.cn/p/9fddf666b718

如果覺得對你有用,就點個喜歡吧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 PS:轉(zhuǎn)載請注明出處作者:TigerChain地址:http...
    TigerChain閱讀 1,466評論 3 4
  • 無意中看到zhangwnag大佬分享的webpack教程感覺受益匪淺,特此分享以備自己日后查看,也希望更多的人看到...
    小小字符閱讀 8,359評論 7 35
  • GitChat技術(shù)雜談 前言 本文較長,為了節(jié)省你的閱讀時間,在文前列寫作思路如下: 什么是 webpack,它要...
    蕭玄辭閱讀 12,874評論 7 110
  • 構(gòu)建一個小項目——FlyBird,學(xué)習(xí)webpack和react。(本文成文于2017/2/25) 從webpac...
    布蕾布蕾閱讀 17,107評論 31 98
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,828評論 25 709

友情鏈接更多精彩內(nèi)容