首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React本机- NativeEventEmitter - JSModule不听事件

React本机- NativeEventEmitter - JSModule不听事件
EN

Stack Overflow用户
提问于 2022-04-10 17:40:14
回答 1查看 669关注 0票数 0

我想要创建一个RN包(不久将在npm上发布),它有一个本地的一面。正因为如此,我认为把它放在我的node_modules dir中是个好主意,所以我可以在开发过程中进行测试,目前只有android。

  • 我在文档后面创建模块,用创建-反应-本机库创建,它用类型记录创建
  • 编写Java模块和包
  • 编写了我模块的JS端

唯一不起作用的是在我的模块js端从本机端获取事件。但是,如果我将事件NativeEventEmitter直接放在我的App.js (RN组件)上,它的工作原理就像一种魅力。

我需要在事件的基础上做一些抽象,这样我就可以公开一个友好的api。

我在模块中所做的每一次更改,都会运行yarn run bob build (来自创建-反应-本机库),然后在测试项目上运行yarn run android

这是我的包目录结构

代码语言:javascript
复制
.
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── android
│   ├── build
│   │   ├── generated
│   │   ├── intermediates
│   │   ├── outputs
│   │   └── tmp
│   ├── build.gradle
│   ├── gradle
│   │   └── wrapper
│   ├── gradlew
│   ├── gradlew.bat
│   ├── local.properties
│   └── src
│       └── main
├── babel.config.js
├── ios
│   ├── GpsState.h
│   ├── GpsState.m
│   └── GpsState.xcodeproj
│       ├── project.pbxproj
│       └── project.xcworkspace
├── lib // that is the build destination dir
│   ├── commonjs
│   │   ├── index.js
│   │   ├── index.js.map
│   │   ├── types.js
│   │   └── types.js.map
│   ├── module
│   │   ├── index.js
│   │   ├── index.js.map
│   │   ├── types.js
│   │   └── types.js.map
│   └── typescript
│       ├── __tests__
│       ├── index.d.ts
│       └── types.d.ts
├── package-lock.json
├── package.json
├── react-native-gps-state.podspec
├── scripts
│   └── bootstrap.js
├── src
│   ├── __tests__
│   │   └── index.test.tsx
│   ├── index.tsx
│   └── types.ts
├── tsconfig.build.json
├── tsconfig.json
└── yarn.lock

我的模块package.json只涉及相关部件

代码语言:javascript
复制
{
  "main": "lib/commonjs/index",
  "module": "lib/module/index",
  "types": "lib/typescript/index.d.ts",
  "react-native": "src/index",
  "source": "src/index",
  ....
  "scripts": {
    "test": "jest",
    "typescript": "tsc --noEmit",
    "lint": "eslint \"**/*.{js,ts,tsx}\"",
    "prepare": "bob build",
    "release": "release-it",
    "example": "yarn --cwd example",
    "pods": "cd example && pod-install --quiet",
    "bootstrap": "yarn example && yarn && yarn pods"
  },
}

我的index.tsx (js是我包的一部分)

代码语言:javascript
复制
import { NativeModules, NativeEventEmitter } from 'react-native';

const emitter = new NativeEventEmitter(NativeModules.GpsState);
emitter.addListener('OnStatusChange', (response: any) => {
    // never reach here, seems theres no listeners attached
    console.log('jsmodule -> OnStatusChange -> received....', response);
});

const GPSState = {
 foo: ()=>NativeModules.GPSState.bar() //those call to native side are working well by the way
 debugEmitter: ()=>NativeModules.GPSState.debugEmitter()
}

export default GPSState

最后,iam也使用MessageQueue来保证事件被分派,它们是

代码语言:javascript
复制
 LOG  GPSState debugEmitter js -> native
 LOG  {"args": [], "method": "debugEmitter", "module": "GPSState", "type": 1}
 LOG  ----------------------------------------------------------------------------------------------------------------------------------------------
 LOG  RCTDeviceEventEmitter emit native -> js
 LOG  {"args": ["OnStatusChange", {"status": 99}], "method": "emit", "module": "RCTDeviceEventEmitter", "type": 0}
 LOG  ----------------------------------------------------------------------------------------------------------------------------------------------

最后一个注意,MessageQueue.spy也不适用于我的js包,只在我的App.js (RN组件)中工作。

谁想近距离看看这是回购

是的,这都是大家

EN

回答 1

Stack Overflow用户

发布于 2022-08-17 02:15:32

使用的react-native不是相同的路径,不能接收事件。

这是因为custom_ library使用react-native of custom_library/node_modules/react-native而不是your_app of your_app/node_modules/react-native

import xxx from 'react-native'修改为custom_ library中的import xxx from 'your_app/node_modules/react-native'以正常接收事件。

使用发布的custom_ library不会导致此问题。预期:它不会安装在devDependencies中定义的react-native

我还想知道为什么不同路径的react-native不能接收事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71819083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档