Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
);
like image 779
JSK Avatar asked Oct 13 '17 10:10

JSK


1 Answers

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"
/>
like image 129
klugjo Avatar answered Oct 22 '22 01:10

klugjo