0

I am trying to add repetitive components using ReactJs and at the same time I am deleting components and rendering the results. While doing the result seems quite confusing and I might need expert Help, So please Help me understanding and solve the issue.

Let me share my Code:

render() {
const contactComponentList = this.props.transportOperator.contacts.map((object, index) => {
     return (
            <div key={index} ref={"contact_"+index}>
                <MuiThemeProvider muiTheme={muiThemebtn}>
                    <Paper key={index} rounded={false} style={material.addWidgetStyle.paperStyle} zDepth={0}>
                        <div style={material.addWidgetStyle.rowStyle}>
                            <span style={material.addWidgetStyle.titleColumnStyle}>
                                {language.AddTransportText.TransportContactName}*
                            </span>
                            <span style={material.addWidgetStyle.greySpanStyle}>

                                <TextField
                                    id={"name" + index}
                                    ref="name"
                                    hintText={language.AddTransportText.TransportContactName}
                                    type="text"
                                    min="0"
                                    onChange={this.handleTransportOpContactName.bind(this, index)}
                                    underlineStyle={errorTxtName !== '' || errorValName !== '' ? { borderColor: 'red' } : {}}
                                    />
                                <IconButton style={errorTxtName !== '' || errorValName !== '' ? styles.showWarining : styles.hideWarning} 
                                    tooltip={errorTxtName !== '' ? errorTxtName : errorValName}>
                                    <Warning color="red" />
                                </IconButton>
                            </span>
                        </div>
                        <div style={material.addWidgetStyle.rowStyle}>
                            <span style={material.addWidgetStyle.titleColumnStyle}>

                            </span>
                            <span>
                            {index >= 1 ?
                            <IconButton style={styles.leftBtn} iconStyle={styles.leftIcon}
                                tooltip={language.AddDriverText.DeleteTransportOpContact}
                                onClick={this.deleteContactComponent.bind(this,index)}>
                                <ActionDelete color={grey700} />
                            </IconButton>
                            : ''
                            }
                            {(this.props.transportOperator.contacts.length-1 === index) ? 
                            <IconButton 
                                style={styles.leftBtn} iconStyle={styles.leftIcon}
                                tooltip={language.AddDriverText.AddTransportOpContact}
                                onClick={this.addContactComponent}>
                                <ContentAdd color={grey700} />
                            </IconButton>
                            : ''
                            }
                            </span>
                        </div>
                    </Paper>
                </MuiThemeProvider>
            </div>
        )
    });

    return (
        <MuiThemeProvider>
            <div>
                {contactComponentList}
            </div>
        </MuiThemeProvider>
    );
}

The code that adds and deletes the repetitive components is as follows:

addContactComponent(event) {
    tempArray = this.props.transportOperator.contacts;
    tempArray.push({
        'name': '',
    });
    this.props.error.push({
        'errorName': '',
    });
    contacts=tempArray;
    this.setState({ contactComponents: tempArray });
    this.props.transportOperator.contacts=contacts;
    event.currentTarget.style.display = 'none';
}

deleteContactComponent(index,event){
    tempArray.splice(index,1);
    contacts=tempArray;
    this.setState({
        contactComponents: tempArray
    });
    this.props.transportOperator.contacts=contacts;
}

But a strange behavior I am facing is suppose we have 4 contact component added i.e: A,B,C,D and as we are deleting by index from the array, so we delete B. in console.log where I am seeing the remaining values of the array, it shows A,C,D are there, but in UI, I see that the last element is deleted i.e A,B,C is remaining.

I tried everything but could not understand this strange behavior. Please I need a expert help.

NDeveloper
  • 3,115
  • 7
  • 23
  • 34
  • Try to use something unique per each contact as key instead of index in your root contact div. If you have contact name or such - it will work just fine: `
    `
    – Amid Jun 21 '17 at 10:08
  • I think you are referring to the whole parent element with an existing key which is not unique to the component that's why when a state changes it also effects the other parts of it. – Ozan Jun 21 '17 at 10:09
  • Hi @Ozan and @Amid : I am sorry I edited my code. Please have a look. I am actually iterating the `contactComponentList ` which contains the entire `
    ` part. So when I am pushing a component by `addContactComponent` method, I am adding into `contactComponentList ` so each time the index is updated. Even if I do `
    ` then also the problem persists. :(
    – NDeveloper Jun 21 '17 at 10:51
  • Can you make the following simple change. Add to each of your contact object a field called `uid` and populate it with some unique string (take guid for example: https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript). then change your code that creates `contactComponentList` to `
    `
    – Amid Jun 21 '17 at 11:18
  • @Amid: I tried doing this, but in this case entered values are being reset. Suppose I have added three components, div key generated as you suggested, but when I am deleting one suppose from A,B,C deleting B, the UI shows blank where I should retain A,C in the entered components. – NDeveloper Jun 21 '17 at 11:50
  • Here is a small sample: https://codepen.io/anon/pen/YQVgYZ check it and verify that your code is similar. If necessary reduce your code until you will get down to small working example as I provided. – Amid Jun 21 '17 at 12:08

0 Answers0