先看兩段代碼:
export class ywg extends PureComponent { …… render() { return ( …… ); } }
export class ywg extends Component { …… render() { return ( ……
);
}
}
PureComponent VS Component
Stateless components may also be referred to as Pure Components, or even Dumb Components, and are meant to represent any React Component declared as a function that has no state and returns the same markup given the same props.
These types of components surprisingly compose a large majority of our applications and, as a result, React 0.14 introduces the ability to write these stateless components as functions, also known as functional stateless components.
So these are some of the UI elements that I summarize from Tencent work.
無狀態的組件又被叫做“純粹的組件”,甚至是“啞巴組件”。這是指這樣的一些組件:React組件中有一些被聲明為函數(而不是繼承自Component的class),它們沒有state
,只要給定同樣的props
,就會返回同樣的標簽塊。這種類型的組件出人意料的成為構建大型應用程序的常用方法,所以,自從React 0.14開始支持使用函數的方式來創建這種無狀態組件,所以也被叫做“函數式無狀態組件”。無狀態組件渲染效率更高,編寫和組合更加簡單。
在RN項目中,無論你使用這兩者中的哪一種,都無可厚非,關鍵就是自己在使用時權衡利弊或者是跟團隊保持一致就好。