在JSX中渲染并传递/指定动态字段

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

Render and pass/specify dynamic field in JSX

问题

我在运行时创建了一个字段对象(通过API调用),如下所示:

field: {
  "firstNameField-0":
    {
      id: 'firstNameField',
      mandatory: true,
      value: 'First Name 1'
    }
  }

上面的索引(即0)是动态的,但是一次只能有一个值与索引匹配,我将这个值存储在一个名为“index”的状态变量中。所以,基本上字段名称的格式是firstNameField-${index}

我想将此字段对象/配置传递给我的JSX。因此,看起来像是这样的:

{
  field.firstNameField &&
    <MyField key={field.firstNameField.id} field={field.firstNameField} />
}

在JSX本身是否可以指定并呈现具有此动态索引的内容?

我尝试了类似下面的东西,但它抛出了语法错误:

<MyField key={field.[`firstNameField-${index}`].id} field={field.[`firstNameField-${index}`]}  />

更新

是否有一种更简洁的方式来编写这个JSX,因为它非常冗长和重复?

{
  myContactObj.gridItems &&
  myContactObj.gridItems[index] && 
  myContactObj.gridItems[index].fields[0] && 
  myContactObj.gridItems[index].fields[0][`firstNameField-${index}`] &&
  <MyField key={myContactObj.gridItems[index].fields[0][`firstNameField-${index}`].id} field={myContactObj.gridItems[index].fields[0][`firstNameField-${index}`]} index={index}  />
}
英文:

I have a field object created at runtime (via API call) as below

field: {
  "firstNameField-0":
    {
      id: 'firstNameField',
      mandatory: true,
      value: 'First Name 1'
    }
  }

The index (i.e. 0) above is dynamic, but at a time, only one value is possible for the index and I have this value in a state variable "index". So, basically the field names are of the format firstNameField-${index}

I want to pass this field object/configuration to my JSX. So, looking at something like

{
  field.firstNameField &&
    <MyField key={field.firstNameField.id} field={field.firstNameField} />
}

Is it possible to specify and render with this dynamic index in the JSX itself ?

I was trying something like below, but it is throwing syntax error;

<MyField key={field.[`firstNameField-${index}`].id} field={field.[`firstNameField-${index}`]}  />

UPDATED

Is there a shorthand way of writing this JSX, since it is quite verbose and repetitive ?

{
  myContactObj.gridItems &&
  myContactObj.gridItems[index] && 
  myContactObj.gridItems[index].fields[0] && 
  myContactObj.gridItems[index].fields[0][`firstNameField-${index}`] &&
  <MyField key={myContactObj.gridItems[index].fields[0][`firstNameField-${index}`].id} field={myContactObj.gridItems[index].fields[0][`firstNameField-${index}`]} index={index}  />
}

答案1

得分: 2

不要在括号表示法中使用点。

<MyField key={field[`firstNameField-${index}`].id} field={field[`firstNameField-${index}`]}  />

为了简化嵌套属性检查,您可以使用可选链接

myContactObj?.gridItems?.[index]?.fields?.[0]?.[`firstNameField-${index}`]
	&& <MyField ... />
英文:

Don't use dots in bracket notation.

<MyField key={field[`firstNameField-${index}`].id} field={field[`firstNameField-${index}`]}  />

To shorten nested property checks, you can use optional chaining.

myContactObj?.gridItems?.[index]?.fields?.[0]?.[`firstNameField-${index}`]
	&& <MyField ... />

答案2

得分: 1

只需在括号前删除点号:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const fieldValue = field[`firstNameField-${index}`];
<MyField key={fieldValue.id} field={fieldValue}  />

<!-- end snippet -->

至于第二个问题,你可以简化你的JSX代码,像这样:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const fieldValue = myContactObj.gridItems?.[index].fields?.[0]?.[`firstNameField-${index}`];

// ...

{
  fieldValue &&
    <MyField key={fieldValue.id} field={fieldValue} index={index}  />
}

<!-- end snippet -->
英文:

Just remove the dot before the brackets:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const fieldValue = field[`firstNameField-${index}`];
&lt;MyField key={fieldValue.id} field={fieldValue}  /&gt;

<!-- end snippet -->

About the 2nd question, you could simplify your JSX to something like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const fieldValue = myContactObj.gridItems?.[index].fields?.[0]?.[`firstNameField-${index}`];

// ...

{
  fieldValue &amp;&amp;
    &lt;MyField key={fieldValue.id} field={fieldValue} index={index}  /&gt;
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月28日 01:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348273.html
匿名

发表评论

匿名网友

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

确定