javascript - How do I unload components?
问题描述
I have project component that I'm duplicating according to a project list. Once the page is rendered and all Project components are displayed I want to enable a search functionality.
When I perform the search and I filter the project list, the old results are displayed near the new results after search!
How to cause it to display only search results?
Updated code (Working):
class ProjectsList extends Component {
state = {
projectsDetails: [......],
filterKey: null
}
componentWillMount() {
//service.getProjectDetails();
}
handleSearchChange(e) {
this.setState({ filterKey: e.target.value });
}
render() {
const { filterKey, projectsDetails } = this.state;
const filteredProjects = filterKey
? projectsDetails.filter(item =>
item.Name.toLowerCase().includes(filterKey.toLowerCase()) && item.FabricationConfigs !== null
)
: projectsDetails.filter(item =>
item.FabricationConfigs !== null
);
return (
<table width="100%">
<tbody>
<tr>
<td>
<div className="Toolbar2">
<table width="100%">
<tbody>
<tr>
<td>
<h1>Projects</h1>
</td>
<td width="20%" align="right">
<Search labelText="" id="search-1" onChange={ this.handleSearchChange.bind(this) }/>
</td>
</tr>
</tbody>
</table>
<hr></hr>
</div>
<div className="inline">
{filteredProjects.map(item =>
<Project
key={item.Name}
name={item.Name}
description={item.Description}
configurations={item.FabricationConfigs}/>)}
</div>
</td>
</tr>
</tbody>
</table>
);
}
}
解决方案
假设您正在渲染一个列表:
state = {
list: [{code: 'a'},{code: 'b'},{code: 'c'}],
};
//some render function
<div>{this.state.list.map(item => <Item item={item} />}</div>
为了不卸载这些项目之一,您必须取消渲染它们。
因此您可以按索引删除一个(例如)。您可以使用您的功能进行过滤。
removeByIndex(idx) {
this.setState({
list: this.state.list.filter((item, index) => index !== idx),
});
}
因此,具有所述索引的Item组件将被取消渲染并因此被卸载。
推荐阅读
- flutter - Flutter Doctor - 无法确定是否安装了 IntelliJ
- javascript - 结果闪烁的Inner.HTML显示
- javascript - pdfJs 从查看器获取访问权限
- powershell - 使用 PowerShell ISE 我想使用 For Loop 在文件夹中创建文件夹,并在每个这样的文件夹中,再次使用 For Each Loop 创建示例 txt 文件
- reactjs - 渲染具有对 dom 元素的 ref 的 React 组件的多个实例
- java - Java中的多个重叠正方形
- node.js - 如何并发承诺限制到特定数量?
- c# - iTextPdf 替换 PDF 对象错误标志
- semantic-release - 是否可以只发布带有语义发布的预发布?
- regex - 为什么将组添加到我的正则表达式会改变它捕获的内容