从 react-native-static-server 启动 StaticServer 时出错:undefined 不是对象(评估"this.staticServer.start")



我将我的应用程序与世博会分离,我安装并链接了 react-native-static-server 模块。我在 App.js 文件中添加了这段代码,就在组件定义之前,以查看它是如何工作的:

import StaticServer from 'react-native-static-server';
const server = new StaticServer( 8080, { localOnly: false, keepAlive : true } );
server.start( ).then( ( url ) => {
console.log( "Serving at URL", url );
} );

现在,当我调用 start() 方法时,我收到此错误:

TypeError: undefined is not an object (evaluating 'this.staticServer.start')

应用程序不断崩溃并重新启动。到目前为止,我只在Android上测试了它。有人知道我做错了什么吗?我在 github 上没有看到任何问题。

Android Studio 中适用于我的应用程序的 Logcat 显示以下内容:

D/ReactNative: ReactInstanceManager.createReactContextInBackground()
D/ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
W/unknown:ReactNative: Tried to enqueue runnable on already finished thread: 'native_modules... dropping Runnable.
W/MessageQueue: Handler (com.facebook.react.bridge.queue.MessageQueueThreadHandler) {14699d1} sending message to a Handler on a dead thread
java.lang.IllegalStateException: Handler (com.facebook.react.bridge.queue.MessageQueueThreadHandler) {14699d1} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:545)
at android.os.Handler.enqueueMessage(Handler.java:662)
at android.os.Handler.sendMessageAtTime(Handler.java:631)
at android.os.Handler.sendMessageDelayed(Handler.java:601)
at android.os.Handler.post(Handler.java:357)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl.runOnQueue(MessageQueueThreadImpl.java:61)
at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
at android.os.Looper.loop(Looper.java:164)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
at java.lang.Thread.run(Thread.java:764)

我查看了 react-native-static-server 存储库,并意识到它在过去两个月内没有更新。我假设它可能适用于两个月前的反应原生版本。

我将世博会 sdk 降级为 25,这是基于 react-native@0.52。现在一切正常。

确保在安装 react-native-static-server 后执行pod install。 只需CD iOS,然后安装pod应该可以正常工作。

最新更新