React数据绑定

上一节说到使用react完成布局和html代码的展示。这一章节将讲解react的数据的绑定。

一、需要使用的React api

1、getInitialState方法,在ES5的语法中用来设置组件的初始化的数据。在该方法中定义要使用的数据模型。
  2、setState方法,用来更新组件的数据并更新视图
  3、forceUpdate方法,强制更新数据和视图
  4、refs属性用来从组件的dom中获取到实际的ref指定的dom元素
  5、this.state.xxxx能够获取当前组件中在getInitialState中的数据模型的值
  6、this.props.xxxx能够从父组件获取传递过来的值

上一节中,定义了两个组件,展示列表组件和编辑列表组件。现完成两个组件的数据的绑定。
分析:1、展示列表组件,该组件具有删除和编辑的功能,需要添加两个事件,remove和edit。
   2、编辑列表组件,该组件提供编辑输入,保存编辑,删除功能,数据由父组件提供。
   3、父组件List提供子组件的数据和子组件方法的具体实现。
具体代码如下:

单独的/**
 * Created by Lenovo on 2017/4/7.
 */
//展示组件
const Item = React.createClass({
//删除方法
remove(){
    this.props.onRemove(this.props.id);
},
//编辑方法
edit(){
    this.props.onEdit(this.props.id,this.props.value);
},
render(){
    return <li className="list-group-item">
        {this.props.value}//此处为通过props来接受从父组件传递过来的数据并绑定到虚拟的dom上
        <a href="#" className="item-right glyphicon glyphicon-remove" onClick={this.remove}></a>
        <a href="#" className="item-right glyphicon glyphicon-edit" onClick={this.edit}></a>
    </li>
}
});

//编辑组件
const EditItem = React.createClass({
getInitialState(){
    return{
        value:''
    }
},
//将录入的数据存入到当前的组件的状态中
changeHandle(event){
    this.setState({value:event.target.value});
},
//保存方法调用父组件中的保存方法
save(){
    this.props.onSave(this.props.id,this.state.value);
},
//删除方法调用父组件中的删除方法
remove(){
    this.props.onRemove(this.props.id);
},
render(){
        return <li className="list-group-item">
            <input type="text" onChange={this.changeHandle} defaultValue={this.props.value}/>
            <a href="#" className="margin-leftandright-5 glyphicon glyphicon-share" onClick={this.save}></a>
            <a href="#" className="margin-leftandright-5 glyphicon glyphicon-remove" onClick={this.remove}></a>
        </li>
}
});

//容器组件
const List = React.createClass({
getInitialState(){
    return{
        key:0,
        List:new Map(),
        eList:new Map()
    }
},
//添加方法的具体实现
add(){
    const key=this.state.key++;
    this.state.eList.set(key,'');
    this.forceUpdate();
},
//保存方法的具体实现
save(id,value){
  this.state.List.set(id,value);
  this.state.eList.delete(id);
  this.forceUpdate();
},
 //展示列表删除方法的具体实现
remove(id){
  this.state.List.delete(id);
  this.forceUpdate();
},
 //编辑列表删除方法的具体实现
eremove(id){
    this.state.eList.delete(id);
    this.forceUpdate();
},
//展示列表编辑方法的具体实现
edit(id,value){
    this.state.eList.set(id,value);
    this.state.List.delete(id);
    this.forceUpdate();
},
render(){
    const ListDom=[];
    const EditListDom=[];
    for(let entity of this.state.List){
        ListDom.push(<Item value={entity[1]} id={entity[0]}
                           onRemove={this.remove}
                           onEdit={this.edit}
        />)
    }
    for(let entity of this.state.eList){
        EditListDom.push(<EditItem value={entity[1]}
                                   id={entity[0]}
                                   onSave={this.save}
                                   onRemove={this.eremove}
        />)
    }
    return <div>
        <button className="menu-btn btn btn-success glyphicon glyphicon-plus" onClick={this.add}>
            添加心事
        </button>
        <ul className="list-group">
            {ListDom}
            {EditListDom}
        </ul>
    </div>
  }
});

 //渲染
ReactDOM.render(
        <List/>,
        document.getElementById('content')
);

本节讲了数据和dom元素绑定,该项目还存在很多的不好的交互行为,需要进行优化,下一节将对该项目进行优化。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原教程内容详见精益 React 学习指南,这只是我在学习过程中的一些阅读笔记,个人觉得该教程讲解深入浅出,比目前大...
    leonaxiong阅读 7,830评论 1 18
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,017评论 25 709
  • 自己最近的项目是基于react的,于是读了一遍react的文档,做了一些记录(除了REFERENCE部分还没开始读...
    潘逸飞阅读 8,833评论 1 10
  • 本笔记基于React官方文档,当前React版本号为15.4.0。 1. 安装 1.1 尝试 开始之前可以先去co...
    Awey阅读 12,305评论 14 128
  • 如果可以拥有超能力,我希望自己能够处于永恒临在状态,与万事万物都有强烈的联结感,一体感,活在当下,同时大脑被开发1...
    蓝梦_宝贝阅读 4,097评论 0 1