gpt4 book ai didi

Reactjs:在功能组件中实现 dangerouslySetInnerHTML

转载 作者:行者123 更新时间:2023-12-05 03:59:35 25 4
gpt4 key购买 nike

我一直在尝试弄清楚如何将 dangerouslySetInnerHTML 应用于此组件内部的 label={item.description}:

const SwitchList = ({
color,
disabled,
sortKey,
sortDirection = SORT_DIRECTION.ASC,
items,
data,
children,
onChange
}) => {
if (items.length === 0) {
return React.Children.only(children);
}
let sortProp = null;
let sortedItems = items;
if (sortKey) {
sortProp = new SortProperty(sortKey, sortDirection === SORT_DIRECTION.ASC);
sortedItems = items.sort(genericObjectSort(sortProp));
}
return (
<div>
{sortedItems.map(item => {
return (
<Switch
checked={data[item.key]}
label={item.description}
onChange={onChange(item.key)}
key={item.key}
color={color}
disabled={
typeof disabled === "function" ? disabled(item) : disabled
}
className={styles.customSwitch}
/>
);
})}
</div>
);
};
SwitchList.propTypes = {
sortKey: PropTypes.string,
sortDirection: PropTypes.number,
items: PropTypes.arrayOf(PropTypes.object),
data: PropTypes.object,
children: PropTypes.node,
color: PropTypes.string,
disabled: PropTypes.oneOfType([PropTypes.bool, PropTypes.func]),
onChange: PropTypes.func
};

export default SwitchList;

辅助函数将无济于事,因为这不是基于类的组件。

如何获取 {items.description} 并将 dangerouslySetInnerHTML 应用到功能组件中?

最佳答案

试试这个:

<Switch
checked={data[item.key]}
label={<span dangerouslySetInnerHTML={{__html:item.description}}></span>}
onChange={onChange(item.key)}
key={item.key}
color={color}
disabled={
typeof disabled === "function" ? disabled(item) : disabled
}
className={styles.customSwitch}
/>

或者

label= {<span className="content" dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(item.description) }}></span>}

关于Reactjs:在功能组件中实现 dangerouslySetInnerHTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57032676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com