【发布时间】:2025-12-24 08:55:16
【问题描述】:
我使用 React bootstrap Typeahead 库 (https://github.com/ericgio/react-bootstrap-typeahead)。我的问题是,基于索引this.state.todo,我需要在this.state.todos 内找到具有给定索引的对象。在多选中将找到的对象的名称显示为默认值。
在这里演示:https://stackblitz.com/edit/react-rsfpup
class App extends Component {
constructor() {
super();
this.state = {
todos: [{id:1, name: 'Paul'}, {id:2, name: 'Martin'}, {id:3, name: 'Jacob'}],
todo:[1, 3],
selectedItems: []
};
}
handleSelect = items => {
this.setState({ selectedItems: items });
};
render() {
return (
<div>
<Todos
todos={this.state.todos}
todo={this.state.todo}
handleSelect = {this.handleSelect}
/>
</div>
);
}
}
class Todos extends Component {
constructor(props) {
super(props);
}
render() {
var tempArr = this.props.todos.filter(item => !this.props.todo.includes(item));
return (
<div>
<Typeahead
id={'example4'}
labelKey="name"
multiple
selected={tempArr}
options={this.props.todos}
onChange={this.props.handleSelect}
/>
</div>
);
}
}
【问题讨论】:
-
您好 Umbro,请检查我的解决方案,如果有帮助请告诉我。
标签: javascript reactjs react-bootstrap-typeahead