1
这里是我当前的代码:删除删除特定集合项目
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import React, { Component } from 'react';
import UUID from 'node-uuid';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
import ProviderInfos from '../ProviderInfos/ProviderInfos';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
export default class PretzelStandComponent extends Component {
constructor(props) {
super(props);
this.state = {
inputPretzel: [],
inputCurry: [],
inputWurst: []
};
this.incrementPretzel = this.incrementPretzel.bind(this);
this.incrementCurry = this.incrementCurry.bind(this);
this.incrementWurst = this.incrementWurst.bind(this);
this.decrementPretzel = this.decrementPretzel.bind(this);
this.decrementCurry = this.decrementCurry.bind(this);
this.decrementWurst = this.decrementWurst.bind(this);
}
componentDidMount() {
this.incrementPretzel();
this.incrementCurry();
this.incrementWurst();
}
incrementPretzel() {
const uuid = require('uuid/v1');
uuid();
const inputPretzel = this.state.inputPretzel;
this.setState({
inputPretzel: inputPretzel.concat(<InputGroup>
<Input placeholder="Pretzel" key={uuid} /><ProviderInfos /></InputGroup>),
});
}
incrementCurry() {
const uuid = require('uuid/v1');
uuid();
const inputCurry = this.state.inputCurry;
this.setState({
inputCurry: inputCurry.concat(<InputGroup>
<Input placeholder="Curry" key={uuid} /><ProviderInfos /></InputGroup>),
});
}
incrementWurst() {
const uuid = require('uuid/v1');
uuid();
const inputWurst = this.state.inputWurst;
this.setState({
inputWurst: inputWurst.concat(<InputGroup>
<Input placeholder="Wurst" key={uuid} /><ProviderInfos /></InputGroup>),
});
}
decrementPretzel() {
this.setState({
inputPretzel: this.state.inputPretzel.splice(this.state.inputPretzel, this.state.inputPretzel.length - 1),
});
}
decrementCurry() {
this.setState({
inputCurry: this.state.inputCurry.splice(this.state.inputCurry, this.state.inputCurry.length - 1),
});
}
decrementWurst() {
this.setState({
inputWurst: this.state.inputWurst.splice(this.state.inputWurst, this.state.inputWurst.length - 1),
});
}
render() {
return (
<Card>
<CardBlock className="main-table">
<fieldset>
<legend>Pretzels</legend>
{this.state.inputPretzel}
<button onClick={this.incrementPretzel}>Add a Pretzel</button>
<button onClick={this.decrementPretzel}>Remove a Pretzel</button>
</fieldset>
<fieldset>
<legend>Curry</legend>
{this.state.inputCurry}
<button onClick={this.incrementCurry}>Add Curry</button>
<button onClick={this.decrementCurry}>Remove Curry</button>
</fieldset>
<fieldset>
<legend>Wurst</legend>
{this.state.inputPretzel}
<button onClick={this.incrementPretzel}>Add Wurst</button>
<button onClick={this.decrementPretzel}>Remove Wurst</button>
</fieldset>
<Button color="secondary">Options</Button>{' '}
<Button id="btn">Exécuter</Button>
</CardBlock>
</Card>
);
}
}
正如你可以看到我有这些元素的三个不同的元素和集合:
- 脆饼
- Currys
- 和香肠
我可以添加它们并删除最后一个。但我想删除每一个。
在我放入setState并添加到每个集合的Html代码中,我想添加一个删除按钮或者在每行旁边都有一个删除按钮,这会删除正确的行。
这实际上是非常好,相当我正在寻找!我继续实施它,并冒昧地将这种单独食物类型的组成部分全面导入。但当我点击一个“x”时,它会删除该食物类型的所有元素 – tatsu
我认为发生了什么是''正在被重新渲染,因为它没有被保存在状态的任何地方。这需要一些返工来将值作为对象的一部分与id一起添加,然后将它传递给'incrementX'函数。如果您需要更多信息,我可以更新示例。 –
是的,没错!请:)你是否试图说应该有一个计数器传递给foodtype组件以及增量和减量函数?我不明白。 – tatsu