【问题标题】:Is it possible to conditionally render only the placeholder attribute in a input tag in React?是否可以有条件地仅在 React 的输入标签中呈现占位符属性?
【发布时间】:2021-02-13 07:05:54
【问题描述】:

我想做如下的事情:

        return (
          <input  
            {(tagsArrayEmpty) ? placeholder='enter your tags' : placeholder='empty'}
            type="text"
          />
        )

但我收到错误:解析错误:意外的令牌,预期的“...”

是否可以有条件地只渲染输入标签的占位符部分而不是完整的输入标签?我阅读了conditional rendering here 的文档,但所有示例至少只呈现完整标签。只是想知道有没有办法。

【问题讨论】:

    标签: reactjs conditional-rendering


    【解决方案1】:

    当然,像这样:

           return (
              <input  
                placeholder={tagsArrayEmpty ? 'enter your tags' : 'empty'}
                type="text"
              />
            )
    

    【讨论】:

    • 如果我想在 '' 这样的占位符中使用来自 fontawesome 的搜索图标怎么办?
    • 这可能值得自己提出问题
    猜你喜欢
    • 2014-09-22
    • 2012-05-07
    • 1970-01-01
    • 2021-02-28
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    相关资源
    最近更新 更多