我正在try 创建一个Fabric组件.我大部分时间都在工作.我面临的唯一问题是无法让click listener为fabric组件工作.

我创建了一个等级库文件

import type {HostComponent, ViewProps} from 'react-native';
import type {DirectEventHandler} from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

type Event = Readonly<{
  value: string;
}>;

interface NativeProps extends ViewProps {
  text: string;
   onClickHandler?: BubblingEventHandler<Event>; ////Event name should start with on
}

export default codegenNativeComponent<NativeProps>(
  'MyButtonView',
) as HostComponent<NativeProps>;

对于android my fabric组件,如下所示

public class MyButtonView extends androidx.appcompat.widget.AppCompatButton {

    public MyButtonView(Context context) {
        super(context);
        configureViews();
    }

    private void configureViews(){
        setBackgroundColor(Color.YELLOW);
        setOnClickListener(view -> {
            onReceiveNativeEvent();
        });
    }

    public void onReceiveNativeEvent() {
        WritableMap event = Arguments.createMap();
        event.putString("message", "MyMessage");
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTModernEventEmitter.class)
                .receiveEvent(getId(),getId(),"onClickHandler",true,0,event,1);

    }
}

不确定在receiveEvent中通过first param as intcanCoalesceEventcustomCoalesceKeycategory

ViewManager中,我也添加了以下代码

@Nullable
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
    Log.i("here55","999999");
    return MapBuilder.of("onClickHandler",
            MapBuilder.of("registrationName", "onClickHandler")
    );
}

对于android,我没有得到从nativeandroidjs侧的callback

我关注的是https://reactnative.dev/docs/native-components-android,但它是针对旧架构的,我认为不适用于fabric个组件

iOShttps://reactnative.dev/docs/native-components-ios相同,doc对新架构没有太大帮助

对于iOS,我创建了headerobjective-c++文件

通常,如果我们想添加属性,我们会这样做

RCT_EXPORT_VIEW_PROPERTY(text, NSString)

不确定如何为事件处理程序执行此操作

推荐答案

iOS

假设您发布的本机规范是正确的(我不太熟悉TypeScript+Codegen duo),当您在ios/目录中运行pod install命令时,Codegen应该在React-Codegen pod中生成EventEmitters个cpp文件(+头)(您可以看到下面如何在XCode中找到它)

link to the screenshot with Pods structure

然后,您可以在Objective-C++代码中使用这些类来发出事件:

if (_eventEmitter != nullptr) {
    std::dynamic_pointer_cast<const facebook::react::$$NAME OF YOUR EVENT EMITTER TYPE$$>(_eventEmitter)
        ->onEvent(facebook::react::$$NAME OF YOUR EVENT EMITTER TYPE::OnEvent{.value = $$VALUE$$});
}

请参阅下面的链接,了解如何在react-native-screens中完成

  1. Native component spec in Flow
  2. Event emission in Obj-C++ code
  3. Event exposure in screen manager-我不太确定这条线是否必要,还没有测试出来.

Android

  1. 要触发codegen,只需运行本机构建.
  2. 将本机事件类定义为done here
  3. 看看这里的how to dispatch event
  4. Export your event types

Java相关问答推荐

为什么如果数组列表中有重复项,我的代码SOMETIMES不返回true?

Java中不同包中的类之间的配置共享

Java WireMock定义存根在Cucumber并行执行的多线程测试中失败

工件部署期间出错[Tomcat 8.5.45]

Java中是否有某种类型的池可以避免重复最近的算术运算?

Hibernate 6支持Joda DateTime吗?

嵌入式ActiveMQ Artemis Web控制台加载错误

基于调车场算法的科学计算器

计算两个浮点数之间的距离是否对称?

对于亚洲/香港,使用ResolverStyle.STRICT的LocalDate.parse返回意外结果

PDFBox未加载内容

try 判断可选参数是否为空时出现空类型安全警告

如何在JavaFX中处理多个按钮

使用htmlunit和java单击按钮

Cordova Android Gradle内部版本组件不兼容

Java Flux中的延迟增加

Android无法在Java代码中调用Kotlin代码,原因是在Companion中使用Kotlin枚举时

如何在Spring Security中设置一个任何人都可以打开的主页?

在JSON上获取反斜杠

如何在 WebSphere Application Server 内的托管线程上运行 BatchEE 作业(job)?