繁体   English   中英

样式未传递给自定义组件React Native

[英]Style is not passed down to Custom Component React Native

我有一个带有此代码的自定义按钮组件

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

import styles from './styles';

const CustomBtn = ({props, text, onPress }) => (
    <TouchableOpacity {...props} onPress={onPress}>
        <View style={styles.button}>
             <Text style={styles.text}>{text}</Text>
        </View>
    </TouchableOpacity>
);

CustomBtn = {
  text: PropTypes.string,
  onPress: PropTypes.func,
};

export default CustomBtn;

我想在我编写的视图中覆盖组件的样式(边距,填充)

<CustomBtn style={styles.BtnMargin} onPress={this.handlePressLogin} text="Login" />

但是我的自定义按钮没有样式。 如何更改自定义btn的代码来解决此问题?

您正在使用无状态组件错误。 如果查看签名,您会注意到它接受props作为参数:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

在线路{...props} varibale props未定义,因为它是相同的this.props.props在正常组分。 您真正想要做的是:

const CustomBtn = (props) => (
    <TouchableOpacity {...props} onPress={props.onPress}>
        <View style={styles.button}>
             <Text style={styles.text}>{props.text}</Text>
        </View>
    </TouchableOpacity>
);

暂无
暂无

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

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