How to set default props values to custom state less component in React Native -
i have simple component, called divider
here source code:
import react "react"; import { stylesheet, view } "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", }, });
what trying achieve values in styles.separator
becomes default values of component, since values using in cases, in edge cases need change marginbottom
16
example.
so case want <divider />
, <divider marginbottom = 16 />
what have below, doesn't work.
import react "react"; import { stylesheet, view } "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}, }} /> ); } }
you can receive custom style props , use them in component style array. when call props style after component's, overwrite equal style property has.
for example, let's have component named 'card', can write component this:
<view style={[style.cardstyle, props.style]}> {props.children} </view>
and call <card style={{ backgroundcolor: '#ffffff'}} />
so it's getting defined 'cardstyle' it's own component, adding styles received props.
hope helps.
edit:
you can try this
import react "react"; import { stylesheet, view } "react-native"; const divider = (props) => { <view style = {{ height: stylesheet.hairlinewidth, margintop: {this.props.margintop}, marginbottom: {this.props.marginbottom}, backgroundcolor: {this.props.backgroundcolor}, }} /> } divider.defaultprops = { margintop: 0, marginbottom: 8, backgroundcolor: "#ffffff80", } export default divider;
let me know if works you.
Comments
Post a Comment