这个React条件渲染表达式有什么问题?

huangapple go评论49阅读模式
英文:

What is wrong with this React conditional rendering expression?

问题

我不想在contact.phone字段为空字符串时显示它。

我尝试了以下条件渲染的方式:

<Select
    label="Send To"
    labelId="send-to-label"
    required
    error={state.submitted && !state.sendTo}
    fullWidth
    placeholder="Select contact"
    id="send-to"
    value={state.sendTo ? state.sendTo.contactId : ''}
    onChange={selectSendToChanged}
    MenuProps={{ classes: { paper: 'contactMenuList' } }}
>
    {state.sendToContacts.map((contact: Contact) => (
        <MenuItem key={contact.contactId} value={contact.contactId}>
            {contact.contactId !== 'other' 
                ? `${contact.name} - ${contact.phone}`
                : contact.name
            }
        </MenuItem>
    ))}
</Select>

但它在contact.phone字段上给出了"The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type."错误。我还尝试了下面的代码(与上面相同,但没有花括号),但它将整个JS行转储为UI上的字符串。

<Select
    label="Send To"
    labelId="send-to-label"
    required
    error={state.submitted && !state.sendTo}
    fullWidth
    placeholder="Select contact"
    id="send-to"
    value={state.sendTo ? state.sendTo.contactId : ''}
    onChange={selectSendToChanged}
    MenuProps={{ classes: { paper: 'contactMenuList' } }}
>
    {state.sendToContacts.map((contact: Contact) => (
        <MenuItem key={contact.contactId} value={contact.contactId}>
            {contact.contactId !== 'other' 
                ? `${contact.name} - ${contact.phone}`
                : contact.name
            }
        </MenuItem>
    ))}
</Select>

(请注意,我已经将您的代码示例中的&lt;&gt;转换为了正常的HTML标签,以便更容易阅读。)

英文:

I had the below Reactjs snippet in our UI, which works fine now.

&lt;Select
                    label=&quot;Send To&quot;
                    labelId=&quot;send-to-label&quot;
                    required
                    error={state.submitted &amp;&amp; !state.sendTo}
                    fullWidth
                    placeholder=&quot;Select contact&quot;
                    id=&quot;send-to&quot;
                    value={state.sendTo ? state.sendTo.contactId : &#39;&#39;}
                    onChange={selectSendToChanged}
                    MenuProps={{ classes: { paper: &#39;contactMenuList&#39; } }}
                &gt;
                    {state.sendToContacts.map((contact: Contact) =&gt; (
                        &lt;MenuItem key={contact.contactId} value={contact.contactId}&gt;
                            {contact.name} - {contact.phone}
                        &lt;/MenuItem&gt;
                    ))}
                &lt;/Select&gt;

I don't want the contact.phone field to be shown when it is an empty string.

I tried implementing condition rendering like this:

&lt;Select
                    label=&quot;Send To&quot;
                    labelId=&quot;send-to-label&quot;
                    required
                    error={state.submitted &amp;&amp; !state.sendTo}
                    fullWidth
                    placeholder=&quot;Select contact&quot;
                    id=&quot;send-to&quot;
                    value={state.sendTo ? state.sendTo.contactId : &#39;&#39;}
                    onChange={selectSendToChanged}
                    MenuProps={{ classes: { paper: &#39;contactMenuList&#39; } }}
                &gt;
                    {state.sendToContacts.map((contact: Contact) =&gt; (
                        &lt;MenuItem key={contact.contactId} value={contact.contactId}&gt;
                            {contact.contactId != &#39;other&#39; 
								? {contact.name} - {contact.phone}
								: {contact.name}
							}
                        &lt;/MenuItem&gt;
                    ))}
                &lt;/Select&gt;

but it gives "The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type." error on contact.phone field. I also tried the below snippet (the same as above but without curly brackets) but it dumps the entire JS line as a string on the UI.

&lt;Select
                    label=&quot;Send To&quot;
                    labelId=&quot;send-to-label&quot;
                    required
                    error={state.submitted &amp;&amp; !state.sendTo}
                    fullWidth
                    placeholder=&quot;Select contact&quot;
                    id=&quot;send-to&quot;
                    value={state.sendTo ? state.sendTo.contactId : &#39;&#39;}
                    onChange={selectSendToChanged}
                    MenuProps={{ classes: { paper: &#39;contactMenuList&#39; } }}
                &gt;
                    {state.sendToContacts.map((contact: Contact) =&gt; (
                        &lt;MenuItem key={contact.contactId} value={contact.contactId}&gt;
                            contact.contactId != &#39;other&#39; 
								? {contact.name} - {contact.phone}
								: {contact.name}
                        &lt;/MenuItem&gt;
                    ))}
                &lt;/Select&gt;

答案1

得分: 2

MenuItem 在您的条件渲染块中的语法看起来不正确。您可能希望将其更改为以下内容:

&lt;MenuItem key={contact.contactId} value={contact.contactId}&gt;
  {contact.contactId != &#39;other&#39; 
    ? `${contact.name} - ${contact.phone}`
    : contact.name}
&lt;/MenuItem&gt;

这是因为花括号内的任何内容都被解释为JS表达式,所以您的条件渲染版本等同于,例如,&quot;John&quot; - &quot;&quot;&quot;John&quot; - &quot;555-555-5555&quot;。将要呈现的字符串包装在模板字符串中可以避免将其尝试评估为表达式。

英文:

The syntax for MenuItem in your conditional rendering block looks incorrect. You might want to change it to the following:

&lt;MenuItem key={contact.contactId} value={contact.contactId}&gt;
  {contact.contactId != &#39;other&#39; 
    ? `${contact.name} - ${contact.phone}`
    : contact.name}
&lt;/MenuItem&gt;

This is because anything inside curly braces is interpreted as a JS expression, so your conditional rendering version was equivalent to, for example, &quot;John&quot; - &quot;&quot; or &quot;John&quot; - &quot;555-555-5555&quot;. Wrapping the string to be rendered in a template string avoids trying to evaluate this as an expression.

huangapple
  • 本文由 发表于 2023年6月19日 10:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定