microsoft / microsoft/react-native-windows

How Func<T,TResult> works in a ReactMethod?

Open
#8,321 2 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area: Native Modules enhancement Needs: Dev Design Workstream: Module Support
Dominant language
C++
Stars
17.3k
Forks
1.2k
Avg merge
1d 13h
Merged PRs (30d)
33

Description

Hello, I just want to ask a question about how Func<T,TResult> works as an argument in a ReactMethod. I know we have Action<T> and it works well as a ReactMethod argument, but Action only works for void functions and I need to capture the returned value.

This is my C# code:

[ReactMethod("sampleFunction")]
public void SampleFunction(string sample,  Func<JSValueObject, JSValue> handler)
{
   var obj = new JSValueObject()
   {
       { "sample1", "sample1" },
       { "sample2", "sample2"},
       { "arguments", "arguments"}
   };

   JSValue result = handler(obj);
   Console.WriteLine(result);
}

And this is my Typescript code:

const getCallback = function() {
    return function(result) : string {
      console.log(result.sample1);
      return "thank you";
    };
};

NativeModules.SampleModule.sampleFunction("sample", getCallback());

Actually, I cant build my project because I getting this error in my SampleFunction:
image

If it helps, the error happens in line 46.
image

How I can fix this? Also, my Js call is correct?
Thanks in advance :)

My react-native and react-native-windows versions:
"react-native-windows": "0.64.6",
"react-native": "0.64.1"

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the C# SampleFunction ReactMethod and the TypeScript NativeModules.SampleModule.sampleFunction call, then reproduce the build error reported at line 46 using React Native Windows 0.64.6. Done means determining whether this callback signature is supported and identifying the correct supported call shape, with the build error resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, react-native, typescript
Domain
api, desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.