Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend some style object in react native

I want to make an base style and extend others by base.How to implement it in react native code.

tvSmallDefault : {fontSize : Dimens.tvDefaultSmall, color : Colors.black},
tvNormalDefault : {fontSize : Dimens.tvDefaultNormal, color : Colors.black},

Here i gave black color in both above styles. How to make an base style and use it in both, something like this.

base: {color : Colors.black},
tvSmallDefault parent base: {fontSize : Dimens.tvDefaultSmall},
tvNormalDefault parent base : {fontSize : Dimens.tvDefaultNormal},
like image 844
Khemraj Sharma Avatar asked Mar 07 '23 21:03

Khemraj Sharma


1 Answers

You could declare the base style as an object, then mix it in using the ... spread operator (or Object.assign if the spread operator isn't available)

const baseStyle = { color: Colors.black }

const tvSmallDefault = { ...baseStyle, fontSize: Dimens.tvDefaultSmall }
const tvNormalDefault = { ...baseStyle, fontSize: Dimens.tvDefaultNormal }

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

like image 152
kingdaro Avatar answered Mar 09 '23 10:03

kingdaro