Javascript 样式不会传递给自定义组件
我有一个自定义按钮组件与此代码Javascript 样式不会传递给自定义组件,javascript,reactjs,react-native,components,Javascript,Reactjs,React Native,Components,我有一个自定义按钮组件与此代码 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
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;
从“React”导入React;
从“react native”导入{TouchableOpacity,View,Text};
从“道具类型”导入道具类型;
从“./styles”导入样式;
const CustomBtn=({props,text,onPress})=>(
{text}
);
自定义BTN={
text:PropTypes.string,
onPress:PropTypes.func,
};
导出默认CustomBtn;
我想在我的视图中覆盖我写入的组件的样式(边距、填充)
<CustomBtn style={styles.BtnMargin} onPress={this.handlePressLogin} text="Login" />
但是我的自定义按钮不符合样式。如何更改自定义btn的代码以解决此问题?您使用的代码错误。如果查看签名,您会注意到它接受道具作为参数:
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const CustomBtn = (props) => (
<TouchableOpacity {...props} onPress={props.onPress}>
<View style={styles.button}>
<Text style={styles.text}>{props.text}</Text>
</View>
</TouchableOpacity>
);