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

Using @microsoft/fast-react-wrapper with refs & accessing public element members #6909

Closed
DavidVollmers opened this issue Feb 8, 2024 · 2 comments
Labels
closed:obsolete No longer valid status:needs-investigation Needs additional investigation

Comments

@DavidVollmers
Copy link

DavidVollmers commented Feb 8, 2024

I choose a bug report for this but I guess this will be more a discussion-like thing.

When working with @microsoft/fast-react-wrapper (version 1.0.0-alpha.27) I run into two problems which I think originate from the same "culprit":

First one is using refs with wrapped elements and second one is calling public methods of the original FASTElement using a ref.

So in general when you want to use the ref prop on a wrapped FASTElement you need to do it like this:

const myRef = React.createRef<Component<typeof MyFASTElement>>();

function onClick() {
  myRef.current.myMethod(); //<-- will not work since the member is not included in the type definition
}

<MyFASTElement ref={myRef} onClick={onClick} />

Problem is that this hides the fact that ref contains an instance of MyFASTElement and thus you cannot access any public members.

I am not yet 100% familiar with this issue but I belive what is missing is a proper union type for the ReactWrapper<TElement> type definition. Something like this:

export declare interface ReactWrapper<TElement extends HTMLElement, TEvents> extends Constructable<ReactModule.Component<ReactWrapperProps<TElement, TEvents>>> {
    displayName: string;
} & TElement & Component

Important is the & TElement & Component part. This way you can build refs like this and have the current value be an instance of the actual element:

const myRef = React.createRef<typeof MyFASTElement>();

function onClick() {
  myRef.current.myMethod();
}

<MyFASTElement ref={myRef} onClick={onClick} />

Any thoughts on this? I am still playing around with it but so far it looks promising...

@DavidVollmers DavidVollmers added the status:triage New Issue - needs triage label Feb 8, 2024
@chrisdholt chrisdholt added status:needs-investigation Needs additional investigation and removed status:triage New Issue - needs triage labels Feb 16, 2024
@janechu
Copy link
Collaborator

janechu commented May 29, 2024

Unfortunately @microsoft/fast-react-wrapper is being deprecated, refer to #6955.

@janechu janechu closed this as completed May 29, 2024
@janechu janechu added the closed:obsolete No longer valid label May 29, 2024
@chrisdholt
Copy link
Member

Strongly recommend https://www.npmjs.com/package/custom-element-react-wrappers as an alternative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed:obsolete No longer valid status:needs-investigation Needs additional investigation
Projects
None yet
Development

No branches or pull requests

3 participants