Error when trying to use Button in react-native

2020-02-11 04:34发布

问题:

When making a simple react-native program I cannot get the <Button> component to work. The error I always get after react-native run-android is

java.lang.String cannot be cast to com.facebook.react.uimanager.AccessibilityDelegateUtil$AccessibilityRole

When I omit the button, everything works fine, and I can click the Text just fine.

code :

import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button } from 'react-native';



type Props = {};
export default class App extends Component<Props> {
  render() {
    return (
        <View style={styles.container}>

        <Button onPress={()=>console.log('hi')} title="go"/>
        <Text onPress={()=>console.log('hi')}>Welcome to React Native!</Text>

      </View>
        );
      }
 }

some dependencies :

  "dependencies": {
 maar eens alles goed staat hoop ik gewoon dat de miserie gedaan is, cf   "react": "16.6.0-alpha.8af6728",
    "react-native": "0.57.3"
  },

回答1:

I've just tested your code on version 0.55. It was perfectly correct and worked fine. I'm likely to have error with your react-native version.

init lower version

react-native init --version="react-native@0.55.0" YOUR_APP_NAME

before that try this also

<Button
    onPress={() => alert('hi')}
    title="Press Me"
 >
   Press Me
</Button>


回答2:

A new version of react-native 0.57.4 is now out. Please update your react native version. I tested with the new version and just working fine now.

Hope it works for you too! :)