這裏是我當前的代碼: (2個文件&類: 「FoodStandComponent.jsx」)重構代碼不具有的setState的Html
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import uuid from 'uuid/v4';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
import React, { Component } from 'react';
import ProviderInfos from '../ProviderInfos/ProviderInfos';
import InputType from './InputType/InputType';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
const propTypes = {
newInput: React.PropTypes.array,
exportInput: React.PropTypes.func,
};
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
class FoodStandComponent extends Component {
constructor(props) {
super(props);
this.state = {
newInput: [
{ name: 'Pretzel', id: uuid() },
{ name: 'Curry', id: uuid() },
{ name: 'Wurst', id: uuid() },
],
InValue: '',
};
this.add = this.add.bind(this);
this.remove = this.remove.bind(this);
}
add = (name) => {
const ninput = this.state.newInput.concat({ name, id: uuid(), value: this.state.InValue });
this.setState({
newInput: ninput,
InValue: '',
});
};
remove = (id, name) => {
const toBeRemoved = this.state.newInput.filter(x => x.name === name).pop();
if (toBeRemoved) {
this.setState({
newInput: this.state.newInput.filter(x => x.name !== name).concat(
this.state.newInput.filter(x => x.name === name && x.id !== toBeRemoved.id),
),
});
}
};
render() {
console.log();
const cubeFifteenOrUnder = this.state.newInput.filter(x => x.name === 'Pretzel')
&& this.state.newInput.filter(x => x.name === 'Pretzel').length <= 13;
const dsoFifteenOrUnder = this.state.newInput.filter(x => x.name === 'Curry')
&& this.state.newInput.filter(x => x.name === 'Curry').length <= 13;
const multiFifteenOrUnder = this.state.newInput.filter(name => name === 'Wurst')
&& this.state.newInput.filter(x => x.name === 'Wurst').length <= 13;
return (
<Card>
<CardBlock className="main-table">
<fieldset>
<legend>Pretzels</legend>
<InputGroup>
<Input placeholder="Pretzel" />
<ProviderInfos type="Pretzel" />
{ cubeFifteenOrUnder && (
<div className="plus" onClick={() => this.add('Pretzel')}>
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
{ !cubeFifteenOrUnder && (
<div className="plus-off">
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
</InputGroup>
{this.state.newInput.map((mapStorageVariable) => {
if (mapStorageVariable.name === 'Pretzel') {
return (<InputType
id={mapStorageVariable.id}
placeholder={mapStorageVariable.name}
value={mapStorageVariable.value}
onRemove={() => this.remove(mapStorageVariable.id, mapStorageVariable.name)}
/>);
}
return null;
})}
</fieldset>
<fieldset>
<legend>Curries</legend>
<InputGroup>
<Input placeholder="Curry" />
<ProviderInfos type="Curry" />
{ dsoFifteenOrUnder && (
<div className="plus" onClick={() => this.add('Curry')}>
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
{ !dsoFifteenOrUnder && (
<div className="plus-off">
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
</InputGroup>
{this.state.newInput.map((mapStorageVariable) => {
if (mapStorageVariable.name === 'Curry') {
return (<InputType
id={mapStorageVariable.id}
placeholder={mapStorageVariable.name}
value={mapStorageVariable.value}
onRemove={() => this.remove(mapStorageVariable.id, mapStorageVariable.name)}
/>);
}
return null;
})}
</fieldset>
<fieldset>
<legend>Wursts</legend>
<InputGroup>
<Input placeholder="Wurst" />
<ProviderInfos type="Wurst" />
{ multiFifteenOrUnder && (
<div className="plus" onClick={() => this.add('Wurst')}>
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
{ !multiFifteenOrUnder && (
<div className="plus-off">
<i className="fa fa-plus" aria-hidden="true" />
</div>
) }
</InputGroup>
{this.state.newInput.map((mapStorageVariable) => {
if (mapStorageVariable.name === 'Wurst') {
return (<InputType
id={mapStorageVariable.id}
placeholder={mapStorageVariable.name}
value={mapStorageVariable.value}
onRemove={() => this.remove(mapStorageVariable.id, mapStorageVariable.name)}
/>);
}
return null;
})}
</fieldset>
<Button color="secondary">Options</Button>{' '}
<Button id="btn">Exécuter</Button>
</CardBlock>
</Card>
);
}
}
SearchExtendedComponent.propTypes = propTypes;
export default SearchExtendedComponent;
(和InputTypeComponent.jsx)
/* ************************************* */
/* ******** IMPORTS ******** */
/* ************************************* */
import ProviderInfos from '../../ProviderInfos/ProviderInfos';
import React, { Component } from 'react';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
/* ************************************* */
/* ******** VARIABLES ******** */
/* ************************************* */
/* ************************************* */
/* ******** COMPONENT ******** */
/* ************************************* */
export default class InputTypeComponent extends Component {
constructor(props) {
super(props);
this.state = {
type: '',
};
}
onRemove =() => {
this.props.onRemove(this.props.id);
}
onChange =() => {
this.props.onChange(this.props.id);
}
render() {
const { placeholder, id, value } = this.props;
const { type } = this.state;
this.type = placeholder;
return (
<InputGroup key={id}>
<Input placeholder={placeholder} />{value}
<ProviderInfos type={this.type} />
<div className="minus" onClick={this.onRemove}>
<i className="fa fa-minus" aria-hidden="true" />
</div>
</InputGroup>
);
}
}
我m試圖通過將「添加」和「刪除」按鈕重構爲一個函數來獲取列表。
正如你可以看到以上感謝@Jacky Choo的回答&代碼我幾乎在那裏,但問題是,我已經失去了我以前的功能,我想刪除刪除線,當我點擊它自己刪除按鈕。
當我點擊這個減號時,包含文本和改變的複選框的行保留。 和最後一行消失。
更新:
修好了! 通過改變刪除這個我得到我的預期結果。是的,刪除的下面的行會被重置,但是Redux可以處理這些行。對@Jacky Choo大肆宣揚,他基本上爲我着想!
remove = (id, name) => {
this.setState({
newInput: this.state.newInput.filter(x => x.name === name && x.id !== id),
});
};
我覺得你這樣做是錯的,如果你把HTML到您的狀態開始。理想狀態應該是那個。該HTML基於從該州收到的數據呈現, – dcodesmith
我知道這就是爲什麼我試圖解決它....幫助我! – tatsu
那麼你可以返回不同的函數,返回HTML基於一個handlerVariable – Nocebo