摘要:哈哈,又是我,廢話不多說,直接看代碼個(gè)人原則既然是組件化的,那么相同的代碼,我不會(huì)寫第二遍不在結(jié)構(gòu)中夾雜太多邏輯遵守上一篇寫一手漂亮的的規(guī)則可讀性在我心里永遠(yuǎn)大于性能追求極致性能場景除外對生命周期函數(shù)排序傳遞多個(gè)時(shí)注意換行利用對象展開符傳遞
哈哈,又是我,廢話不多說,直接看代碼
個(gè)人原則既然react是組件化的,那么相同的代碼,我不會(huì)寫第二遍
不在dom結(jié)構(gòu)中夾雜太多js邏輯
遵守上一篇《寫一手漂亮的js》的規(guī)則
"可讀性" 在我心里永遠(yuǎn)大于 "性能"(追求極致性能場景除外)
對生命周期函數(shù)排序// bad class Demo extends React.Component { render() {} componentDidMount() {} componentWillMount() {} } // good class Demo extends React.Component { componentWillMount() {} componentDidMount() {} render() {} }傳遞多個(gè)props時(shí)注意換行
// bad利用對象展開符傳遞props{}} /> // goood {}} />
const someProps = { a: 1, b: 2, c: 3 } // bad利用箭頭函數(shù)綁定this// goood // 當(dāng)有些屬性不需要傳遞的時(shí)候 const { a, ...otherProps } = someProps
// bad class Demo extends React.Component { handleClick() { } render() { } } // good class Demo extends React.Component { handleClick = () => { } render() { } }提前解構(gòu)state,props
// bad class Demo extends React.Component { handleClick = () => { this.props.add(this.state.a + this.state.b) this.props.respond() } } // good class Demo extends React.Component { handleClick = () => { const { a, b } = this.state const { respond, add } = this.props add(a + b) respond() } }map時(shí)不要使用index當(dāng)做key,用item的id
index沒辦法利用key來避免不必要的渲染
// bad class Demo extends React.Component { render() { return arr.map((item, i) => ( {item.name} )) } } // good class Demo extends React.Component { render() { return arr.map(item => ( {item.name} )) } }不要將大段的內(nèi)聯(lián)樣式寫在組件上
影響閱讀
// bad class Demo extends React.Component { render() { return (給props加上類型檢查11) } } // good const styles = { container: { width: "100px", height: "100px", textAlign: "center", lineHeight: "100px" } } class Demo extends React.Component { render() { return (11) } }
一定程度上能及時(shí)發(fā)現(xiàn)問題,當(dāng)然更好的選擇是flow、ts
// bad class Demo extends React.Component { // nothing } // good import PropTypes from "prop-types"; class Demo extends React.Component { static propTypes = { className: PropTypes.string, style: PropTypes.object, url: PropTypes.oneOfType([ PropTypes.string, PropTypes.array, ]), onClick: PropTypes.func, } }盡量不要在渲染組件時(shí)傳遞匿名函數(shù)
首先它會(huì)影響閱讀
每次渲染會(huì)生成新的匿名函數(shù),對子組件來說就是新的props,就會(huì)觸發(fā)再一次更新
當(dāng)然,當(dāng)函數(shù)只有一行的時(shí)候,我覺得也是可以這么做的,從代碼簡潔性考慮
// bad class Demo extends React.Component { render() { return (補(bǔ)充{ a++ this.props.add() }}>11 ) } } // good class Demo extends React.Component { handleClick = () => { a++ this.props.add() } render() { return (11 ) } }
大??靵碓u論區(qū)批評、指正、補(bǔ)充
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/92578.html
摘要:介紹看了很多,卻沒有人教我怎么去寫一手漂亮的代碼,今天我來講講我自己寫的經(jīng)驗(yàn)不要在代碼中留大段注釋掉的代碼留給去管理,不然你要干嘛適當(dāng)?shù)負(fù)Q行適當(dāng)?shù)奶砑幼⑨?,但不要瘋狂的添加注釋對一段代碼或者一行特別需要注意的代碼注釋不要瘋狂的注釋,太啰嗦, 介紹 看了很多best practice,卻沒有人教我怎么去寫一手漂亮的js代碼,今天我來講講我自己寫js的經(jīng)驗(yàn) 不要在代碼中留大段注釋掉的代碼 ...
平日學(xué)習(xí)接觸過的網(wǎng)站積累,以每月的形式發(fā)布。2017年以前看這個(gè)網(wǎng)址:http://www.kancloud.cn/jsfron... 03月份前端資源分享 1. Javascript 175453545 Redux compose and middleware 源碼分析 深入 Promise(二)——進(jìn)擊的 Promise Effective JavaScript leeheys blog -...
平日學(xué)習(xí)接觸過的網(wǎng)站積累,以每月的形式發(fā)布。2017年以前看這個(gè)網(wǎng)址:http://www.kancloud.cn/jsfron... 03月份前端資源分享 1. Javascript 175453545 Redux compose and middleware 源碼分析 深入 Promise(二)——進(jìn)擊的 Promise Effective JavaScript leeheys blog -...
平日學(xué)習(xí)接觸過的網(wǎng)站積累,以每月的形式發(fā)布。2017年以前看這個(gè)網(wǎng)址:http://www.kancloud.cn/jsfron... 03月份前端資源分享 1. Javascript 175453545 Redux compose and middleware 源碼分析 深入 Promise(二)——進(jìn)擊的 Promise Effective JavaScript leeheys blog -...
閱讀 1458·2021-09-24 10:26
閱讀 3751·2021-09-06 15:02
閱讀 711·2019-08-30 14:18
閱讀 638·2019-08-30 12:44
閱讀 3169·2019-08-30 10:48
閱讀 2007·2019-08-29 13:09
閱讀 2059·2019-08-29 11:30
閱讀 2371·2019-08-26 13:36