Home > database >  How to change font base on props and set default if there is no prop
How to change font base on props and set default if there is no prop

Time:11-05

The question is pretty simple, i have a text, which have a default prop, fontSize, problem is i want to have props to change that size when needed, so i set default font and a props to change font, but i really don't know what is the right way to do this.

Here is my did'nt work solution:

The props called textStyle, i define by using typescript interface textStyle: TextStyle

I tried to have conditions in style like

style={textListStyle?.fontSize ?? 15}   <== not work

The complier complain

No overload matches this call.
  Overload 1 of 2, '(props: TextProps | Readonly<TextProps>): Text', gave the following error.
    Type 'number' is not assignable to type 'StyleProp<TextStyle>'.
  Overload 2 of 2, '(props: TextProps, context: any): Text', gave the following

error.

And i tried

style={!textListStyle?.fontSize ? 15 : textListStyle?.fontSize}

Same issue, so what is the best solution? Thank you so much

CodePudding user response:

Not sure to really understand what you are trying to do. For What I understood, you have a prop and you try to define a certain fontSize when prop is true and another one when it's not.

I would do :

style={{fontSize: textListStyle.fontSize ? 15 : 20 }}

Or if your "textListStyle?.fontSize" is an integer:

style={{fontSize: textListStyle?.fontSize ?? 15}}
  • Related