React本身不支持在行內style裏直接寫成fontSize: `${fontSize}px !important`,具體原因不清楚
解決辦法如下代碼所示,使用ref的方式去處理該h1虛擬元素的樣式(請注意,node.style.setProperty裏的第一個參數,必須寫成css的形式,而不是JSX的形式):
<h1
className="contentTitle"
style={{
textAlign: titleCenter ? 'center' : 'left',
}}
ref={(node) => {
if (node) {
node.style.setProperty('font-size', `${fontSize}px`,'important');
}
}}
>
{title}
</h1>