英文:
Accessibility Identifier issue in ReactNative
问题
iOS 使用 testID
,Android 使用 accessibilityLabel
来设置我的 React Native 组件的可访问性标识。对于 iOS,一切正常,但对于 Android,我的标识符后面会附加 ,
(逗号和空格)。我不确定是什么原因导致了这个问题。以下是我的代码:
const renderAccessibilityLabel = (str) => {
const propsForAutomation = {};
if (Platform.OS === "ios") {
propsForAutomation.testID = str;
} else {
propsForAutomation.accessibilityLabel = str;
}
return propsForAutomation;
};
// 在渲染方法中使用:
<Text {...renderAccessibilityLabel("MyText")}>{MyText}</Text>
结果:
iOS:MyText
Android:MyText,
我不知道代码有什么问题
英文:
I am setting accessibility identifier for my components in react native using testID
in iOS and accessibilityLabel
in android. For iOS is working fine but for Android it my identifier is appended with ,
(a comma and a space). I am not sure what is causing issue. Here is my code:
const renderAccessibilityLabel = (str) => {
const propsForAutomation = {};
if (Platform.OS === "ios") {
propsForAutomation.testID = str;
} else {
propsForAutomation.accessibilityLabel = str;
}
return propsForAutomation;
};
// Inside render method:
<Text {...renderAccessibilityLabel("MyText")}>{MyText}</Text>
result > ios: MyText
android: MyText,
I don't know whats wrong with code
答案1
得分: 0
有一个在 React Native 中的 bug,已在 0.60.5 版本中修复。
我猜测你正在使用一个较早的版本 (0.60),更新版本将会纠正此问题。
问题出现在 ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java
文件中。
第 178 行:contentDescription.append(accessibilityLabel + ", ");
如果因某种原因无法更新,可以通过在 if (contentDescription.length() > 0) {
行之前添加 contentDescription = contentDescription.replaceAll(", $", "");
来修复。
或者,可以替换该文件以包含此更改:https://github.com/facebook/react-native/commit/812abfd。
英文:
There was a bug with react native that was fixed in 0.60.5.
My guess is you are using an earlier version (0.60), updating will correct this.
Problem occurs in ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java
Line 178 contentDescription.append(accessibilityLabel + ", ");
Just in case you can't update for any reason, it can be fixed by adding contentDescription = contentDescription.replaceAll(", $", "");
just above the line if (contentDescription.length() > 0) {
Or by replacing the file with the changes in https://github.com/facebook/react-native/commit/812abfd.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论