Reactjs 反应:强制完成组件更新
我已经编写了一个组件(它是一个列表)。更新组件时(此列表已更改),onclick事件不会更新,并传递与呈现组件之前相同的值。有没有办法强制React从头开始更新整个div?变量“value.edit\u title\u normal”不会更改,只是onclick事件不会更改 *为了简洁起见,我删除了一些多余的代码*Reactjs 反应:强制完成组件更新,reactjs,Reactjs,我已经编写了一个组件(它是一个列表)。更新组件时(此列表已更改),onclick事件不会更新,并传递与呈现组件之前相同的值。有没有办法强制React从头开始更新整个div?变量“value.edit\u title\u normal”不会更改,只是onclick事件不会更改 *为了简洁起见,我删除了一些多余的代码* class SearchResults extends React.PureComponent { //Code to show the detailed informati
class SearchResults extends React.PureComponent {
//Code to show the detailed information
render() {
//Individual record
const startItem = (this.props.itemsPerPage * this.props.page) - this.props.itemsPerPage;
const endItem = startItem + this.props.itemsPerPage;
//Slice is from/to
let all = searchableDatabase.slice(startItem, endItem).map((value, index) => {
return (
<div onClick={(e) => this.showDetailedInfo(index, e)} > // <== This bit here is not updating when re-rendered
{value.edit_title_normal}
</div>
)
});
//Main container
return (
<div>
<div>
<Pagination allLength={searchableDatabase.length} page={this.props.page} itemsPerPage={this.props.itemsPerPage} />
</div>
<div>
{all}
</div>
</div>
);
}
}
类搜索结果扩展了React.PureComponent{
//显示详细信息的代码
render(){
//个人记录
const startItem=(this.props.itemsPerPage*this.props.page)-this.props.itemsPerPage;
const endItem=startItem+this.props.itemsPerPage;
//切片从/到
let all=searchableDatabase.slice(startItem,endItem).map((值,索引)=>{
返回(
this.showDetailedInfo(index,e)}>/您误诊了该问题。map回调中的索引始终从零开始,因此它不可能更改为其他索引。您正在对从原始数组切片的新数组进行操作,而map方法不知道或不关心原始数组的索引
我假设您希望索引从startItem
开始,而不是从0开始。然后您必须手动将其添加到索引中:
this.showDetailedInfo(index+startItem,e)}>