React 不使用ES6
来源:互联网 发布:交换机端口介绍 编辑:程序博客网 时间:2024/05/21 19:30
React Without ES6
ES6语法创建组件
class Greeting extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; }}
如果不用ES6语法,就需要用create-react-class
模块创建
var createReactClass = require('create-react-class');var Greeting = createReactClass({ render: function() { return <h1>Hello, {this.props.name}</h1>; }});
声明默认props
ES6 class中把defaultProps
定义为组件的一个属性
class Greeting extends React.Component { // ...}Greeting.defaultProps = { name: 'Mary'};
createReactClass()
中则要定义getDefaultProps()
var Greeting = createReactClass({ getDefaultProps: function() { return { name: 'Mary' }; render: function() { return <h1>Hello, {this.props.name}</h1>; } },});
设置state
初始值
ES6中在constructor中直接设置this.state
class Counter extends React.Component { constructor(props) { super(props); this.state = {count: props.initialCount}; } // ...}
createReactClass()
中则要定义getInitialState()
var Counter = createReactClass({ getInitialState: function() { return {count: this.props.initialCount}; }, // ...});
自动绑定
ES6中不会主动给实例绑定this
,所以要显示绑定
class SayHello extends React.Component { constructor(props) { super(props); this.state = {message: 'Hello!'}; // 这行很重要 this.handleClick = this.handleClick.bind(this); } handleClick() { alert(this.state.message); } render() { // Because `this.handleClick` is bound, we can use it as an event handler. return ( <button onClick={this.handleClick}> Say hello </button> ); }}
createReactClass()
就不需要主动绑定了
var SayHello = createReactClass({ getInitialState: function() { return {message: 'Hello!'}; }, handleClick: function() { alert(this.state.message); }, render: function() { return ( <button onClick={this.handleClick}> Say hello </button> ); }});
虽说ES6的方法更麻烦点,但是实际效果更好。可以通过ES6箭头函数的特性去写(箭头函数体内的this
对象就是定义所在的对象,而不是使用时所在的对象,且不可改变)
class SayHello extends React.Component { constructor(props) { super(props); this.state = {message: 'Hello!'}; } // WARNING: 这个语法还在测试阶段! handleClick = () => { alert(this.state.message); } render() { return ( <button onClick={this.handleClick}> Say hello </button> ); }}
以上方法还属于测试阶段,为了安全使用,可以遵照以下几条建议:
- 在constructor里绑定
this
- 箭头函数调用时
onClick={(e) => this.handleClick(e)}
- 还是使用
createReactClass
阅读全文
0 0
- React 不使用ES6
- 十七、不使用ES6编写React应用
- react使用es6(I)
- react 使用es6(II)
- [译]使用 ES6+ 写 react
- React+ES6
- 在 React.js 中使用 ES6+
- 使用 Webpack 和 ES6 进行 React 开发
- React使用ES6和ES5写法对比
- 使用webpack搭建react ES6开发环境
- 使用ES6语法重构React组件
- 不使用ES6 语法糖
- React-Native ES6使用语法和underscore的使用
- ReactJS学习系列课程(React ES6语法使用)
- 使用 webpack + react + redux + es6 开发组件化前端项目
- 使用 webpack + react + redux + es6 开发组件化前端项目
- 使用 webpack + react + redux + es6 开发组件化前端项目
- 基于ES6,使用React、Webpack、Babel构建模块化JavaScript应用
- 怎样快速写出 AC 的代码
- 深入解析OkHttp3
- python基础知识纵览(下)
- python中文的编码问题(使用decode('gbk').encode('utf-8')和decode('utf-8').encode('gbk'))
- ubuntu centos 安装docker ce 依赖清华大学镜像
- React 不使用ES6
- Light Field 光场以及MATLAB光场工具包(LightField ToolBox)的使用说明
- JEESZ-kafka集群安装
- Servlet
- STM32—无需中断来实现使用DMA接收串口数据(原创)
- (转载)Linux 学习记录 一(安装、基本文件操作)
- rocket mq 的广播模式示例
- windows 下搭建邮件服务器
- Oracle数据库数据导出