【发布时间】:2017-06-27 17:45:12
【问题描述】:
我有一个排序过滤器,它采用一个数组来填充选项。试图查看选项value 等于数组中的文本,但我在标题中得到错误:
Invalid attempt to destructure non-iterable instance
我需要将文本作为选项标签中的值传递,以便当用户更新过滤器时,正确的文本会显示给用户所做的选择。
这是我的代码:
function Sorting({by, order, rp}: SortingProps) {
const opts = [
['Price (low)', 'price', 'asc'],
['Price (high)', 'price', 'desc'],
['Discount (low)', 'discount', 'asc'],
['Discount (high)', 'discount', 'desc'],
['Most popular', 'arrival', 'latest'],
['Most recent', 'arrival', 'latest'],
];
const onChange = (i) => {
const [text, by, order] = opts[i];
refresh({so: {[by]: order}});
/* GA TRACKING */
ga('send', 'event', 'My Shop Sort By', text, 'Used');
};
return (
<div className={cn(shop.sorting, rp.sorting.fill && shop.sortingFill)}>
<Select className={shop.sortingSelect} label="Sort By" onChange={onChange} value={`${by}:${order}`}>
{opts.map(([text], i) =>
<Option key={i} value={text}>{text}</Option>
)}
</Select>
</div>
)
}
【问题讨论】:
-
错误发生在哪一行?传递给
onChange处理程序的i的实际值是多少?我猜它是一个 Event 对象,而不是一个整数。 -
我认为问题出在您正在接受的参数
i中。它不会是一个数字。您不能将其用作数组索引。
标签: reactjs ecmascript-6 destructuring