当前位置: 首页 > 知识库问答 >
问题:

在React native Android上定义自定义本机事件

邴宏大
2023-03-14

我试图在Android上的React原生应用程序中定义一个自定义事件。我有原生视图,它有一个原生按钮。当按下按钮时,我想向我的React原生组件发送一条消息,以显示一个模式屏幕。

我遵循了示例,但不理解所有元素,在尝试中做了一些猜测。

在我的ViewManager类中:

public class MyViewManager extends SimpleViewManager<MyView> {

    // Contructor etc...

    @Override
    protected MyView createViewInstance(ThemedReactContext themedReactContext) {
        //... Create and return the view 
    }

    /**
     * Custom native events
     * @return Map of additional events
     */
    @Override
    public @Nullable
    Map getExportedCustomDirectEventTypeConstants() {
        return MapBuilder.of(
                "showModal",
                MapBuilder.of("registrationName", "onPressModalButton")
        );
    }
}

自定义视图:

public class MyView extends View {

    public MyView(Context c)
    {
        super(c);
    }

    public void showModal() {
        Log.d("MyView", "showModal");

        WritableMap event = Arguments.createMap();
        event.putString("showModal", "onPressModalButton");
        ReactContext reactContext = (ReactContext)getContext();
        reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
                getId(),
                "showModal", 
                event);
    }
}

所以我希望,每次调用show Modal时,触发JS事件,我可以在React Native应用程序中显示模态视图。

在我的React组件中,我有:

  class MyNativeComponent extends React.Component {
    static propTypes = {
      ...View.propTypes,
      onPressModalButton: React.PropTypes.func
    }
    render() {
      return <MyView {...this.props} />
    }
  }

  const MyView = requireNativeComponent('MyView', MyNativeComponent, { nativeOnly: {showModal: true} })

  class MyComponent extends React.Component {
    constructor(props) {
      super(props);
      this._showModal = this._showModal.bind(this);
    }

    _showModal(event: Event) {

      console.log("showModal from React!")
        if (!this.props.onPressModalButton) {
          return;
        }
      this.props.onPressModalButton(event.nativeEvent.showModal);
    }

    //...
  }

我不理解的是映射是如何工作的,以及如何定义事件(例如示例中的onChange)。

共有1个答案

田瀚
2023-03-14

嗯,我没有显示我的渲染方法,那就是缺少的部分所在。我错过了传递事件处理方法的prop,在本例中,onPressModalButton={this_u.showmodel}

render() {
  return (
    <View style={styles.container}>
      < MyNativeComponent style={{flex: 1}} onPressModalButton={this._showModal} />
    </View>
  )
}

 类似资料:
  • 在App开发中,经常会遇到页面间传值的需求,比如从新闻列表页进入详情页,需要将新闻id传递过去; Html5Plus规范设计了evalJS方法来解决该问题; 但evalJS方法仅接收字符串参数,涉及多个参数时,需要开发人员手动拼字符串; 为简化开发,mui框架在evalJS方法的基础上,封装了自定义事件,通过自定义事件,用户可以轻松实现多webview间数据传递。 仅能在5+ App及流应用中使用

  • 自定义事件主要会被用于框架、组件设计与实现中。 自定义的事件有许多的创建方式,但实际的业务场景中几乎不会被用到,网络上的文献记载其具体的使用场景也相对较少。 1. 使用 Event 构造函数 使用 Event 构造函数就可以创建一个自定义事件。 案例演示 预览 复制 复制成功! <style> .btn { border: 1px solid #4caf50; padding: 8px 12p

  • 什么是 script ? script 被定义在 composer.json 里,可以是 PHP 类静态方法,也可以是可执行的命令行调用。script 是用来 Composer 执行时执行扩展包里的自定义代码,或者扩展包专属的命令行。 注:只有项目里的 composer.json 会被执行,很多时候我们的项目会依赖于多个扩展,这些扩展里的里 composer.json 配置的 script 将不会

  • 自定义事件 Custom events iScroll还提供额一些你可以挂靠的有用的自定义事件。 使用on(type, fn)方法注册事件。 myScroll = new IScroll('#wrapper'); myScroll.on('scrollEnd', doSomething); 上面的代码会在每次滚动停止是执行doSomething方法。 可以挂靠的事件如下: beforeScrol

  • 我们知道,父组件使用 prop 传递数据给子组件。但子组件怎么跟父组件通信呢?这个时候 Vue 的自定义事件系统就派得上用场了。 使用v-on绑定自定义事件 每个 Vue 实例都实现了事件接口,即: 使用$on(eventName)监听事件 使用$emit(eventName, optionalPayload)触发事件 Vue 的事件系统与浏览器的EventTarget API有所不同。尽管它们的

  • 上述提到的事件都是系统内置的,如触摸事件,键盘事件等。此外,你可以制作自定义事件,这些事件不是由系统控制触发的,而是通过代码手动触发。 创建自定义事件监听器: _listener = EventListenerCustom::create("game_custom_event1", [=](EventCustom* event){ std::string str("Custom event