inheritance - Inheriting defaultProps from superclass in React -
i think props
, (for example, theme), universal among components makes sense extract handling (to superclass). follows default value belongs there.
however, idionatic way achieve in react?
class base extends react.component { bgcolor() { switch (this.props.theme) { case 'summer': return 'yellow'; break; case 'autumn': return 'grey'; break; case 'winter': return 'white'; break; } } } base.defaultprops = { theme: 'autumn' };
class sky extends base { render() { return <div style={{backgroundcolor: this.bgcolor()}}>{this.props.clouds}</div>; } } sky.defaultprops = { clouds: [] };
...defaultprops
class property (as opposed instance), , there no inheritance.
by assigning sky.defaultprops
hide base ones. if want combine them, you'd need explicitly that, e.g.
sky.defaultprops = object.assign({}, base.defaultprops, { clouds: [] });
Comments
Post a Comment