gpt4 book ai didi

android - 为自定义 native 组件 react native 所需的未知模块

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:02:30 25 4
gpt4 key购买 nike

我正在尝试为 ReactNative 实现自定义 native 组件。根据文档,我创建了如下所示的所有组件。但我仍然收到错误 Required Unknown Module "MyCustomToast"。请帮我解决这个问题。

SampleToast.java:-

public class SampleToast extends ReactContextBaseJavaModule {


public SampleToast(ReactApplicationContext reactContext) {
super(reactContext);
}

@Override
public String getName() {
return "MyCustomToast";
}

@ReactMethod
public void showToast(String message) {
Toast.makeText(getReactApplicationContext(), message, Toast.LENGTH_LONG).show();
}

MakeReactPackage.java:-

public class MakeReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {

List<NativeModule> modules = new ArrayList<>();
modules.add(new SampleToast(reactContext));
return modules;

}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.asList();
}

MainActivity.java:-

public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {

private ReactInstanceManager mReactInstanceManager;
private ReactRootView mReactRootView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);

mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new MakeReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();

mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorldBha", null);

setContentView(mReactRootView);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
}

@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}

@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}

@Override
protected void onPause() {
super.onPause();

if (mReactInstanceManager != null) {
mReactInstanceManager.onPause();
}
}

@Override
protected void onResume() {
super.onResume();

if (mReactInstanceManager != null) {
mReactInstanceManager.onResume(this);
}
}}

在 JS 中

react-native.js:-

MyCustomToast:require('MyCustomToast'),

SampleToast.js:-

 'use strict';

/**
* This exposes the native ToastAndroid module as a JS module. This has a function 'show'
* which takes the following parameters:
*
* 1. String message: A string with the text to toast
* 2. int duration: The duration of the toast. May be ToastAndroid.SHORT or ToastAndroid.LONG
*/
var { NativeModules } = require('react-native');
module.exports = NativeModules.MyCustomToast;

Index.android.js:-

    /**
* Sample React Native App
* https://github.com/facebook/react-native
*/
'use strict';

var React = require('react-native');
var toastMessage = require('SampleToast')
var {
AppRegistry,
StyleSheet,
Text,
View,
NativeModules
} = React;




var HelloWorldBha = React.createClass({
render: function () {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<Text style={styles.instructions}>
Shake or press menu button for dev menu
</Text>


toastMessage.show('Bharath Kumar');

</View>
);
}
});

var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

AppRegistry.registerComponent('HelloWorldBha', () => HelloWorldBha);

最佳答案

在 SampleToast.js 中使用 providesModule 注释。所以 SampleToast.js 变成:

/**
* @providesModule SampleToast
*/

'use strict';

/**
* This exposes the native ToastAndroid module as a JS module. This has a function 'show'
* which takes the following parameters:
*
* 1. String message: A string with the text to toast
* 2. int duration: The duration of the toast. May be ToastAndroid.SHORT or ToastAndroid.LONG
*/
var { NativeModules } = require('react-native');
module.exports = NativeModules.MyCustomToast;

或者,当您在 Index.android.js 中使用 require('SampleToast') 时,您需要提供相对路径。因此,如果 SampleToast.js 和 Index.android.js 位于同一目录中,您将需要

var toastMessage = require('./SampleToast');

就我个人而言,我更喜欢第一种选择。

关于android - 为自定义 native 组件 react native 所需的未知模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33145554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com