React和ES6(二)--ES6的类和ES7的property initializer
前一篇的内容太简单了,会不会很失望。这次就来一个接近实际应用的例子,对应的React的组件也会更加复杂。这次开发一个购物车的页面。在这个页面中你会看到某个产品的信息,比如:图片、名称和价格。另外,一个用户可以增加和减少该商品的数量。 创建cart_item.html<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>React and ES6 Part 2</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css">
</head>
<body>
<div class="root"></div>
<script src="dist/bundle.js"></script>
</body>
</html>
我们使用了一个CDN来引用样式。添加个样式美化一下这个小应用的内容。 CartItem组件现在开发 import React from 'react';
import ReactDOM from 'react-dom';
import CartItem from './cartItem';
const order = {
title: 'Fresh fruits package',image: 'http://images.all-free-download.com/images/graphiclarge/citrus_fruit_184416.jpg',initialQty: 3,price: 8
};
ReactDOM.render(
<CartItem title={order.title} image={order.image} initialQty={order.initialQty} price={order.price} />,document.querySelector('.root') );
cart.jsx作为购物车功能的总容器。这里引入了
CartItem组件初步定义现在应该给出 import React from 'react';
export default class CartItem extends React.Component {
// 1
constructor(props) {
super(props);
this.state = {
qty: props.initialQty,total: 0
};
}
// 2
componentWillMount() {
this.recalculateTotal();
}
// 3
increaseQty() {
this.setState({qty: this.state.qty + 1},this.recalculateTotal);
}
// 3
decreaseQty() {
let newQty = this.state.qty > 0 ? this.state.qty - 1 : 0;
this.setState({qty: newQty},this.recalculateTotal);
}
// 3
recalculateTotal() {
this.setState({total: this.state.qty * this.props.price});
}
render() {
return (
// ...略...
);
}
}
下面解释一下: CartItem的render方法上一节没有给出 import React from 'react';
export default class CartItem extends React.Component {
// ...略...
render() {
return (
<article className="row large-4"> <figure className="text-center"> <p> <img src={this.props.image} /> </p> <figcaption> <h2>{this.props.title}</h2> </figcaption> </figure> <p className="large-4 column"><strong>Quantity: {this.state.qty}</strong></p> <p className="large-4 column"> <button onClick={this.increaseQty.bind(this)} className="button success">+</button> <button onClick={this.decreaseQty.bind(this)} className="button alert">-</button> </p> <p className="large-4 column"><strong>Price per item:</strong> ${this.props.price}</p> <h3 className="large-12 column text-center"> Total: ${this.state.total} </h3> </article> ); } }
在 不用担心 React在ES6中的Default Props和Props类型假设我们现在需要给 幸好这些在React里都有内置支持。所以,相关代码非常少。 在 CartItem.propTypes = {
title: React.PropTypes.string.isRequired,price: React.PropTypes.number.isRequired,initialQty: React.PropTypes.number
};
CartItem.defaultProps = {
title: "Indefined Product",price: 100,initialQty: 0
};
这时候如果你在cart.jsx文件中给 在项目中使用ES7你可能要问了ES6还没整明白的就开始用上ES7了?我要说的是我们往前看,使用一些已经在静态语言里有的特性并不会造成太大的问题。最关键的问题是 首先安装必要的 然后在.babelrc文件中添加这个preset的配置: {
"presets": ["es2015","react","stage-0"]
}
ES7属性初始化器和React组件的Default Props、Props Types在 export default class CartItem extends React.Component {
static propTypes = {
title: React.PropTypes.string.isRequired,initialQty: React.PropTypes.number
};
static defaultProps = {
title: 'Undefined Product',initialQty: 0
};
constructor(props) {
super(props);
this.state = {
qty: props.initialQty,total: 0
};
}
// ...略...
这样的定义和前面一节在类定义后面再定义default props和props types是一样的效果。但是这样的定义是不是更加的接近静态语言类中定义属性的方式呢?当然之前的default props和props types的定义也可以删去了。 ES7的方式定义React组件的State最后一步就是把initial state(state初始值)从constructor里拿出来放到property initializer(属性初始化器)里。代码如下: export default class CartItem extends React.Component {
state = {
qty: this.props.initialQty,total: 0
};
constructor(props) {
super(props);
// this.state = {
// qty: props.initialQty,
// total: 0
// };
}
//...略...
}
记得把原来constructor里的state代码删了。 总结通过这篇你就可以熟悉了如何用ES6写React的组件,也熟悉了如何使用ES7的property initializer。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |