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

Support for React 19 new APIs #846

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft

Support for React 19 new APIs #846

wants to merge 13 commits into from

Conversation

davesnx
Copy link
Member

@davesnx davesnx commented Jul 16, 2024

This PR adds some of the new functions on React 19.

I left out all the canary stuff: React.promise/React.context/useActionState/async components. In future PRs will do ref as prop

@@ -0,0 +1,53 @@
(* Provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data". *)

module Iterator = struct
Copy link
Member

Choose a reason for hiding this comment

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

I think we added iterator stuff in melange, can you reuse that?

Copy link
Member

Choose a reason for hiding this comment

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

otherwise we should add what we need

Copy link
Member Author

Choose a reason for hiding this comment

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

Totally, added it here to make it pass (that's why it's a draft pr) until I can use melange.dom for this

@@ -648,7 +648,7 @@ type domProps = {
[@mel.optional]
acceptCharset: option(string),
[@mel.optional]
action: option(string), /* uri */
action: option(FormData.t => Js.Promise.t(unit)), /* Since action is taken by "form" as string and React 19 accepts a callback we keep a 'action_' field to avoid a breaking change. */
Copy link
Member

Choose a reason for hiding this comment

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

what do you mean by "keep a 'action_'" field? isn't the field still named action (without underscore?)

Copy link
Member

Choose a reason for hiding this comment

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

I believe this is now a breaking change because we don't accept a string for action anymore

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't find a way to keep both (you can't have 2 mel.as with the same name), so it's either we don't support action like this (and users could cloneElement it) or we break with action.

Not fan of either

Choose a reason for hiding this comment

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

as I understand action is only for <form>? maybe create <Form />, a separate component specifically for RSC apps, not to make API more complex for a majority of use cases?

Copy link
Member

Choose a reason for hiding this comment

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

we thought we'd be able to support this by e.g. adding @mel.unwrap support in @@deriving jsProperties but it turns out it's unsound (some discussion in melange-re/melange#1162).

it'd be kinda nice if ppxlib allowed to make the original type abstract, which would remove the unsoundness.

type blob
type entryValue

let classify : entryValue -> [> `String of string | `File of file ] =
Copy link
Member

Choose a reason for hiding this comment

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

I think we could just use Js.Types.classify as I mentioned in the melange PR comment.

@@ -886,6 +887,31 @@ external useDebugValue: ('value, ~format: 'value => string=?, unit) => unit =

module Experimental = {
/* This module is used to bind to APIs for future versions of React. There is no guarantee of backwards compatibility or stability. */
[@mel.module "react"] external usePromise: Js.Promise.t('a) => 'a = "use";
[@mel.module "react"] external useContext: Context.t('a) => 'a = "use";
[@mel.module "react"] external use: 'a => 'b = "use";
Copy link
Member

Choose a reason for hiding this comment

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

should we default the name use to Js.Promise.t('a) and add useAny for 'a => 'b?

Choose a reason for hiding this comment

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

What's the use case for use : 'a -> 'b, which is essentially Obj.magic?

[@mel.module "react"]
external useOptimistic:
('state, ('state, 'optimisticValue) => 'state) =>
('state, 'optimisticValue => unit) =
Copy link
Member

Choose a reason for hiding this comment

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

addOptimistic is the dispatching function to call when you have an optimistic update. It takes one argument, optimisticValue, of any type and will call the updateFn with state and optimisticValue.

if it'll call the updateFn it will return 'state

Suggested change
('state, 'optimisticValue => unit) =
('state, 'optimisticValue => 'state) =

Copy link
Member

Choose a reason for hiding this comment

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

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