【发布时间】:2019-12-24 07:43:11
【问题描述】:
经验:我是 React 的初学者。
我要学习的内容: Hooks (useState) - 但我不知道如何更新状态并以此重新渲染视图。据我了解,如果更新后的状态与上一个有点相似,React 不会重新渲染视图......在谷歌搜索之后,我尝试复制状态并以某种方式更新它,但我遗漏了一些东西,我不知道什么。
我想在项目中做什么:当用户从下拉列表中选择一个地区时,我有一个我想要过滤的国家/地区列表。这是在选择发生时触发的函数,以及我希望解释我想要做什么的 cmets:
const change = event => {
//copy the `data` state (which has a list of all the countries)
let newData = [...data];
console.log(newData);
//filter through the countries list to get only those with the selected region
let filtered = newData.filter(obj => obj.region === event.target.value);
console.log(filtered);
//change the countries list with the filtered one, and rerender the view
setData([data, newData]);
console.log(data);
};
您可以找到相关文件和代码HERE(向下滚动以访问change 函数)
从“按地区过滤器”下拉菜单中选择一个地区
在控制台中查看错误/输出
【问题讨论】:
标签: reactjs react-hooks