简体   繁体   English

React 仅在数组映射上循环一次

[英]React loops over array map only once

I want to generate a table in react and I want to take the data from firebase and put it in a json and pass that to the main file我想在 react 中生成一个表,我想从 firebase 获取数据并将其放入 json 并将其传递给主文件

  var dummy = [
       {'name':'shoe','remark':'remark','price':'price','photo':'photo','amount':'350',},
   
   ]
   var ref = db.ref("users/random/cart");
   ref.once("value")
   .then( function(snapshot) {
     var snapshot_value = snapshot.val();
   var this_is_an_example = {'name':'shoe','remark':'remark','price':'price','photo':'photo','amount':'350'};
       dummy.push(this_is_an_example)
       ref.off();
   
   });
   export default dummy;

In the main file if I console.log() I get在主文件中,如果我 console.log() 我得到在此处输入图像描述

Now this is the map :现在这是地图:

dummy.map((data) => {
console.log('data here', data)

在此处输入图像描述 }) })

[ [在此处输入图像描述 ] ]

It iterates once then throws this error I do not think that the table error is the reason why it iterates only once because the error is thrown even when I only have one element in array它迭代一次然后抛出这个错误我不认为表错误是它只迭代一次的原因,因为即使我在数组中只有一个元素也会抛出错误

Here is the full code for the table这是表格的完整代码

              <ClientTable>
               <thead>
               <ClientTr>
                   <ClientTh>Item Name</ClientTh>
                   <ClientTh>Remark</ClientTh>
                   <ClientTh>Price</ClientTh>
                   <ClientTh>Quantity</ClientTh>
                   <ClientTh>Amount</ClientTh>
                   <ClientTh>Edit</ClientTh>
               </ClientTr>
               </thead>
                   <tbody>
                   {dummy.map((data, key) => {
                       console.log('one', data)
                       return (
                           
                           <ClientTr key={'key'}>
                               <ClientTd>
                                   <Wrapper>
   
                                       <ProductWrapper>
                                           <ImageWrapper src={Product} alt="product" />
                                       </ProductWrapper>
                                       {data.name}
                                   </Wrapper>
                               </ClientTd>
                               <ClientTd>
                                   {data.remark}
                               </ClientTd>
                               <ClientTd>
                                   {data.price}
                               </ClientTd>
                               <ClientTd>
                                   <NumberBox />
                               </ClientTd>
                               <ClientTd>
                                   {data.amount}
                               </ClientTd>
                               <ClientTd>
                                   Delete
                               </ClientTd>
                           </ClientTr>
   
                       );
   
                   })}
                   </tbody>
               </ClientTable>

See ClientTr is the Direct child of ClientTable, and the issue says tr can't be the direct child of the table, tr can appear as child of thead, tbody or tfoot.请参阅 ClientTr 是 ClientTable 的直接子级,问题说 tr 不能是表的直接子级,tr 可以作为 thead、tbody 或 tfoot 的子级出现。 So your ClientTr contains headers, just put it inside the thead and the issue will be resolved.因此,您的 ClientTr 包含标题,只需将其放在 thead 中即可解决问题。

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

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