Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Memoized the `ModalState` context value so consumers no longer re-render on every parent render (`javascript:S6481`).

## [3.8.5] - 2026-08-31

## [3.8.4] - 2026-08-31
Expand Down
83 changes: 67 additions & 16 deletions react/ModalState.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,71 @@ import {
} from './utils/StateUtils'
import { findSla } from './utils/SlasUtils'
import { newAddress } from './utils/newAddress'
import memoizeOne from './utils/memoizeOne'

class ModalState extends Component {
// The context value is rebuilt only when one of the memoized arguments
// below changes; the handler methods are class properties, so their
// identity is already stable for the lifetime of the instance.
// One positional argument per context field is deliberate: memoizeOne
// compares arguments by identity, so wrapping them in an object would
// defeat the memoization. Hence the S107 suppression below.
getContextValue = memoizeOne(
(
activeState,
activeSidebarState,
bestPickupOptions,
externalPickupPoints,
geolocationStatus,
hoverPickupPoint,
isSearching,
isSelectedBestPickupPoint,
lastState,
lastSidebarState,
lastMapCenterLatLng,
logisticsInfo,
pickupOptions,
pickupPoints,
residentialAddress,
searchedAreaNoPickups,
selectedPickupPoint,
shouldSearchArea,
showOtherPickupPoints
) => /* NOSONAR javascript:S107 — see the comment above */ ({
activeState,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

acho que vale tentar ignorar essa regra do sonar nesse bloco de código, oq acha @huandrey ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concordo, @jeffersontuc. Segui com a supressão em bf8c7a7: /* NOSONAR javascript:S107 */ na linha do =>, que é onde o Sonar ancora o S107, mais um comentário acima do getContextValue explicando o motivo. O memoizeOne compara os argumentos por identidade, então agrupar tudo num objeto quebraria a memoização.

Considerei a alternativa de memoizar em cima de this.state (um parâmetro só, sem supressão), mas ela reconstruiria o value em qualquer setState, inclusive de campos que nem estão no contexto (askForGeolocation, localSearching). Preferi manter a memoização precisa.

Detalhe: usei comentário de bloco porque o Prettier move um // NOSONAR de fim de linha para dentro do objeto, e aí a supressão para de valer.

activeSidebarState,
bestPickupOptions,
externalPickupPoints,
geolocationStatus,
isSearching,
isSelectedBestPickupPoint,
hoverPickupPoint,
lastState,
lastSidebarState,
lastMapCenterLatLng,
logisticsInfo,
pickupOptions,
pickupPoints,
residentialAddress,
searchedAreaNoPickups,
searchPickupsInArea: this.searchPickupsInArea,
selectNextPickupPoint: this.selectNextPickupPoint,
selectPreviousPickupPoint: this.selectPreviousPickupPoint,
selectedPickupPoint,
setActiveState: this.setActiveState,
setAskForGeolocation: this.setAskForGeolocation,
setActiveSidebarState: this.setActiveSidebarState,
setGeolocationStatus: this.setGeolocationStatus,
setHoverPickupPoint: this.setHoverPickupPoint,
setMapCenterLatLng: this.setMapCenterLatLng,
setSelectedPickupPoint: this.setSelectedPickupPoint,
shouldSearchArea,
setShouldSearchArea: this.setShouldSearchArea,
setShowOtherPickupPoints: this.setShowOtherPickupPoints,
showOtherPickupPoints,
})
)

constructor(props) {
super(props)

Expand Down Expand Up @@ -518,15 +581,15 @@ class ModalState extends Component {

return (
<ModalStateContext.Provider
value={{
value={this.getContextValue(
activeState,
activeSidebarState,
bestPickupOptions,
externalPickupPoints,
geolocationStatus,
hoverPickupPoint,
isSearching,
isSelectedBestPickupPoint,
hoverPickupPoint,
lastState,
lastSidebarState,
lastMapCenterLatLng,
Expand All @@ -535,22 +598,10 @@ class ModalState extends Component {
pickupPoints,
residentialAddress,
searchedAreaNoPickups,
searchPickupsInArea: this.searchPickupsInArea,
selectNextPickupPoint: this.selectNextPickupPoint,
selectPreviousPickupPoint: this.selectPreviousPickupPoint,
selectedPickupPoint,
setActiveState: this.setActiveState,
setAskForGeolocation: this.setAskForGeolocation,
setActiveSidebarState: this.setActiveSidebarState,
setGeolocationStatus: this.setGeolocationStatus,
setHoverPickupPoint: this.setHoverPickupPoint,
setMapCenterLatLng: this.setMapCenterLatLng,
setSelectedPickupPoint: this.setSelectedPickupPoint,
shouldSearchArea,
setShouldSearchArea: this.setShouldSearchArea,
setShowOtherPickupPoints: this.setShowOtherPickupPoints,
showOtherPickupPoints,
}}
showOtherPickupPoints
)}
>
{children}
</ModalStateContext.Provider>
Expand Down
Loading