react简述,以及rails react的对比。

对于rails中集成react组件有两个比较好的gem可供选择。

1. react_rails

这个gem相对简单一点,有一个很好的例子可供参考。

reactjs-a-guide-for-rails-developers

2. react_on_rails

这个gem复杂些,对环境要求比较高。

环境要求

  1. nodejs
  2. yarn
  3. foreman – gem install foreman

recet_on_rails example

本人使用react_rails和react_on_rails两个gem实现了同一个例子。
react_rails是使用coffee编写。
react_on_rails是使用原生的jsx编写。
源码

react_rails主文件在 app/assets/javascripts/components

react_on_rails主文件夹在
client/app/bundles/HelloWorld/components/records下。

该如何选择:
简单的使用react_rails.
复杂的使用react_on_rails

3. 纯前端的React Deomo

  • 主要是实现了一个简单的TODO功能。
  • UI是从bootstrsap切换成了Ant-Design.
  • react源码,wiki中有介绍如何接入ant-design.其实官方文档里已经说明了如何使用。
  • Ant-Design官网

react jsx语法规范

Note

  1. 每次在组件中调用方法,在执行该方法时会报错找不到this(null),这个时候需要去手动绑定,在构造函数的函数里。
    constructor(props) {
    super(props);
    this.state = {
         name: 'xiaozhu'
    }
    this.example = this.example.bind(this);
  }

  example() {
        // do somthing
    }

这样很麻烦吧,可以换成es6的写法:

constructor(props) {
    super(props);
    this.state = {
         name: 'xiaozhu'
    }
    //this.example = this.example.bind(this);
  }

  example = (param) => {
        // do somthing
    }

这样写,就不需要自己去手动绑定了。
参考
具体的不是es6的效果请查阅
2. 组件之间进行通信,需要在对应的组件上添加属性。

// OneComponent
render(){
    return(
        <ExampleComponent handleOperation={this.doSomthing}>
    )
}

doSomthing = (param) => {
    // do somthing
}


// ExampleComponent
render(){
    return(
        <h1>Hi,Xiaozhu</h1>
        <a className='btn btn-default' onClick={this.handleToUpdate}>
        Update
      </a>
    )
}

handleToUpdate = (e) => {
    var _this = this
    e.preventDefault();
    $.ajax({
        ...
        success: function(data){
            _this.props.handleOperation (data)
        }
    })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值