一個小案例,鞏固有狀態組件和無狀態組件的使用
通過for回圈生成多個組件
- 資料:
CommentList = [
{ user: '張三', content: '哈哈,沙發' },
{ user: '張三2', content: '哈哈,板凳' },
{ user: '張三3', content: '哈哈,涼席' },
{ user: '張三4', content: '哈哈,磚頭' },
{ user: '張三5', content: '哈哈,樓下山炮' }
]
入口
// JS打包入口檔案
// 1. 匯入 React包
import React from 'react'
import ReactDOM from 'react-dom'
// 匯入評論串列樣式【注意:這種樣式是全域的】
// import './css/commentList.css'
// 匯入評論串列組件
import CommentList from './components/comment1/CommentList.jsx'
ReactDOM.render(<div>
<CommentList></CommentList>
</div>, document.getElementById('app'))
評論串列組件
import React from 'react'
// 匯入當前組件需要的子組件
import CommentItem from './CommentItem.jsx'
// 評論串列組件
export default class CommentList extends React.Component {
constructor(props) {
super(props)
// 定義當前評論串列組件的 私有資料
this.state = {
cmts: [
{ user: '張三', content: '哈哈,沙發' },
{ user: '張三2', content: '哈哈,板凳' },
{ user: '張三3', content: '哈哈,涼席' },
{ user: '張三4', content: '哈哈,磚頭' },
{ user: '張三5', content: '哈哈,樓下山炮' }
]
}
}
// 在 有狀態組件中, render 函式是必須的,表示,渲染哪些 虛擬DOM元素并展示出來
render() {
//#region 回圈 評論串列的方式1,比較low,要把 JSX 和 JS 語法結合起來使用
/* var arr = []
this.state.cmts.forEach(item => {
arr.push(<h1>{item.user}</h1>)
}) */
//#endregion
return <div>
<h1 className="title">評論串列案例</h1>
{/* 我們可以直接在 JSX 語法內部,使用 陣列的 map 函式,來遍歷陣列的每一項,并使用 map 回傳操作后的最新的陣列 */}
{this.state.cmts.map((item, i) => {
// return <CommentItem user={item.user} content={item.content} key={i}></CommentItem>
return <CommentItem {...item} key={i}></CommentItem>
})}
</div>
}
}
使用CSS模塊化
- 可以在webpack.config.js中為css-loader啟用模塊化:
css-loader?modules&localIdentName=[name]_[local]-[hash:8] - 使用
:global()定義全域樣式
webpack中的配置
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader?modules&localIdentName=[name]_[local]-[hash:5]'] }, // 通過 為 css-loader 添加 modules 引數,啟用 CSS 的模塊化
{ test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] },
{ test: /\.(png|gif|bmp|jpg)$/, use: 'url-loader?limit=5000' },
{ test: /\.jsx?$/, use: 'babel-loader', exclude: /node_modules/ }
]
}
style樣式
.box{
border: 1px solid #ccc;
padding-left: 15px;
box-shadow: 0 0 6px #ccc;
margin: 10px 0;
}
/* 注意:當啟用 CSS 模塊化之后,這里所有的類名,都是私有的,如果想要把類名設定成全域的一個類,可以把這個類名,用 :global() 給包裹起來 */
/* 當使用 :global() 設定了全域的 類樣式之后,這個類不會被重命名 */
/* 只有私有的類才會被重命名 */
:global(.title){
color:red;
text-align: center;
}
.title{
color: green;
font-size: 16px;
}
.body{
font-size: 14px;
color:red;
}
評論項的組件
import React from 'react'
// 注意: 在使用 import 的時候,import 只能放到模塊的 開頭位置
import inlineStyles from './cmtItemStyles.js'
// 匯入評論項的樣式檔案【這種直接 import '../路徑識別符號' 的 CSS 匯入形式,并不是模塊化的CSS】
// import '../../css/commentItem.css'
// 默認情況下,如果沒有為 CSS 啟用模塊化,則接收到的 itemStyles 是個空物件,因為 .css 樣式表中,不能直接通過 JS 的 export defualt 匯出物件
// 當啟用 CSS 模塊化之后,匯入 樣式表得到的 itemStyles 就變成了一個 樣式物件,其中,屬性名是 在樣式表中定義的類名,屬性值,是自動生成的一個復雜的類名(防止類名沖突)
import itemStyles from '../../css/commentItem.css'
console.log(itemStyles)
// 封裝一個 評論項 組件,此組件由于不需要自己的 私有資料,所以直接定義為 無狀態組件
export default function CommentItem(props) {
// 注意: 如果要使用 style 屬性,為 JSX 語法創建的DOM元素,設定樣式,不能像網頁中那么寫樣式;而是要使用JS語法來寫樣式
// 在 寫 style 樣式的時候,外層的 { } 表示 要寫JS代碼了,內層的 { } 表示 用一個JS物件表示樣式
// 注意: 在 style 的樣式規則中,如果 屬性值的單位是 px, 則 px 可以省略,直接寫一個 數值 即可
//#region 樣式優化1
/* const boxStyle = { border: '1px solid #ccc', margin: '10px 0', paddingLeft: 15 }
const titleStyle = { fontSize: 16, color: "purple" }
const bodyStyle = { fontSize: 14, color: "red" } */
//#endregion
//#region 樣式優化2 把 樣式物件,封裝到唯一的一個物件中
/* const inlineStyles = {
boxStyle: { border: '1px solid #ccc', margin: '10px 0', paddingLeft: 15 },
titleStyle: { fontSize: 16, color: "purple" },
bodyStyle: { fontSize: 14, color: "red" }
} */
//#endregion
/* return <div style={inlineStyles.boxStyle}>
<h1 style={inlineStyles.titleStyle}>評論人:{props.user}</h1>
<h3 style={inlineStyles.bodyStyle}>評論內容:{props.content}</h3>
</div> */
// 注意: 當你懷念 vue 中 scoped 指令的時候,要時刻知道 , react 中并沒有指令的概念
return <div className={itemStyles.box}>
<h1 className={itemStyles.title}>評論人:{props.user}</h1>
<h3 className={itemStyles.body}>評論內容:{props.content}</h3>
</div>
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/155326.html
標籤:JavaScript
上一篇:react-組件
