2017-09-25 16 views

回答

0

什么,我做的是对的src/组件名称的文件夹。在那里创建一个common/目录。现在创建一个文件名称Button.js

您可以在该按钮中定义所需的全部内容。在我的情况下,我通过按钮的onPress动作一些样式的按钮和一些文本的按钮。

您也可以称为“孩子们”的属性,如果你想把什么东西裹在标签传承给像例子中的组件的下方

<Button>children</Button> 

我的组件看起来像这样

import React from 'react' 
import { 
    Text, 
    TouchableOpacity, 
    StyleSheet 
} from 'react-native' 

const Button = ({ children, onPress, style, buttonTextStyle }) => { 
    const { 
     buttonStyle, 
     textStyle 
    } = styles 

    return (
    <TouchableOpacity style={[buttonStyle, style]} onPress={onPress}> 
     <Text style={[textStyle, buttonTextStyle]}> 
     {children} 
     </Text> 
    </TouchableOpacity> 
) 
} 

const styles = StyleSheet.create({ 
    textStyle: { 
    alignSelf: 'center', 
    color: '#FFFFFF', 
    fontSize: 16, 
    fontWeight: '600', 
    paddingTop: 10, 
    paddingBottom: 10 
    }, 
    buttonStyle: { 
    flex: 1, 
    alignSelf: 'stretch', 
    backgroundColor: '#17b5b3' 
    } 
}) 

export { Button } 

您可以使用TouchableOpacity组件从react-native或TouchableWithoutFeedback或其他任何方式使用TouchableOpacity进行演示。

您可以通过该文件夹

import Button from './common/Button' 
+0

你将如何调用我们想要这个按钮类此按钮REFFERENCE调用它......就像从” ../导入此>进口****。 ./../Button“和内部渲染(<**** />)?? @Panagiotis Vrs – Vijay

+0

你可以把它放在你的类的render()函数里,就像render(){return

+0

谢谢,伙计,它的工作真棒... – Vijay