简体   繁体   English

如何在 react&antD 中获得正确的索引

[英]how can I get the right index in react&antD

AppUI.js AppUI.js

class AppUI extends Component {
render() {
    return (
        <div>
            <List
                itemLayout="horizontal"
                dataSource={this.props.list}
                renderItem={(item, index) => (
                    <List.Item onClick={(index) => {
                        this.props.handleDelete(index)
                    }}>
                        {item.title}
                    </List.Item>
                )}
            />
        </div>
    )
}

} }

App.js应用程序.js

class App extends Component {
constructor(props) {
    super(props);
    this.state = store.getState();
    this.handleStore = this.handleStore.bind(this);
    this.handleDelete = this.handleDelete.bind(this);
    store.subscribe(this.handleStore);
}


render() {
    return (
        <AppUI
            handleDelete={this.handleDelete}
            list={this.state.list}
        />
    )
}



handleDelete(index) {
    const action = getDeleteListAction(index);
    store.dispatch(action);
}

handleStore() {
    this.setState(store.getState())

}

} }

my question is that how can I get the right index when I click each list我的问题是当我单击每个列表时如何获得正确的索引
my question is that how can I get the right index when I click each list我的问题是当我单击每个列表时如何获得正确的索引
my question is that how can I get the right index when I click each list我的问题是当我单击每个列表时如何获得正确的索引

  renderItem={(item, index) => (
                        <List.Item onClick={(index) => {
                            this.props.handleDelete(index)
                        }}>
                            {item.title}
                        </List.Item>
                    )}

Your onClick handler needs a bit of tweaking, that param is actually the event, which is what you ended up passing into handleDelete.您的 onClick 处理程序需要进行一些调整,该参数实际上是事件,这就是您最终传递给 handleDelete 的内容。 You want something like你想要类似的东西

renderItem={(item, index) => (
  <List.Item onClick={() => {
    this.props.handleDelete(index)
  }}>
    {item.title}
  </List.Item>
)}

As a side note, its probably better in the long run to pass the item itself and find by a ID or some sort of identifier附带说明一下,从长远来看,传递项目本身并通过 ID 或某种标识符查找可能会更好

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

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