Reactから来て、小道具を渡すのは同じだと思っていましたが、どうやらそうではありませんか?
サインインしたいログインフォームがあり、異なるスタイルのサインアップボタンがあります。
私のログインフォームには、次のメソッドがあります。
renderButton (type, text) {
if (this.state.loading) {
return <Spinner size="small" />;
}
let btnColors = {
bgColor: colors.whiteText,
textColor: colors.primaryTeal
};
if (type === "signUp") {
btnColors.bgColor = colors.whiteText;
btnColors.textColor = colors.primaryTeal;
} else if (type === "logIn") {
btnColors.bgColor = colors.darkTeal;
btnColors.textColor = colors.whiteText;
}
return (
<Button colors={btnColors} onPress={this.onButtonPress.bind(this)}>
{text}
</Button>
);
}
Render で呼び出される:
{this.renderButton("signUp", "SIGN UP")}
Button.js は次のようになります。
import React from 'react';
import { Text, TouchableOpacity, View, StyleSheet} from 'react-native';
const Button = ({colors, onPress, children }) => {
const styles = StyleSheet.create({
textStyle: {
alignSelf: 'center',
color: colors.textColor,
fontSize: 16,
fontWeight: '900',
paddingTop: 10,
paddingBottom: 10,
},
buttonStyle: {
flex: 1,
backgroundColor: colors.bgColor,
borderRadius: 50
},
});
return (
<TouchableOpacity onPress={onPress} style={styles.buttonStyle}>
<Text style={styles.textStyle}>
{children}
</Text>
</TouchableOpacity>
);
};
export { Button };
エラーで:
undefined is not an object (evaluating 'colors.textColor')
なぜそれが機能しないのですか?また、スタイリングに使用するオブジェクトとして小道具を条件付きで渡すにはどうすればよいですか?