問題很簡單,我有一個文本,它有一個默認的道具,fontSize問題是我想要道具在需要時改變那個大小,所以我設定了默認字體和一個道具來改變字體,但我真的沒有知道這樣做的正確方法是什么。
這是我沒有用的解決方案:
被呼叫的道具textStyle,我使用 typescript 介面定義textStyle: TextStyle
我想有條件的style像
style={textListStyle?.fontSize ?? 15} <== not work
編譯器抱怨
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
錯誤。
我試過
style={!textListStyle?.fontSize ? 15 : textListStyle?.fontSize}
同樣的問題,那么最好的解決方案是什么?非常感謝
uj5u.com熱心網友回復:
不確定是否真正了解您要嘗試做什么。根據我的理解,你有一個道具,當 prop 為真時,你嘗試定義一個特定的 fontSize,當它不是時定義另一個。
我會做 :
style={{fontSize: textListStyle.fontSize ? 15 : 20 }}
或者如果你的“textListStyle?.fontSize”是一個整數:
style={{fontSize: textListStyle?.fontSize ?? 15}}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/348738.html
標籤:javascript 打字稿 反应原生
上一篇:Onclick按鈕,狀態不會改變
