当在设备上运行时,会出现这样的错误"the title prop of a button must be a string - react native"

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

export default class sample extends Component {

render() {
return (
    <Button
      style={{fontSize: 20, color: 'green'}}
      styleDisabled={{color: 'red'}}
      onPress={() => this._handlePress()}>
      title="Press Me"
    </Button>
);
}


_handlePress() {
  console.log('Pressed!');
}
}

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

推荐答案

我认为你过早地关闭了按钮标签.

<Button
  style={{fontSize: 20, color: 'green'}}
  styleDisabled={{color: 'red'}}
  onPress={() => this._handlePress()}> // <-- closed tag here
  title="Press Me"
</Button>

只需在title属性后关闭标记

<Button
  style={{fontSize: 20, color: 'green'}}
  styleDisabled={{color: 'red'}}
  onPress={() => this._handlePress()}
  title="Press Me"
>
  Press Me
</Button>

React-native相关问答推荐

PubNubReact无法工作,因为它已被废弃

如何更改分隔底部标签和屏幕内容的线的 colored颜色 ?React Native Tab导航器

React-Native:请纠正我对状态如何工作的误解 - 动态文本输入

@testing-library/react-native -> 渲染 -> TypeError:无法读取未定义的属性(读取存在)

导航到另一个屏幕后清空搜索屏幕

React-native 重置数据库

React Native 的 Firebase 初始错误(无法读取未定义的属性getItem)

React Native WebView onMessage 没有做任何事情

react-native StyleSheet 属性和选项列表

react-native: 'adb' 不是内部或外部命令、可运行程序或批处理文件

使用react native获取设备令牌

redux saga 的delay延迟功能不起作用

试图注册两个同名的视图 RNGestureHandlerButton

如何在react-native中设置 FlatList 的刷新指示器?

是否可以将 Image.getSize 与静态图像文件一起使用?

React Native Android 文本组件额外填充

React Native require(image) 返回数字

使用 Jest 的 react-navigation 测试组件

如何在 React Native 中等待 Alert 对话框的响应?

React Navigation 切换背景 colored颜色 和样式 StackNavigator