feat(mobx-react-lite): add support passing propsAreEqual fn for obser… - #4680
feat(mobx-react-lite): add support passing propsAreEqual fn for obser…#4680js2me wants to merge 1 commit into
Conversation
🦋 Changeset detectedLatest commit: 078fa55 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
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
IObserverOptionsto include an optionalpropsAreEqual(prevProps, nextProps)callback and thread it through toReact.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
propsAreEqualoption, callers will pass anoptionsobject withoutforwardRef, but the current dev warning triggers for any truthyoptionsand specifically warns about{ forwardRef: true }. This becomes a misleading warning for the new feature. Restrict the warning to the deprecatedforwardRefoption (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.
| readonly forwardRef?: boolean | ||
| propsAreEqual?: (prevProps: Readonly<P>, nextProps: Readonly<P>) => boolean |
| 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) |
|
@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 |
…ver HOC
Code change checklist
/docs. For new functionality, at leastAPI.mdshould be updatednpm -w mobx run test:performance)