javascript - 使用 $splice 从状态中删除嵌套元素 - React

标签 javascript reactjs state splice

我正在尝试从 Component 状态中删除一个元素。这就是状态中感兴趣的部分的样子:

this.state({user:{
    ...
    instruments: [
        0: {
            name:"bass guitar",
            experience: "2"
        }
        1: {
            name:"drums",
            experience: "1"
        }
        ...
    ]
}})

当用户单击按钮时,我想从状态中删除该元素。使用以下代码,数组中的第一个元素始终被删除,因为 splice 无法访问内部 name 属性。有什么想法吗?

Instrument.js

remove(){
    this.props.removeInstrument(this.props.name);
}

render(){
    return(
        <article className="instrument">
                <Col xs={12} sm={6}>
                    <div className="container-elements">
                        <span className="delete-element" onClick={this.remove.bind(this)}>x</span>
                        <h3>{this.props.name}</h3>
                        <p>{this.props.experience} years of experience</p>
                    </div>
                </Col>
        </article>
    );
}

EditProfile.js

    removeInstrument(val) {
    console.log('clicked on remove! ', val);
    this.setState({
        user: update(this.state.user, {instruments: {$splice: [[val,1]]}})
    })
}

// this is how I render the instrument
<div className="container-tags">
   {this.state.user.instruments.map((instrument, index) => {
       return <Instrument key={instrument.name} removeInstrument={this.removeInstrument} name={instrument.name} experience={instrument.experience}/>;
    })}
</div>

最佳答案

您可以使用数据的索引来删除它,而不是使用名称

Instrument.js

remove(){
    this.props.removeInstrument(this.props.index);
}

render(){
    return(
        <article className="instrument">
                <Col xs={12} sm={6}>
                    <div className="container-elements">
                        <span className="delete-element" onClick={this.remove.bind(this)}>x</span>
                        <h3>{this.props.name}</h3>
                        <p>{this.props.experience} years of experience</p>
                    </div>
                </Col>
        </article>
    );
}

EditProfile.js

removeInstrument(val) {
    console.log('clicked on remove! ', val);
    this.setState({
        user: update(this.state.user, {instruments: {$splice: [[val,1]]}})
    })
}

// this is how I render the instrument
<div className="container-tags">
   {this.state.user.instruments.map((instrument, index) => {
       return <Instrument key={instrument.name} removeInstrument={this.removeInstrument} name={instrument.name} experience={instrument.experience} index={index}/>;
    })}
</div>

关于javascript - 使用 $splice 从状态中删除嵌套元素 - React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41097893/

相关文章:

javascript - 当 Tab 更改时,React Native TextInput 值仍然存在

oauth - 授权代码流 - 来自多个选项卡的并发请求

javascript - 如何在另一个主创建 react 应用程序中添加创建 react 子应用程序

javascript - 如何在 React 应用程序中访问 this.refs 不可用的函数中的 DOM 元素?

javascript - React 功能组件默认 props 与默认参数

qt - 在过渡动画完成后更改状态

javascript - IIFE 和函数提升——不是函数错误

javascript - 使用jquery限制左右滚动以防止过度滚动

javascript - 使用 localtimezone 解析 JavaScript 中的日期对象的日期

javascript - 即使使用 AngularJS 和 PHP 的凭据不正确,登录仍然会定向