這次帶給大家React組件的使用詳解,使用React組件的注意事項有哪些,下面就是實戰案例,一起來看一下。
當我剛開始寫React的時候,我看過很多寫元件的方法。一百篇教程就有一百種寫法。雖然React本身已經成熟了,但是如何使用它似乎還沒有一個「正確」的方法。所以我(作者)把我們團隊這些年來總結的使用React的經驗總結在這裡。希望這篇文字對你有用,不管你是初學者還是老手。
開始前:
我們使用ES6、ES7語法如果你不是很清楚展示元件和容器元件的區別,建議您從閱讀這篇文章開始如果您有任何的建議、疑問都清在評論裡留言基於類別的元件
現在開發React元件一般都用的是基於類別的元件。下面我們就來一行一樣的寫我們的元件:
import React, { Component } from 'react'; import { observer } from 'mobx-react'; import ExpandableForm from './ExpandableForm'; import './styles/ProfileContainer.css';
我很喜歡css in javascript。但是,這個寫樣式的方法還是太新了。所以我們在每個元件裡引入css檔。而且本地引入的import和全域的import會用一個空白行來分割。
初始化State
import React, { Component } from 'react' import { observer } from 'mobx-react' import ExpandableForm from './ExpandableForm' import './styles/ProfileContainer.css' export default class ProfileContainer extends Component { state = { expanded: false }
您可以使用了舊方法在constructor
裡初始化state
。更多相關可以看這裡。但是我們選擇更清晰的方法。
同時,我們確保在類別前面加上了export default
。 (譯者註:雖然這個在使用了redux的時候不一定對)。
propTypes and defaultProps
import React, { Component } from 'react' import { observer } from 'mobx-react' import { string, object } from 'prop-types' import ExpandableForm from './ExpandableForm' import './styles/ProfileContainer.css' export default class ProfileContainer extends Component { state = { expanded: false } static propTypes = { model: object.isRequired, title: string } static defaultProps = { model: { id: 0 }, title: 'Your Name' } // ... }
propTypes
和defaultProps
是靜態屬性。盡可能在元件類別的的前面定義,讓其他的開發人員讀程式碼的時候可以立刻注意到。他們可以起到文檔的作用。
如果你使用了React 15.3.0或更高的版本,那麼需要另外引入prop-types
包,而不是使用React.PropTypes
。更多內容移步這裡。
你所有的元件都應該有prop types。
方法
import React, { Component } from 'react' import { observer } from 'mobx-react' import { string, object } from 'prop-types' import ExpandableForm from './ExpandableForm' import './styles/ProfileContainer.css' export default class ProfileContainer extends Component { state = { expanded: false } static propTypes = { model: object.isRequired, title: string } static defaultProps = { model: { id: 0 }, title: 'Your Name' } handleSubmit = (e) => { e.preventDefault() this.props.model.save() } handleNameChange = (e) => { this.props.model.changeName(e.target.value) } handleExpand = (e) => { e.preventDefault() this.setState({ expanded: !this.state.expanded }) } // ... }
在類別元件裡,當你把方法傳遞給子元件的時候,需要確保他們被呼叫的時候使用的是正確的this。一般都會在傳給子元件的時候這麼做:this.handleSubmit.bind(this)
。
使用ES6的箭頭法就簡單多了。它會自動維護正確的上下文(this
)。
給setState傳入一個方法
在上面的範例裡面有這麼一行:
this.setState({ expanded: !this.state.expanded });
setState
其實是異步的! React為了提高效能,會把多次呼叫的setState
放在一起呼叫。所以,呼叫了setState
之後state不一定會立刻就改變。
所以,呼叫setState
的時候,你不能依賴目前的state值。因為i根本不知道它是值會是神馬。
解決方法:給setState
傳入一個方法,把呼叫前的state值當作參數傳入這個方法。看看範例:
this.setState(prevState => ({ expanded: !prevState.expanded }))
感謝Austin Wood的幫助。
拆解元件
import React, { Component } from 'react' import { observer } from 'mobx-react' import { string, object } from 'prop-types' import ExpandableForm from './ExpandableForm' import './styles/ProfileContainer.css' export default class ProfileContainer extends Component { state = { expanded: false } static propTypes = { model: object.isRequired, title: string } static defaultProps = { model: { id: 0 }, title: 'Your Name' } handleSubmit = (e) => { e.preventDefault() this.props.model.save() } handleNameChange = (e) => { this.props.model.changeName(e.target.value) } handleExpand = (e) => { e.preventDefault() this.setState(prevState => ({ expanded: !prevState.expanded })) } render() { const { model, title } = this.props return () } }
{title}
有多行的props
的,每一個prop都應該單獨佔據一行。就如上例一樣。要達到這個目標最好的方法是使用一套工具:Prettier
。
裝飾者(Decorator)
@observer export default class ProfileContainer extends Component {
如果你了解某些函式庫,例如mobx,你就可以用上例的方式來修飾類別組件。裝飾器就是把類別組件當作一個參數傳入了一個方法。
裝飾器可以編寫更靈活、更有可讀性的元件。如果你不想用裝飾器,你可以這樣:
class ProfileContainer extends Component { // Component code } export default observer(ProfileContainer)
閉包
盡量避免在子元件中傳入閉包,如:
<input> { model.name = e.target.value }} // ^ Not this. Use the below: onChange={this.handleChange} placeholder="Your Name"/>
注意:如果input
是一個React元件的話,這樣自動觸發它的重繪,不管其他的props是否發生了改變。
一致性檢定是React最消耗資源的部分。不要把額外的工作加到這裡。處理上例中的問題最好的方法是傳入一個類別方法,這樣還會更易讀,也更容易除錯。如:
import React, { Component } from 'react' import { observer } from 'mobx-react' import { string, object } from 'prop-types' // Separate local imports from dependencies import ExpandableForm from './ExpandableForm' import './styles/ProfileContainer.css' // Use decorators if needed @observer export default class ProfileContainer extends Component { state = { expanded: false } // Initialize state here (ES7) or in a constructor method (ES6) // Declare propTypes as static properties as early as possible static propTypes = { model: object.isRequired, title: string } // Default props below propTypes static defaultProps = { model: { id: 0 }, title: 'Your Name' } // Use fat arrow functions for methods to preserve context (this will thus be the component instance) handleSubmit = (e) => { e.preventDefault() this.props.model.save() } handleNameChange = (e) => { this.props.model.name = e.target.value } handleExpand = (e) => { e.preventDefault() this.setState(prevState => ({ expanded: !prevState.expanded })) } render() { // Destructure props for readability const { model, title } = this.props return (// Newline props if there are more than two ) } }
{title}
{ model.name = e.target.value }} // Avoid creating new closures in the render method- use methods like below onChange={this.handleNameChange} placeholder="Your Name"/>
方法组件
这类组件没有state没有props,也没有方法。它们是纯组件,包含了最少的引起变化的内容。经常使用它们。
propTypes
import React from 'react' import { observer } from 'mobx-react' import { func, bool } from 'prop-types' import './styles/Form.css' ExpandableForm.propTypes = { onSubmit: func.isRequired, expanded: bool } // Component declaration
我们在组件的声明之前就定义了propTypes
。
分解Props和defaultProps
import React from 'react' import { observer } from 'mobx-react' import { func, bool } from 'prop-types' import './styles/Form.css' ExpandableForm.propTypes = { onSubmit: func.isRequired, expanded: bool, onExpand: func.isRequired } function ExpandableForm(props) { const formStyle = props.expanded ? {height: 'auto'} : {height: 0} return () }
我们的组件是一个方法。它的参数就是props
。我们可以这样扩展这个组件:
import React from 'react' import { observer } from 'mobx-react' import { func, bool } from 'prop-types' import './styles/Form.css' ExpandableForm.propTypes = { onSubmit: func.isRequired, expanded: bool, onExpand: func.isRequired } function ExpandableForm({ onExpand, expanded = false, children, onSubmit }) { const formStyle = expanded ? {height: 'auto'} : {height: 0} return () }
现在我们也可以使用默认参数来扮演默认props的角色,这样有很好的可读性。如果expanded
没有定义,那么我们就把它设置为false
。
但是,尽量避免使用如下的例子:
const ExpandableForm = ({ onExpand, expanded, children }) => {
看起来很现代,但是这个方法是未命名的。
如果你的Babel配置正确,未命名的方法并不会是什么大问题。但是,如果Babel有问题的话,那么这个组件里的任何错误都显示为发生在 里的,这调试起来就非常麻烦了。
匿名方法也会引起Jest其他的问题。由于会引起各种难以理解的问题,而且也没有什么实际的好处。我们推荐使用function
,少使用const
。
装饰方法组件
由于方法组件没法使用装饰器,只能把它作为参数传入别的方法里。
import React from 'react' import { observer } from 'mobx-react' import { func, bool } from 'prop-types' import './styles/Form.css' ExpandableForm.propTypes = { onSubmit: func.isRequired, expanded: bool, onExpand: func.isRequired } function ExpandableForm({ onExpand, expanded = false, children, onSubmit }) { const formStyle = expanded ? {height: 'auto'} : {height: 0} return () } export default observer(ExpandableForm)
只能这样处理:export default observer(ExpandableForm)
。
这就是组件的全部代码:
import React from 'react' import { observer } from 'mobx-react' import { func, bool } from 'prop-types' // Separate local imports from dependencies import './styles/Form.css' // Declare propTypes here, before the component (taking advantage of JS function hoisting) // You want these to be as visible as possible ExpandableForm.propTypes = { onSubmit: func.isRequired, expanded: bool, onExpand: func.isRequired } // Destructure props like so, and use default arguments as a way of setting defaultProps function ExpandableForm({ onExpand, expanded = false, children, onSubmit }) { const formStyle = expanded ? { height: 'auto' } : { height: 0 } return () } // Wrap the component instead of decorating it export default observer(ExpandableForm)
某些情况下,你会做很多的条件判断:
<p> {props.downloadMode && currentImage && !currentImage.video && currentImage.blogText ? !currentImage.submitted && !currentImage.posted ? </p><p>Please contact us for content usage</p> : currentImage && currentImage.selected ? <button>Deselect</button> : currentImage && currentImage.submitted ? <button>Submitted</button> : currentImage && currentImage.posted ? <button>Posted</button> : <button>Select post</button> }
这么多层的条件判断可不是什么好现象。
有第三方库JSX-Control Statements可以解决这个问题。但是与其增加一个依赖,还不如这样来解决:
<p> { (() => { if(downloadMode && !videoSrc) { if(isApproved && isPosted) { return </p><p>Right click image and select "Save Image As.." to download</p> } else { return <p>Please contact us for content usage</p> } } // ... })() }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
以上是React組件的使用詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

H5通過多媒體支持、離線存儲和性能優化提升網頁用戶體驗。 1)多媒體支持:H5的和元素簡化開發,提升用戶體驗。 2)離線存儲:WebStorage和IndexedDB允許離線使用,提升體驗。 3)性能優化:WebWorkers和元素優化性能,減少帶寬消耗。

HTML5代碼由標籤、元素和屬性組成:1.標籤定義內容類型,用尖括號包圍,如。 2.元素由開始標籤、內容和結束標籤組成,如內容。 3.屬性在開始標籤中定義鍵值對,增強功能,如。這些是構建網頁結構的基本單位。

HTML5是構建現代網頁的關鍵技術,提供了許多新元素和功能。 1.HTML5引入了語義化元素如、、等,增強了網頁結構和SEO。 2.支持多媒體元素和,無需插件即可嵌入媒體。 3.表單增強了新輸入類型和驗證屬性,簡化了驗證過程。 4.提供了離線和本地存儲功能,提升了網頁性能和用戶體驗。

H5代碼的最佳實踐包括:1.使用正確的DOCTYPE聲明和字符編碼;2.採用語義化標籤;3.減少HTTP請求;4.使用異步加載;5.優化圖像。這些實踐能提升網頁的效率、可維護性和用戶體驗。

Web标准和技术从HTML4、CSS2和简单的JavaScript演变至今,经历了显著的发展。1)HTML5引入了Canvas、WebStorage等API,增强了Web应用的复杂性和互动性。2)CSS3增加了动画和过渡功能,使页面效果更加丰富。3)JavaScript通过Node.js和ES6的现代化语法,如箭头函数和类,提升了开发效率和代码可读性,这些变化推动了Web应用的性能优化和最佳实践的发展。

H5不僅僅是HTML5的簡稱,它代表了一個更廣泛的現代網頁開發技術生態:1.H5包括HTML5、CSS3、JavaScript及相關API和技術;2.它提供更豐富、互動、流暢的用戶體驗,能在多設備上無縫運行;3.使用H5技術棧可以創建響應式網頁和復雜交互功能。

H5與HTML5指的是同一個東西,即HTML5。 HTML5是HTML的第五個版本,帶來了語義化標籤、多媒體支持、畫布與圖形、離線存儲與本地存儲等新功能,提升了網頁的表現力和交互性。

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Dreamweaver Mac版
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具

禪工作室 13.0.1
強大的PHP整合開發環境