I have table of customers and the selected customer is stored in the ViewState
. The problem is that all rows re-render when the selection changes which is quite slow. Ideally only the selected row and the row previously selected would re-render, but I did not find out how to achieve this. My structure is identical to the example in the MobX contact list example:
{this.filteredCustomers.map(customer => {
return (
<CustomerRow
key={customer.id}
customer={customer}
viewState={this.props.store.view}
/>
)
})}
and
const CustomerRow = observer((props: CustomerRowProps) => {
const isSelected = props.viewState.isCustomerSelected(props.customer)
const rowClass = isSelected ? 'active' : ''
return (
<tr className={rowClass}>
<td>{props.customer.lastName}</td>
<td>{props.customer.firstName}</td>
</tr>
)
})
All rows depend on the value of ViewState.selectedCustomer
through the isCustomerSelected
method.
Is there another way to structure this that avoids re-rendering all rows?