如何在 React Native 中将字符串作为道具传递

Saw*_*wwy 5 android react-native

我面临的情况是我的组件不会呈现文本,除非它作为 ListView 的一部分呈现。例如,这个效果很好:

<ListView
  dataSource={this.state.dataSource}
  renderRow={(rowData) =>
    <View style={styles.buttonContainer}>
      <Button
        onPress={() => navigate('Team', { team: rowData })}
        title={rowData.name}
      />
    </View>
  }
/>
Run Code Online (Sandbox Code Playgroud)

但是这个不起作用,按钮不显示任何文本,就好像它没有收到任何东西一样

<View style={styles.container}>
  <View style={styles.buttonContainer}>
    <Button
      onPress={() => navigate('Team')}
      title={'Sample Text'}
    />
  </View>
</View>
Run Code Online (Sandbox Code Playgroud)

我的 Button 组件返回如下所示:

return (
  <TouchableOpacity onPress={onPress} style={buttonStyle}>
    <Text style={textStyle}>
      {this.props.title}
    </Text>
  </TouchableOpacity>
)
Run Code Online (Sandbox Code Playgroud)

console.log(this.props.title)打开了 render() ,远程调试器控制台中显示以下内容:
undefined "Sample Text"

package.json 中的依赖项

"react": "16.0.0-alpha.6", "react-native": "^0.43.4", "react-navigation": "git+https://github.com/react-community/react-navigation.git"

wvi*_*oso 5

尝试删除这些括号,因为您不需要它们: title='Sample Text'