Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript array.splice不工作始终删除最后一个元素 add(){ rendercomponent.push(UiInput); childJson.push(this.dynamicJson()) this.setState({documents:rendercomponent,Json:childJson}); } componentDidMount(){ rendercomponent.push(UiInput); this.setState({documents:rendercomponent}) } 戴维森(){ 返回{ “服务”:this.state.formValues.text, “tax”:this.state.formValues.tax, “金额”:this.state.formValues.amount } } popComponent=(索引)=>{ log(`in parent-->${index}`) rendercomponent.拼接(索引1); 拼接(索引,1); this.setState({documents:rendercomponent,Json:childJson,formValues:{}) }; render(){ 返回( {this.state.documents.map((元素,索引)=>{ 返回此.popComponent(索引)}/> })} ); }_Javascript_Reactjs - Fatal编程技术网

Javascript array.splice不工作始终删除最后一个元素 add(){ rendercomponent.push(UiInput); childJson.push(this.dynamicJson()) this.setState({documents:rendercomponent,Json:childJson}); } componentDidMount(){ rendercomponent.push(UiInput); this.setState({documents:rendercomponent}) } 戴维森(){ 返回{ “服务”:this.state.formValues.text, “tax”:this.state.formValues.tax, “金额”:this.state.formValues.amount } } popComponent=(索引)=>{ log(`in parent-->${index}`) rendercomponent.拼接(索引1); 拼接(索引,1); this.setState({documents:rendercomponent,Json:childJson,formValues:{}) }; render(){ 返回( {this.state.documents.map((元素,索引)=>{ 返回此.popComponent(索引)}/> })} ); }

Javascript array.splice不工作始终删除最后一个元素 add(){ rendercomponent.push(UiInput); childJson.push(this.dynamicJson()) this.setState({documents:rendercomponent,Json:childJson}); } componentDidMount(){ rendercomponent.push(UiInput); this.setState({documents:rendercomponent}) } 戴维森(){ 返回{ “服务”:this.state.formValues.text, “tax”:this.state.formValues.tax, “金额”:this.state.formValues.amount } } popComponent=(索引)=>{ log(`in parent-->${index}`) rendercomponent.拼接(索引1); 拼接(索引,1); this.setState({documents:rendercomponent,Json:childJson,formValues:{}) }; render(){ 返回( {this.state.documents.map((元素,索引)=>{ 返回此.popComponent(索引)}/> })} ); },javascript,reactjs,Javascript,Reactjs,array.splice工作不正常它总是从数组中删除最后一个元素 在我的UI中有一个添加组件的按钮,在该按钮上调用add()函数 还有delete按钮,在delete按钮上调用popmonent()函数 我想删除特定的元素,它总是从redercomponent数组中删除最后一个元素 请帮助我对该问题进行排序拼接将使对象渲染组件发生变异,但不会更改该对象的引用。如果引用未更改,则react不会重新渲染它,因为根据react的扩散算法,它不会有所不同。因此,您可以使用切片而不是拼接。这样,新对象的引

array.splice
工作不正常它总是从数组中删除最后一个元素

在我的UI中有一个添加组件的按钮,在该按钮上调用
add()
函数

还有delete按钮,在delete按钮上调用
popmonent()
函数

我想删除特定的元素,它总是从redercomponent数组中删除最后一个元素


请帮助我对该问题进行排序

拼接
将使对象渲染组件发生变异,但不会更改该对象的引用。如果引用未更改,则react不会重新渲染它,因为根据react的扩散算法,它不会有所不同。因此,您可以使用
切片
而不是
拼接
。这样,新对象的引用对于早期对象应该是不同的

检查
rendercomponent!==渲染组件已修改

或许

    add() {
    rendercomponent.push(UiInput);
    childJson.push(this.dynamicJson())
    this.setState({ documents: rendercomponent, Json: childJson });

}
componentDidMount() {
    rendercomponent.push(UiInput);
    this.setState({ documents: rendercomponent })
}
dynamicJson() {
    return {
        "service": this.state.formValues.text,
        "tax": this.state.formValues.tax,
        "amount": this.state.formValues.amount
    }
}
popComponent = (index) => {
    console.log(`in parent-->>${index}`)
    rendercomponent.splice(index, 1);
    childJson.splice(index, 1);
    this.setState({ documents: rendercomponent,Json: childJson,formValues:{} })

};  render() {
    return (
        <div>
    {this.state.documents.map((Element, index) => {
                                    return <Element key={index} index={index} onUpdate={this.onUpdate} popComponent={() => this.popComponent(index)} />
                                })}
          </div>
);
}

如果索引为-1,
rendercomponent.splice(索引为1)
只会删除数组的最后一项

上面的代码似乎有点缺失。缺失的是什么,我只在问题中添加了必要的函数。你可以发布一个小提琴来显示问题吗?问题是,如果我想删除2索引no元素或任何元素,它总是会删除最后一个元素Yes,但是我想删除特定的元素。我的意思是
rendercomponent.splice(索引,1)在索引位置删除1项。但不知何故,如果索引是
-1
,它只会删除最后一项。这就是重新组件的问题。splice(索引,1)不会删除索引位置的1项,而是删除最后一项,我想删除索引位置的项position@NandkishorShinde请检查nowpopComponent=(索引)=>{var rendercomponentModified=[…rendercomponent.slice(0,索引),…rendercomponent.slice(索引+1)]如果(rendercomponent!=rendercomponentModified){this.setState({documents:rendercomponentModified})}childJson.splice(索引,1);this.setState({Json:childJson,formValues:{}});我是这样写函数的,但它不能删除元素,它添加的元素等于索引
 popComponent = (index) => {
    console.log(`in parent-->>${index}`)
    var rendercomponentModified = [
     ...rendercomponent.slice(0,index),
     ...rendercomponent.slice(index+1)
    ];
    var childJsonModified = [
           ...childJson.slice(0,index), 
           ...childJson.slice(index+1)
    ];
     this.setState({ documents: rendercomponentModified,
    Json: childJsonModified,formValues:{} });
   }