美文网首页
React问题汇总

React问题汇总

作者: 赵一矛 | 来源:发表于2018-08-31 17:29 被阅读0次

React和AJax的使用

在页面中动态显示从后台获取到的数据,这个时候需要发送ajax请求.
//在hook函数componentDidMount中进行数据请求,并把获取的数据更新到组件状态中。

   componentDidMount() {
    //先执行Ajax数据请求,全局的get方法
      this.serverRequest = $.get("http://127.0.0.1:8081", function (result) {
        console.log(result)
      let items=getItem(result);
      console.log(items)
     //在根据数据更新组件状态
        this.setState({
        value:items
        });
      }.bind(this));
  }

动态生成表格时的响应事件

在动态生成表格时候,需要一个点击事件,但是总是报错,原因是this没有绑定正确。

 let trs=this.state.value.map(function(item,i){
     return (<tr key={i} id={item.barcode}>
     <td>{item.name}</td>
     <td>{item.unit}</td>
     <td>{item.price}元</td>
     <td>{item.charge}</td>
     <td> <button onClick={this.handleClick}>加入购物车</button><input type="text" placeholder="请输入数量" className="inputNumber" ></input></td></tr>)
   }.bind(this))

React中动态生成完整的表格

let trs=this.state.value.map(function(item,i){
     return (<tr key={i} id={item.barcode}>
     <td>{item.name}</td>
     <td>{item.unit}</td>
     <td>{item.price}元</td>
     <td>{item.charge}</td>
     <td> <button onClick={this.handleClick}>加入购物车</button><input type="text" placeholder="请输入数量" className="inputNumber" ></input></td></tr>)
   }.bind(this))
    return (    
      <div className="App">
      <div id="shop">
      <h1 align="center">购物商城</h1>
       <table id="itemtable" className="table table-striped">
        <thead>
          <tr>
           <th>名称</th>
           <th>规格</th>
           <th>价格</th>
           <th>优惠</th>
           <th >操作</th>
          </tr>
          </thead>
          <tbody ref="item">
          {trs}
         </tbody>
       </table>
       <button onClick={this.handleCheck}> 查看购物车</button>
       </div>
</div

React中点击表格中的按钮,想要获取该按钮的id.

 event = event.nativeEvent;
    const tr = event.target.parentNode.parentNode;

tr.id便是该行的id.

相关文章

  • React Native

    常见问题汇总 [新手提问前先来这里看看]React Native的常见问题 React Native疑难点,问题深...

  • React问题汇总

    React和AJax的使用 在页面中动态显示从后台获取到的数据,这个时候需要发送ajax请求.//在hook函数c...

  • react问题汇总

    hashHistory.push传参 跳转之后页面接收参数 input设置默认值 定时触发事件,setTimeout

  • React Native问题汇总

    (1) com.android.builder.testing.api.DeviceException: No c...

  • React Native问题汇总

    初学 react-native,遇到N多的坑,在这里记录一下,便于以后查找: 环境配置好,一切OK,运行时出现以下...

  • React Native问题汇总

    说明:这是一篇用来记录和收集自己或大家使用react native开发时遇到的坑或问题,大家可以通过评论的方式来分...

  • React Native 问题汇总

    1、Make sure Packager server is running.Make sure Chrome i...

  • packages/react/React.js解析

    React.js 这个文件是react这个包的对外的导出汇总。会把和react包相关的一些api汇总和导出。 主要...

  • React学习资源汇总

    React学习资源汇总 React 官方 官网地址:http://facebook.github.io/react...

  • ReactNative:RCTJSONStringify() e

    React Native 问题汇总: Bug: 原因:原生端在与JS端产生数据交互时是否传值中数据格式出现问题。 ...

网友评论

      本文标题:React问题汇总

      本文链接:https://www.haomeiwen.com/subject/kbkewftx.html