React Learn Note 4

React学习笔记(四)

标签(空格分隔): React JavaScript


三、组件&Props

组件可以将UI切分成一些独立的、可复用的部件,这样你就只需专注于构建每一个单独的部件。

组件接收props,返回react元素。

1. 函数定义\类定义组件

最简单组件方式 - 函数定义组件

// 函数定义组件
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}

ES6 class定义组件,效果同上:

// ES6 class定义组件
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

2. 组件渲染

将组件作为React元素,标签的属性作为props键值:

const element5_1 = <Welcome name="Sara"></Welcome>;
ReactDOM.render(
element5_1,
document.getElementById('root5')
);

**警告:**
组件名称必须大写字母开头。

3. 组合组件

React组件也可以嵌套。

function App() {
return (
<div>
<Welcome name="Bob"></Welcome>
<Welcome name="Cahal"></Welcome>
<Welcome name="John"></Welcome>
</div>
);
} ReactDOM.render(
<App></App>,
document.getElementById('root6')
);

**警告:**
组件的返回值只能有一个根元素。所以将多个`Welcome`元素用`div`包裹。

4. 提取组件

可以将组件切分为更小的组件。

function formatDate(date) {
return date.toLocaleTimeString();
}
function Comment(props) {
return (
<div className="Comment">
<div className="UserInfo">
<img 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: '../static/img/zhifibao_key.jpg', name: 'Jessie'}} text="This is comment text." date={new Date()}></Comment>,
document.getElementById('root7')
);

这个组件接收author(对象)、text(字符串)、以及date(Date对象)作为props。是个复杂的组件。接下来我们提取拆分这个组件。

首先提取Avatar组件:

// 提取组件
function Avatar(props) {
return (
<img src={props.user.avatarUrl} alt={props.user.name} className="Avatar"/>
);
} function UserInfo(props) {
return (
<div className="UserInfo">
<Avatar user={props.user}></Avatar>
<div className="UserInfo-name">
{props.user.name}
</div>
</div>
);
} // 最终Comment组件被简化为
function Comment2(props) {
return (
<div className="Comment">
<UserInfo user={props.author}></UserInfo>
<div className="Comment-text">
{props.text}
</div>
<div className="Comment-date">
{formatDate(props.date)}
</div>
</div>
);
} ReactDOM.render(
<Comment2 author={{avatarUrl: '../static/img/zhifibao_key.jpg', name: 'Xiaoyu Lee'}} text="Wow, this is very beautiful." date={new Date()}></Comment2>,
document.getElementById('root8')
);

5. Props的只读性

无论是使用函数或是类来声明一个组件,它决不能修改它自己的props。

The end...    Last updated by: Jehorn, Jan 07, 2018, 5:44 PM

最新文章

  1. ST教学分析:相同行为连续数
  2. 如何在Ubuntu下的VirtualBox虚拟机(Windows XP)里挂载/使用U盘 (转载)
  3. 为什么是 n(n+1)/2 ?
  4. C#开发中可能会用到的一些小贴士(转)
  5. JS中检测数据类型的四种方式及每个方式的优缺点
  6. 解决tomcat占用8080端口问题图文教程
  7. [转]Linux之od命令
  8. 如何提高多线程程序的cpu利用率
  9. 电话qie听器
  10. javascript:;”是什么意思
  11. 单尺度二维离散小波分解dwt2
  12. 基于webpack的React项目搭建(三)
  13. float之脱离文档流
  14. DSAPI实现简单的透明窗体
  15. Jace 上新建 Station 配置 笔记
  16. [Android] 免费天气预报接口
  17. phpstudy设置允许远程访问mysql数据库
  18. 迅为开发板4412开发板-ANROID系统的烧写方法分享
  19. Debian/Ubuntu pip default install to $HOME/.local
  20. js-倒计时应用

热门文章

  1. 打开页面时就提交,可以做一些自动登陆 还有SICLOGIN的测试代码
  2. 选择排序 Selection Sort
  3. UVALive - 3722 找规律
  4. css 之 BFC
  5. pycharm中使用正则表达式批量添加print括号,完美从python2迁移到python3
  6. kvm 虚拟网络命令操作
  7. Oracle RAC集群添加节点
  8. Js简易代码生成工具
  9. C++ Memory System Part2: 自定义new和delete
  10. JS常用的设计模式(4)——适配器模式