Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 19 support #3985

Merged
merged 13 commits into from
Dec 11, 2024
Merged

React 19 support #3985

merged 13 commits into from
Dec 11, 2024

Conversation

imjordanxd
Copy link
Contributor

@imjordanxd imjordanxd commented Dec 9, 2024

Code change checklist

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

Note: before merging, I think I should also dup any propType checks inside render to be compatible across multiple React versions. I also intentionally haven't bumped the types for react and react-dom to keep the propTypes types such as Validator, Requireable etc.

Copy link

changeset-bot bot commented Dec 9, 2024

🦋 Changeset detected

Latest commit: f1dbfeb

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

This PR includes changesets to release 2 packages
Name Type
mobx-react-lite Minor
mobx-react Minor

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

@imjordanxd imjordanxd marked this pull request as ready for review December 11, 2024 03:15
@@ -377,7 +377,7 @@ describe("is used to keep observable within component body", () => {
})
expect(container.querySelector("span")!.innerHTML).toBe("22")
expect(counterRender).toBe(2)
expect(observerRender).toBe(3)
Copy link
Contributor Author

@imjordanxd imjordanxd Dec 11, 2024

Choose a reason for hiding this comment

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

React now renders the observer an extra time for some reason. The reaction only fires once though when the store mutates

Copy link
Member

Choose a reason for hiding this comment

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

these hooks are all basically anti-pattern anyway, so good enough :)

Comment on lines +9 to +13
if (children && render) {
console.error(
"MobX Observer: Do not use children and render in the same time in `Observer`"
)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

propTypes removed in v19 so I've duped this inside the render body

@@ -49,7 +49,7 @@ test("computed properties react to props when using hooks", async () => {
act(() => {
jest.runAllTimers()
})
expect(seen).toEqual(["parent", 0, "parent", 2])
expect(seen).toEqual(["parent", 0, "parent", 2, 2])
Copy link
Contributor Author

Choose a reason for hiding this comment

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

}, [multiplier])
useEffect(() => setMultiplier(3), [])
},
{
Copy link
Member

Choose a reason for hiding this comment

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

NIT: This pattern reoccures quite a few times in the test, might be nice to make a utility for it? (separate PR is fine if interested)

Copy link
Member

@mweststrate mweststrate left a comment

Choose a reason for hiding this comment

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

This looks great, thanks so much for picking this up!

@mweststrate mweststrate merged commit 2587df3 into mobxjs:main Dec 11, 2024
1 check passed
@github-actions github-actions bot mentioned this pull request Dec 11, 2024
interface IObserverProps {
children?(): React.ReactElement | null
render?(): React.ReactElement | null
}

function ObserverComponent({ children, render }: IObserverProps) {
if (children && render) {
console.error(
"MobX Observer: Do not use children and render in the same time in `Observer`"
Copy link

@levrik levrik Dec 17, 2024

Choose a reason for hiding this comment

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

typo: in the same time -> at the same time

@Venryx
Copy link
Contributor

Venryx commented Mar 8, 2025

I must be missing something, but I am getting TypeScript errors when trying to use [email protected] in a repo with [email protected] and @types/[email protected].

The errors relate to exports such as React.Requireable not being available in @types/[email protected]:

node_modules/mobx-react/dist/propTypes.d.ts:2:38 - error TS2694: Namespace 'React' has no exported member 'Requireable'.
2     observableArray: import("react").Requireable<any>;

node_modules/mobx-react/dist/propTypes.d.ts:3:54 - error TS2694: Namespace 'React' has no exported member 'Validator'.  
3     observableArrayOf: (typeChecker: import("react").Validator<any>) => import("react").Requireable<any>;

Does anyone else see the same issue? (will keep looking into it in the meantime, but it seems to me that mobx-react is just relying on react type exports that no longer exist)

@Venryx
Copy link
Contributor

Venryx commented Mar 9, 2025

Okay yeah, it looks like in @types/react@18, there were Requireable and Validator exports: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/da70d4aee37cd531df1fc030c1e068fe51d3b5e9/types/react/v18/index.d.ts#L4149-L4157

    /**
     * @deprecated Use `Validator` from the ´prop-types` instead.
     */
    type Validator<T> = PropTypes.Validator<T>;

    /**
     * @deprecated Use `Requireable` from the ´prop-types` instead.
     */
    type Requireable<T> = PropTypes.Requireable<T>;

However, as seen, they were marked as deprecated.

And then in @types/react@19, they were removed (ctrl+f shows no results): https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts

So if Requireable and Validator are still needed by mobx-react, they need to import from the prop-types package now, not react.

Venryx added a commit to Venryx/react-vmenu that referenced this pull request Mar 9, 2025
One remaining error is in mobx-react, where it references import("react").Requireable and Validator, where those are now only in prop-types, not mobx-react: mobxjs/mobx#3985 (comment)

Ignoring for now / until I get a response.
@mweststrate
Copy link
Member

mweststrate commented Mar 9, 2025 via email

@Venryx
Copy link
Contributor

Venryx commented Mar 9, 2025

@mweststrate Okay, I've created a PR for it here: #4534

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.

4 participants