简体   繁体   中英

can't repeat an hexadecimal html entity in react jsx value props

So my question is why does this work and displays dots :

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

And the above just displays the plain hexa code !

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

My Field component :

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

If you set a static string as a prop it will be rendered as is.

If you set a variable as a prop it will be sanitized.

Your best bet here is convert your hex char code to string before passing it down to your component (using String.fromCharCode() ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM