简体   繁体   English

如何从 React.js 中的数组中删除 JSX 元素?

[英]How to remove JSX element from array in React.js?

I have an array of sections in state which holds some list of JSX elements.我在 state 中有一组部分,其中包含一些 JSX 元素列表。 I'm trying to delete a specific element which is JSX from array when clicked on button but I can't figure out how to do this.我试图在单击按钮时从数组中删除一个特定的 JSX 元素,但我不知道如何做到这一点。 Any help would be appreciated.任何帮助,将不胜感激。

Section.js Section.js

class Section extends React.Component {
    state = {
        sections: []
    };

    addLectureHandler = () => {
        const secs = this.state.sections;
        secs.push(
            <LectureItem key={Math.random} removeLectureHandler={() => this.removeLectureHandler(<LectureItem />)} />
        );
        this.setState({ sections: secs });
    };

    removeLectureHandler = (item) => {
        console.log(item);
    };

    render() {
        const { classes } = this.props;
        return (
            <div className={classes.container}>
                <h4 className={classes.sectionTitle}>
                    Section 1 - <AssignmentIcon /> Introduction
                </h4>
                <div className={classes.addButtonContainer}>
                    <Button variant="outlined" className={classes.addButton} onClick={this.addLectureHandler}>
                        Create Lecture
                    </Button>
                </div>
                <div className={classes.lectureContainer}>{this.state.sections}</div>
            </div>
        );
    }
}

export default withStyles(styles)(Section);

LectureItem.js LectureItem.js

class LectureItem extends React.Component {
    render() {
        const { classes, removeLectureHandler } = this.props;
        return (
            <div className={classes.container}>
                <div className={classes.titleContainer}>
                    <TextField className={classes.textField} label="New Lecture Title" name="lecture" margin="normal" />
                    <Button className={classes.removeButton} onClick={removeLectureHandler}>
                        <ClearIcon />
                    </Button>
                </div>
                <Button variant="contained" color="primary" className={classes.button}>
                    Add
                </Button>
            </div>
        );
    }
}

export default withStyles(styles)(LectureItem);

Its quite straight forward, its more of a logic statement rather than react, Let me show it to you它非常直截了当,它更像是一个逻辑陈述而不是反应,让我给你看

For you method addLectureHandler change push command to the following对于您的方法addLectureHandler将推送命令更改为以下

 secs.push(
        <LectureItem key={Math.random} removeLectureHandler={() => this.removeLectureHandler(secs.length)} />
    );

You were passing <LectureItem /> which is new element not the one you are pushing, so We will pass length (the actual new index of this element).您正在传递<LectureItem />这是新元素而不是您要推送的元素,因此我们将传递长度(该元素的实际新索引)。

Next change you removeLectureHandler to this接下来将您的removeLectureHandler更改为此

removeLectureHandler = (indexToDelete) => {
    console.log(indexToDelete);
    const secs = this.state.sections;
    secs.splice(indexToDelete, 1);
    this.setState({ sections: secs });

};

And you have successfully deleted an element from the Sections and UI:-0并且您已成功从 Sections 和 UI 中删除了一个元素:-0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM