React从入门到精通系列之(4)组件化和Props传递
四、组件化和属性(props)组件允许您将UI拆分为独立的可重用的部分,并单独地考虑每个部分。 功能性组件(functional)和类组件(class component)定义组件的最简单的方法是编写JavaScript函数: function Welcome(props) { return <h1>hello,{props.name}</h1> } 此函数是个有效的React组件,因为它接收一个带有数据的“props”对象作为参数,并返回一个React元素。 我们把这样的组件称为“功能性组件(functional)”,因为它们是个JavaScript函数。 您还可以使用ES6类来定义组件: class Welcome extends React.Component { render() { return <h1>hello,{this.props.name}</h1>; } } 上述两个组件从React的角度来看是等效的。 类组件有一些额外的功能,我们将在下面的章节中讨论。 在那之前,我们将简单地使用功能组件。 渲染一个组件以前,我们只遇到使用DOM标签的React元素: const element = <div />; 但是,元素也可以表示用户自定义的组件: const element = <Welcome name="zhangyatao" />; 当React看到表示用户定义组件的元素时,它将该JSX标签的所以属性放到一个对象中传递给组件。 我们称这个对象为“props”。 例如,此代码在页面上呈现“Hello,zhangyatao”: function Welcome(props) { return <h1>Hello,{props.name}</h1>; } const element = <Welcome name="zhangyatao" />; ReactDOM.render( element,document.getElementById('root') ); 让我们回顾一下在这个例子中发生了什么:
编写组件组件可以在其返回值中引用去其他组件。 这允许我们对任何级别的细节使用相同的组件抽象。 一个按钮,一个表单,一个对话框,一个屏幕:在React应用程序中,所有这些通常表示为组件。 function Welcome(props) { return <h1>Hello,{props.name}</h1>; } function App() { return ( <div> <Welcome name="zhangyatao" /> <Welcome name="jiangyanyun" /> <Welcome name="pangyun" /> </div> ); } ReactDOM.render( <App />,document.getElementById('root') ); 这个新的React应用程序在顶部有一个单独的
抽离组件永远不要害怕将组件拆分成更小的组件。 import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date.toISOString(); } function Comment(props) { return ( <div className="Comment"> <div className="UserInfo"> <img className="avatar" src={props.author.avatarUrl} alt={props.author.name} /> <div className="UserInfo-name"> {props.author.name} </div> </div> <div className="Comment-text"> {props.text} </div> <div className="Comment-date"> {formatDate(props.date)} </div> </div> ); } ReactDOM.render( <Comment author={{ avatarUrl: 'https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/3ae1dc06.jpg',name: 'zhangyatao' }} text={'我的名字叫张亚涛'} date={new Date()}/>,document.getElementById('root') ); 它接受 首先,我们将提取 function Avatar(props) { return ( <img className="Avatar" src={props.user.avatarUrl} alt={props.user.name} /> ); }
我们现在可以对 function Comment(props) { return ( <div className="Comment"> <div className="UserInfo"> <Avatar user={props.author} /> <div className="UserInfo-name"> {props.author.name} </div> </div> <div className="Comment-text"> {props.text} </div> <div className="Comment-date"> {formatDate(props.date)} </div> </div> ); } 接下来,我们将提取一个 function UserInfo(props) { return ( <div className="UserInfo"> <avatar uer={props.user} /> <div className="UserInfo-name"> {props.user.name} </div> </div> ); } 这使我们可以进一步简化 function Comment(props) { return ( <div className="Comment"> <UserInfo user={props.author} /> <div className="Comment-text"> {props.text} </div> <div className="Comment-date"> {formatDate(props.date)} </div> </div> ); } 最终的代码如下: import React from 'react'; import ReactDOM from 'react-dom'; function formatDate(date) { return date.toISOString(); } function Avatar(props) { return ( <img className="Avatar" src={props.user.avatarUrl} alt={props.user.name} /> ); } function UserInfo(props) { return ( <div className="UserInfo"> <Avatar user={props.user}/> <div className="UserInfo-name"> {props.user.name} </div> </div> ); } function Comment(props) { return ( <div className="Comment"> <UserInfo user={props.author}/> <div className="Comment-text"> {props.text} </div> <div className="Comment-date"> {formatDate(props.date)} </div> </div> ); } ReactDOM.render( <Comment author={{ avatarUrl: 'https://ss0.bdstatic.com/7Ls0a8Sm1A5BphGlnYG/sys/portrait/item/3ae1dc06.jpg',document.getElementById('root') ); 让一个个可重用组件在大型应用程序中交付使用的过程中,抽离组件起初可能看起来像又脏又累的活儿。 所以有一个好的经验法则:如果UI的一部分被使用了好几次(按钮,面板,头像),或者内部比较复杂的东西(App,FeedStory,评论),一个可重用的组件对它来说可以达到最大的发挥空间。 Props是只读的无论是将组件声明为功能组件还是类组件,它都不能修改自己的props。 考虑这个计算参数总和的函数: function sum(a,b) { return a + b; } 这样的函数被称为 function withdraw(account,amount) { account.total -= amount; } React非常灵活,但它有一个严格的规则: 当然,应用中的UI大部分是动态的,随时间变化。 在下一节中,我们将介绍一个“state”的新概念。 状态允许React组件响应用户操作,网络响应和其他任何内容,随时间更改其输出,而不违反此规则。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |