简体   繁体   English

如何将 html 标签数组添加到 jsx

[英]how can i add an array of html tags to jsx

I am new in react and I have an issue that I didn't find any solution for after search.我是新来的反应,我有一个问题,我在搜索后没有找到任何解决方案。

When I store the array of html tags in variable I get minified React error #31当我将 html 标签数组存储在变量中时,我得到minified React error #31

var k = [1,2,3];
var x = k.map(e=><li>{e}</li>);
var e = (
  <div>         
    <ul>
      {1 && {x}}
    <ul>
  </div>
);
var divTag = document.getElementById("k");
ReactDOM.render(e,divTag)

But when I write this I get no errors.但是当我写这篇文章时,我没有得到任何错误。

var k = [1,2,3];
var x = k.map(e=><li>{e}</li>);
var e = (
  <div>         
    <ul>
      {1 && k.map(e=><li>{e}</li>)}
    <ul>
  </div>
);
var divTag = document.getElementById("k");
ReactDOM.render(e,divTag)

You need to remove the curly braces around the x variable {1 && {x}} -> {1 && x}您需要删除x变量{1 && {x}} -> {1 && x}周围的花括号

You are defining a new object containing the key x instead of just passing the array to react您正在定义一个包含键x的新 object 而不是仅传递数组以做出反应

The solution解决方案

Replace {1 && {x}} with {1 && x}{1 && {x}}替换为{1 && x}

Because you've wrapped x into curly braces, you've got the rendering error (you wanted to provide the array).因为您已将x包裹在花括号中,所以您遇到了渲染错误(您想提供数组)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM