我有一个 React Native 应用程序,我需要为其实现外部键盘导航支持。我正在尝试安装第三方软件包('react-native-a11y')来帮助解决此问题。但是,我已经有一个名为react-native-a11y的本地包,重命名/重新配置将需要大量工作,因此我尝试使用别名('react-native-a11y-helper' ).
我的 package.json 中有类似以下内容:
"react-native-a11y-alias": "npm:[email protected]",
pnpm install 后,该包将显示在 node_modules 中,并以所需的别名作为包名称。然后在我的代码中,我从包中导入一个组件,在构建时没有任何问题:
import { KeyboardFocusView } from 'react-native-a11y-alias';
但是当我在 Android 设备上运行该应用程序时,metro 捆绑程序会抛出以下错误:
error: Error: Unable to resolve module react-native-a11y-helper from <file>.tsx: react-native-a11y-helper could not be found within the project or in these directories:
node_modules
../node_modules
我也尝试过使用
require
导入,但没有成功。
如何在RN中正确使用这个包?
如
react-native-a11y
的文件:
这个库尚未完成,目前处于测试阶段。
所以你需要仔细遵循安装公会:
npm i react-native-a11y
cd ios && pod install
MainActivity.java
: //android/app/src/main/java/com/project-name/MainActivity.java
...
import android.content.Intent;
import android.content.res.Configuration;
...
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Intent intent = new Intent("onConfigurationChanged");
intent.putExtra("newConfig", newConfig);
this.sendBroadcast(intent);
}
watch: examples/A11ySample/App.tsx
export const App = () => {
return (
<A11yProvider>
// content here
</A11yProvider>
);
};