不能在 React JSX 值属性中重复十六进制 HTML 实体



所以我的问题是为什么这样做并显示点:

<Field label="Password" value="&#x2022;&#x2022;&#x2022;&#x2022;&#x2022;" type="password" />

上面只显示普通的六进制代码!

<Field label="Password" value={`${'&#x2022;'.repeat(10)}`} type="password" />

我的字段组件:

function renderValueByType(value: string, type: string) {
  switch (type) {
    case 'phone':
      return phoneFormatter(value);
    default:
      return value;
  }
}
/**
 * 
 * @param {*} param0 
 */
const Field = ({ label, value, type, className }: PropTypes) => (
  <div className={className}>
    <span className="Field__label">{label}</span>
    <span className="Field__content">{renderValueByType(value, type)}</span>
  </div>
);

如果您将静态字符串设置为道具,它将按原样呈现。

如果您将变量设置为道具,它将被清理。

这里最好的选择是将您的十六进制字符代码转换为字符串,然后再将其传递给您的组件(使用 String.fromCharCode()):

<Field
   label="Password"
   value={String.fromCharCode("0x2022").repeat(10)}
   type="password"
/>

最新更新