我有一個非常簡單的組件,叫做Divider
這裏是源代碼:如何設置默認道具值自定義狀態,少部分在陣營本地
import React from "react";
import { StyleSheet, View } from "react-native";
export default class Divider extends React.Component {
render() {
return (
<View style = { styles.separator } />
);
}
}
const styles = StyleSheet.create({
separator: {
height: StyleSheet.hairlineWidth,
marginBottom: 8,
backgroundColor: "#FFFFFF80",
},
});
我想實現的是,在值styles.separator
成爲此組件的默認值,因爲這些是我在大多數情況下使用的值,但在某些邊緣情況下,我需要將marginBottom
更改爲16
。
所以大多數情況下,我只想做<Divider />
,但有時<Divider marginBottom = 16 />
我有什麼目前是這樣的下面,但顯然這是行不通的。
import React from "react";
import { StyleSheet, View } from "react-native";
export default class Divider extends React.Component {
static defaultPropts = {
marginTop: 0,
marginBottom: 8,
backgroundColor: "#FFFFFF80",
}
render() {
return (
<View style = {{
height: StyleSheet.hairlineWidth,
marginTop: {this.props.marginTop},
marginBottom: {this.props.marginBottom},
backgroundColor: {this.props.backgroundColor},
}} />
);
}
}
嗨@Mohadmed Khalil,不幸的是這不適合我。 :/ – Silex