我正在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 int
、canCoalesceEvent
、customCoalesceKey
和category
在ViewManager
中,我也添加了以下代码
@Nullable
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Log.i("here55","999999");
return MapBuilder.of("onClickHandler",
MapBuilder.of("registrationName", "onClickHandler")
);
}
对于android
,我没有得到从native
到android
到js
侧的callback
我关注的是https://reactnative.dev/docs/native-components-android,但它是针对旧架构的,我认为不适用于fabric
个组件
与iOS
https://reactnative.dev/docs/native-components-ios相同,doc对新架构没有太大帮助
对于iOS
,我创建了header
和objective-c++
文件
通常,如果我们想添加属性,我们会这样做
RCT_EXPORT_VIEW_PROPERTY(text, NSString)
不确定如何为事件处理程序执行此操作