我有自定義組件,其中我映射屬性。 在這個組件中,我有標籤分配了CSS類。 但我也想要這個標籤可選的內聯樣式。 問題是,在反應中我需要用大括號包圍內聯樣式,我無法將它們轉義或在組件中正確設置它們。如何解決這個問題?作爲React組件中的屬性的Css內聯樣式
代碼從組件:
const CustomComponent = ({items, name}) => (
<fieldset>
{items
.map((item, index) => ({item, id: `${name || 'dod'}-${item.value || index}`}))
.map(({item, id}) =>
<div key={id}
className="className1">
<input
id={id}
name={name}
type="text"
/>
<label htmlFor={id} className="className" style={item.style}>
{item.label}
</label>
</div>
)}
</fieldset>
);
從渲染.jsx
<CustomComponent
name="name"
items={[{
value: 'value',
label: 'label',
style: {{display: 'inline'}} -> not working
}]}
/>
你能告訴我們更多的代碼? 'item.style'的類型是什麼?它必須是一個對象文字?如果你正在映射,使用'key'。 – Noctisdark