【问题标题】:What is the correct react syntax for appending to spread attributes附加到传播属性的正确反应语法是什么
【发布时间】:2020-01-23 03:28:40
【问题描述】:

我正在尝试将 "{ "text" :" 添加到字符串的开头,并将 "}" 添加到字符串的末尾。

我一直在四处寻找和玩下面的不同版本

{'{ "text" : "'+{...propName.props}+'" }'}

甚至可以附加到扩展属性吗?

为我想要实现的目标添加更多信息

一个空的propsName.props 传递给一个<input type="text"/>

<input
      type="text"
      className="SCSSstuff"
      disabled={loading}
      placeholder="Demo Text"
      autoComplete="off"
      ref="someRef"
      tabIndex={3}
      {...propsName.props}  //<---- 'User Text' gets defined here
      />

我需要将其传递给隐藏的输入

<input
      type="hidden"
      disabled={loading}
      {...propsName.props} //<-- I need this output to be { "text" : "User Text" }
      />

【问题讨论】:

  • 问题不太清楚,但你的 propsName.props 包含什么?
  • 你应该使用 Array#reduce,将你的数组缩减为一个字符串
  • @saurssaurav propName.props 将包含来自文本输入的用户输入值
  • 如果是文本输入,那么为什么要使用扩展运算符。您可以使用模板文字(`)。在您的情况下:{text: ${propName.props}}。您可以添加更多代码和描述,以便其他人能够理解和帮助您。
  • 添加了一些代码,希望它能阐明我想要做什么

标签: reactjs


【解决方案1】:

如果你的道具包含用户输入的值,那么它应该是一些原始类型。

您可以使用模板字符串来实现所需的结果。

const propName = {
    props: 'hey'
}
const templateString = `{ text: ${ propName.props } }`
console.log(templateString) // { text: hey }

希望对你有帮助!!!

【讨论】:

  • 你的回答让我非常接近!
  • 如果我们知道输入和预期输出,我们可以增加更多的价值。
  • 更新了我的问题,希望它能澄清事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2019-07-31
  • 2016-11-15
  • 1970-01-01
  • 2023-02-13
相关资源
最近更新 更多