javascript - 无法在 react jsx 值 Prop 中重复十六进制 html 实体

标签 javascript html reactjs jsx flowtype

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

<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>
);

最佳答案

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

如果您将变量设置为 prop,它将被清除。

最好的办法是在将十六进制字符代码传递给组件之前将其转换为字符串(使用 String.fromCharCode()):

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

关于javascript - 无法在 react jsx 值 Prop 中重复十六进制 html 实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46727543/

相关文章:

javascript - 使用 MobX 状态时 Antd 模式无法打开/关闭

javascript - 使用 debounce js 时的数据一致性

javascript - 间隔不断变化?

javascript - 无法使用 mongoose 连接到 mongoDB

javascript - 测试嵌套 JavaScript 对象键是否存在

html - 根据附加的 css 类更改占位符的颜色

javascript - Google Polymer - 如何向 Neon 元素添加内容

java - 如何嵌入Java小程序?

php - 将 *.htm 重定向到 *.php

angularjs - 如何使 componentWillUnmount 工作?