【发布时间】:2019-03-08 20:15:04
【问题描述】:
我正在学习做出反应并尝试根据所选元素更新反应状态。我正在使用反应选择。它现在显示所有数据。但是当我从选择框中选择选项时,我想更新数据。如果我选择多个选项,那么它应该更新过滤所选选项的状态。
const reviews = [
{
"id": 1,
"rating": 5,
"label": [
"Sports",
"Sleep"
],
},
{
"id": 3,
"rating": 5,
"label": [
"Sports",
"Pain"
],
},
];
class SelectCreateFilter extends React.Component {
state = {
multi: null,
data: reviews,
health: healthBenefitsOptions,
category: productCategoryOptions,
products: productOptions,
};
handleChange = name => value => {
this.setState({
[name]: value,
data: this.state.data.filter() //how to perform the data mathcing to reviews.label
});
};
render() {
return (
<div>
<Select
options={groupedOptions}
components={components}
value={multi}
onChange={this.handleChange('multi')}
placeholder="Select multiple tags or products for reviews"
isMulti
isSearchable
/>
</div>
);
}
}
提前感谢您的帮助。 这是来自codesandbox的代码,以便您可以检查上面的代码 https://codesandbox.io/embed/jlro0vo4n3?fontsize=14
【问题讨论】:
-
能否请您附上相关代码并具体说明问题所在?沙盒中的一个文件有 300 行长。
-
谢谢尼克..在 app.js 中我设置了这样的状态 = { multi: null, data: reviews, health: healthBenefitsOptions, category: productCategoryOptions, products: productOptions }; handleChange = name => value => { this.setState({ [name]: value, data: }); };我想用 data.label 更新与所选选项匹配的 handlechange 事件的状态数据
-
您能否在codesandbox 上设置一个可重现的问题示例?
标签: reactjs filter react-select react-state-management