niuyulong101369 2019-12-23 16:54 采纳率: 0%
浏览 541

react删除数组内的对象,画面表示不对

react做了一个简单的追加删除功能,两个button,
add在画面上加一个text控件,delete删除控件,

问题:追加text,放在数组里,删除的时候从第1个删除,但是实际删的是最后一个

代码如下

import React,{Component} from 'react';

var textList = [];

export default class Test extends Component {
    constructor(props){
        super(props);
        this.state = {
            children:[],
        }
        this.add = this.add.bind(this);
        this.delete = this.delete.bind(this);
    }
    add(){
        let length = this.state.children.length;
        textList.splice(this.state.children.length, 1, <input type="text" ref={(ref)=>this["text" + length] = ref}/>);
        this.setState({children : textList});
    }

    delete(){
        textList.splice(0,1);
        this.setState({children : textList});
    }

    render(){
        return(
        <div>
            <input type="button" onClick={this.add} value="add"/>
            <input type="button" onClick={this.delete} value="delete"/>
            <div id="div" ref={(ref)=>this.ref = ref}>
                {this.state.children}
            </div>
        </div>)
    }
}

追加后在文本框里写入012,点删除应该删除0的入力框,结果删除的是3
图片说明

  • 写回答

2条回答

  • dabocaiqq 2019-12-23 17:07
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?