Skip to content

feat(mobx-react-lite): add support passing propsAreEqual fn for obser… - #4680

Open
js2me wants to merge 1 commit into
mobxjs:mainfrom
js2me:feature/customize-memo-comparison
Open

feat(mobx-react-lite): add support passing propsAreEqual fn for obser…#4680
js2me wants to merge 1 commit into
mobxjs:mainfrom
js2me:feature/customize-memo-comparison

Conversation

@js2me

@js2me js2me commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

…ver HOC

Code change checklist

  • Added/updated unit tests
  • Updated /docs. For new functionality, at least API.md should be updated
  • Verified that there is no significant performance drop (npm -w mobx run test:performance)

@changeset-bot

changeset-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 078fa55

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mobx-react-lite Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI left a comment

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.

Pull request overview

This PR adds an optional propsAreEqual comparator to mobx-react-lite’s observer() so consumers can customize the React.memo props equality behavior applied by the HOC.

Changes:

  • Extend IObserverOptions to include an optional propsAreEqual(prevProps, nextProps) callback and thread it through to React.memo.
  • Update TypeScript overloads to propagate props typing into IObserverOptions.
  • Add a changeset entry documenting the patch release for mobx-react-lite.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/mobx-react-lite/src/observer.ts Adds propsAreEqual option and passes it into memo, with updated TS typings/overloads.
.changeset/quiet-stars-sing.md Declares a patch changeset for the new propsAreEqual support.
Comments suppressed due to low confidence (1)

packages/mobx-react-lite/src/observer.ts:88

  • With the new propsAreEqual option, callers will pass an options object without forwardRef, but the current dev warning triggers for any truthy options and specifically warns about { forwardRef: true }. This becomes a misleading warning for the new feature. Restrict the warning to the deprecated forwardRef option (or update the warning message to cover all options).
    options?: IObserverOptions<P>
) {
    if (process.env.NODE_ENV !== "production" && warnObserverOptionsDeprecated && options) {
        warnObserverOptionsDeprecated = false
        console.warn(

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 31 to +32
readonly forwardRef?: boolean
propsAreEqual?: (prevProps: Readonly<P>, nextProps: Readonly<P>) => boolean
Comment on lines 60 to 63
export function observer<
C extends React.FunctionComponent<any> | React.ForwardRefRenderFunction<any>,
Options extends IObserverOptions
Options extends IObserverOptions<React.ComponentProps<C>>
>(
// in props; we assume that if deep objects are changed,
// this is in observables, which would have been tracked anyway
observerComponent = memo(observerComponent)
observerComponent = memo(observerComponent, options?.propsAreEqual)
@kubk

kubk commented Jul 2, 2026

Copy link
Copy Markdown
Collaborator

@js2me Thank you for the contribution. Could you share an example of when this option would be useful? I'm worried it might not be very idiomatic from the MobX perspective

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants