0

I'm trying to get a working remove button for my different categories of things but my remove button only adds just like my add button.

I supposed that's to be expected when using "concat" but I don't know how I'm supposed to be doing this.

I got help for adding here : Add Inputs in React with add button

But how do I delete the last element (perhaps cheking that user has not filled it in beforehand)?

/* ************************************* */
/* ********       IMPORTS       ******** */
/* ************************************* */
import React, { Component } from 'react';
import { Card, CardBlock, Button, InputGroup, Input } from 'reactstrap';
import ProviderInfos from '../ProviderInfos/ProviderInfos';

/* ************************************* */
/* ********      VARIABLES      ******** */
/* ************************************* */

/* ************************************* */
/* ********      COMPONENT      ******** */
/* ************************************* */
export default class SearchExtendedComponent extends Component {

    constructor(props) {
        super(props);
        this.state = { inputCurry: [],
            inputWurst: [],
            inputSausage: [],
            inputPretzel: [],
            Curry: 1,
            Wurst: 1,
            Sausage: 1,
            Pretzel: 1 };
        this.incrementCurry = this.incrementCurry.bind(this);
        this.decrementCurry = this.decrementCurry.bind(this);
        this.incrementWurst = this.incrementWurst.bind(this);
        this.decrementWurst = this.decrementWurst.bind(this);
        this.incrementSausage = this.incrementSausage.bind(this);
        this.decrementSausage = this.decrementSausage.bind(this);
        this.incrementPretzel = this.incrementPretzel.bind(this);
        this.decrementPretzel = this.decrementPretzel.bind(this);
    }

    componentDidMount() {
        this.incrementCurry();
        this.incrementWurst();
        this.incrementSausage();
        this.incrementPretzel();
    }

    incrementCurry() {
        const inputCurry = this.state.inputCurry;
        this.setState({
            Curry: this.state.Curry + 1,
            inputCurry: inputCurry.concat(<InputGroup>
                <Input placeholder="Curry" key={this.state.Curry} /><ProviderInfos /></InputGroup>),
        });
    }

    decrementCurry() {
        const inputCurry = this.state.inputCurry;
        this.setState({
            Curry: this.state.Curry - 1,
            inputCurry: inputCurry.concat(<InputGroup>
                <Input placeholder="Curry" key={this.state.Curry} /><ProviderInfos /></InputGroup>),
        });
    }

    incrementWurst() {
        const inputWurst = this.state.inputWurst;
        this.setState({
            Wurst: this.state.Wurst + 1,
            inputWurst: inputWurst.concat(<InputGroup>
                <Input placeholder="Wurst" key={this.state.Wurst} /><ProviderInfos /></InputGroup>),
        });
    }

    decrementWurst() {
        const inputWurst = this.state.inputWurst;
        this.setState({
            Wurst: this.state.Wurst - 1,
            inputWurst: inputWurst.concat(<InputGroup>
                <Input placeholder="Wurst" key={this.state.Wurst} /><ProviderInfos /></InputGroup>),
        });
    }

    incrementSausage() {
        const inputSausage = this.state.inputSausage;
        this.setState({
            Sausage: this.state.Sausage + 1,
            inputSausage: inputSausage.concat(<InputGroup>
                <Input placeholder="Sausage" key={this.state.Sausage} /><ProviderInfos /></InputGroup>),
        });
    }

    decrementSausage() {
        const inputSausage = this.state.inputSausage;
        this.setState({
            Sausage: this.state.Sausage - 1,
            inputSausage: inputSausage.concat(<InputGroup>
                <Input placeholder="Sausage" key={this.state.Sausage} /><ProviderInfos /></InputGroup>),
        });
    }

    incrementPretzel() {
        const inputPretzel = this.state.inputPretzel;
        this.setState({
            Pretzel: this.state.Pretzel + 1,
            inputPretzel: inputPretzel.concat(<InputGroup>
                <Input placeholder="Pretzel" key={this.state.Pretzel} /><ProviderInfos /></InputGroup>),
        });
    }

    decrementPretzel() {
        const inputPretzel = this.state.inputPretzel;
        this.setState({
            inputPretzel: inputPretzel.deleteElement(this.state.Pretzel),
            Pretzel: this.state.Pretzel - 1,
        });
    }

    render() {
        return (
            <Card>
                <CardBlock className="main-table">
                    <fieldset>
                        <legend>Currys</legend>
                        {this.state.inputCurry}
                        <button onClick={this.incrementCurry}>Ajouter un Curry</button>
                        <button onClick={this.decrementCurry}>Enlever un Curry</button>
                    </fieldset>
                    <fieldset>
                        <legend>Wursts</legend>
                        {this.state.inputWurst}
                        <button onClick={this.incrementWurst}>Ajouter un Wurst</button>
                        <button onClick={this.decrementWurst}>Enlever un Wurst</button>
                    </fieldset>
                    <fieldset>
                        <legend>MasterSausages</legend>
                        {this.state.inputSausage}
                        <button onClick={this.incrementSausage}>Ajouter un Sausage</button>
                        <button onClick={this.decrementSausage}>Enlever un Sausage</button>
                    </fieldset>
                    <fieldset>
                        <legend>Pretzels</legend>
                        {this.state.inputPretzel}
                        <button onClick={this.incrementPretzel}>Ajouter un Pretzel</button>
                        <button onClick={this.decrementPretzel}>Enlever un Pretzel</button>
                    </fieldset>
                    <Button color="secondary">Options</Button>{' '}
                    <Button id="btn">Exécuter</Button>
                </CardBlock>
            </Card>
        );
    }
}

As you can see I tried something new on the fourth element (pretzels) but "deleteElement" is not an accepted function for this object type and I got :

Uncaught TypeError: inputMultiProvider.deleteElement is not a function

In the console.

tatsu
  • 2,316
  • 7
  • 43
  • 87
  • https://stackoverflow.com/questions/29527385/removing-element-from-array-in-component-state Something like this maybe? – Lafexlos Aug 07 '17 at 14:40
  • `inputPretzel: inputPretzel.deleteElement(this.state.Pretzel),` I am guessing you're trying to remove the element from the `inputPretzel` array. – abdul Aug 07 '17 at 14:48
  • yes that is it. but also with `Curry: this.state.Curry - 1, inputCurry: inputCurry.concat( ),` and that was what you suggested would remove the element but it doesn't it just adds one. – tatsu Aug 07 '17 at 14:51
  • @Lafexlos "parsing error unexpected tokens" in my Atom editor on "this" then once i remove both this, on the "1" : `this.setState({ update(inputPretzel, { $splice: [[Pretzel, 1]]}), Pretzel: this.state.Pretzel - 1, });` – tatsu Aug 07 '17 at 14:52
  • @Lafexlos this syntaxe : `this.setState({ inputPretzel : this.state.inputPretzel.splice(this.state.Pretzel, 1), });` sorta works except it deltes them all. if I do `Pretzel + 1` or similar I get the same result: it deletes them all. If I do `splice(1, 1)` it removes them all but one (as expected) I'm starting to think I don't have the right values being stored in my variables. – tatsu Aug 07 '17 at 15:43
  • weird : `console.log(this.state.MultiProvider);` outputs: 2, 3 if I click on add twice but when I click on remove it outputs 4 (and then no longer increments no matter how much i click on remove again) even though I don't have an increment or decrement to Pretzel within decrementPretzel – tatsu Aug 07 '17 at 15:53
  • I think it's because of : `Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of "SearchExtendedComponent"` – tatsu Aug 07 '17 at 15:55
  • I followed their link : https://facebook.github.io/react/docs/lists-and-keys.html#keys (up under the "incorrect" and "corect" examples for key usage) but how in the hell am I supposed to make that work when I am supposed to have the key be on the input and the input not be the only DOM tag I add every time? – tatsu Aug 07 '17 at 16:04

1 Answers1

1

The issue is with keys

the entire class needs to be reworked top down for any of it to work.

firstly : UUIDs are the only way to go here or you'll loose the key/ the key will be the same across entries.

so first add https://www.npmjs.com/package/uuid to your project

you'll need to add an import wich for some reason noone knows the correct syntax to so here it is it might be usefull to alot of people :

import UUID from 'node-uuid';

then you increments should look like this :

incrementPretzel() {
    const uuid = require('uuid/v1');
    uuid();
    const inputPretzel = this.state.inputPretzel;
    this.setState({
        Cube: uuid,
        inputPretzel: inputPretzel.concat(<InputGroup>
            <Input placeholder="Pretzel" key={uuid} /><ProviderInfos /></InputGroup>),
    });
}

and your decrements like this :

decrementPretzel() {
    this.setState({
        inputPretzel: this.state.inputPretzel.splice(this.state.inputPretzel, this.state.inputPretzel.length - 1),
    });
}

but this doesn't solve the problems of :

  • unclean code
  • non-refactored code
  • html within setState

be warned.

tatsu
  • 2,316
  • 7
  • 43
  • 87