简体   繁体   English

通过React Native中的App.js向自定义组件添加样式

[英]Adding styles to a custom component through App.js in React Native

Ok guys, I've been trying to figure this out for the past 3 days and I can't find a solution, please keep in mind that I am self-taught and I've been studying react native for like 3 months now. 好的,过去3天,我一直在努力解决这个问题,但找不到解决方案,请记住,我是自学成才的,并且我已经研究了本机反应已有3个月了。

Anyways, I have a custom button with a defined style and everytime that I render my button it loads with the style presented in its file: 无论如何,我都有一个自定义按钮,具有定义的样式,每次渲染按钮时,它都会加载其文件中显示的样式:

Botaozudo.js: Botaozudo.js:

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

export default class Botaozudo extends React.Component {
    render() {
        const { titulo, evento } = this.props;
        return (

                    <TouchableOpacity
                        style={styles.button}
                        onPress={() => evento()}>
                            <Text style={styles.txtButton}>{titulo}</Text>
                    </TouchableOpacity>

        );
    }
}

const styles = StyleSheet.create({
    btnAlinhar: {
        alignItems: 'flex-end',
        marginRight: 20,
        paddingTop: 7
    },
    button: {
        backgroundColor: '#a082c9',
        width: 100,
        height: 40,
        borderRadius: 10
    },
    button2: {
        backgroundColor: '#a082c9',
        width: 300,
        height: 90,
        borderRadius: 10
    },
    txtButton: {
        color: '#fff',
        fontSize: 20,
        textAlign: 'center',
        paddingVertical: 5
    }
});

Lets say that I want two different buttons on my App.js , one that looks like exactly as above and another one with different size and background color. 可以说,我要在App.js上使用两个不同的按钮,一个看起来与上面完全一样,另一个按钮具有不同的大小和背景颜色。 In my mind I just have to do something like this (for the different one): 在我的脑海中,我只需要做这样的事情(针对另一种):

<Botaozudo 
    style={styles.newBtn} 
    titulo='I am a button' 
    event={() => 
        console.log('yup I am a button')}/>

const styles = StyleSheet.create({
    newBtn: {
        backgroundColor: '#7c7070',
        width: 200,
        height: 100
    }
});

But the thing is that my Botaozudo doesn't know what that style={} prop means. 但问题是我的Botaozudo不知道那style = {}道具的含义。 And what I can't figure out is HOW to make my custom component understand it. 我不知道的是如何使我的自定义组件理解它。

Thanks in advance, 提前致谢,

Install https://www.npmjs.com/package/prop-types 安装https://www.npmjs.com/package/prop-types

Then in Botaozudo.js : 然后在Botaozudo.js中

import React from 'react';
import { Text, TouchableOpacity, StyleSheet } from 'react-native';
import PropTypes from 'prop-types';

export default class Botaozudo extends React.Component {
  static propTypes = {
    // Custom style for Botaozudo. Requires object
    componentStyle: PropTypes.object,
  };

  static defaultProps = {
    componentStyle: styles,
  };

  render() {
    const { titulo, event, componentStyle } = this.props;
    return (
      <TouchableOpacity style={componentStyle.newBtn} onPress={() => event()}>
        <Text style={styles.txtButton}>{titulo}</Text>
      </TouchableOpacity>
    );
  }
}

const styles = StyleSheet.create({
  btnAlinhar: {
    alignItems: 'flex-end',
    marginRight: 20,
    paddingTop: 7,
  },
  button: {
    backgroundColor: '#a082c9',
    width: 100,
    height: 40,
    borderRadius: 10,
  },
  button2: {
    backgroundColor: '#a082c9',
    width: 300,
    height: 90,
    borderRadius: 10,
  },
  txtButton: {
    color: '#fff',
    fontSize: 20,
    textAlign: 'center',
    paddingVertical: 5,
  },
});

and in App.js : 并在App.js中

<Botaozudo 
    componentStyle={styles} 
    titulo='I am a button' 
    event={() => 
        console.log('yup I am a button')}/>

const styles = StyleSheet.create({
    newBtn: {
        backgroundColor: '#7c7070',
        width: 200,
        height: 100
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM