使用lodash渲染嵌套属性的表格。

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

rendering nested properties with a table using lodash

问题

以下是您要翻译的内容:

"I'm trying to display my table head and table body as separate components and dynamically too, with some nested properties (like genre). I use lodash but I get an error with the tBody rendering. I won't mind another means to achieve my goal"

"data for the table body below"

export const data= [
  {
      _id: "5b21ca3eeb7f6fbccd471815",
      title: "Terminator",
      genre: [{_id: "5b21ca3eeb7f6fbccd471818", name: "Action"}],
      numberInStock: 6,
      dailyRentalRate: 2.5,
      publishDate: "2018-01-03T19:04:28.809z"
      
  },
  {
      _id: "5b21ca3eeb7f6fbccd471819",
      title: "Trip to Italy",
      genre: [{_id: "5b21ca3eeb7f6fbccd471814", name: "Comedy"}],
      numberInStock: 7,
      dailyRentalRate: 3.5,
      publishDate: "2018-01-03T19:04:28.809z"
  },
  
  {
      _id: "5b21ca3eeb7f6fbccd47181b",
      title: "Wedding Crashers",
      genre: [{_id: "5b21ca3eeb7f6fbccd471814", name: "Comedy"}],
      numberInStock: 7,
      dailyRentalRate: 3.5,
      publishDate: "2018-01-03T19:04:28.809z"
  }]

"here's columns prop from my parent component to reduce adding much code"

columns = [
    {path: "title", label: "Title"},
    {path: "genre", label: "Genre"},
    {path: "numberInStock", label: "Stock"},
    {path: "dailyRentalRate", label: "Rate"},
    {
        key: "like",
        content: movie => { <Liked liked={movie.liked} onClick={() => this.props.onLike(movie)} /> }
    },
    {
        key: "delete",
        content: movie =>
        {
            <button
                onClick={() => this.props.onDelete(movie)}
                className="btn btn-danger btn-sm">Delete
            </button>;
        }
    }
]
import React, { Component } from 'react';
import _ from 'lodash';
class TableBody extends Component {
  renderCell = (item, column) => {
    if (column.content) return column.content(item);

    return _.get(item, column.path);
  };

  render() {
    const { data, columns } = this.props;

    return (
      <>
        <tbody>
          {data.map((item) => (
            <tr key={item._id}>
              {columns.map((column) => (
                <td key={item._id + (column.path || column.key)}>
                  {this.renderCell(item, column)}
                </td>
              ))}
            </tr>
          ))}
        </tbody>
      </>
    );
  }
}

export default TableBody;

当我尝试运行我的代码时,我收到以下错误信息:

"Uncaught Error: Objects are not valid as a React child (found: object with keys {_id, name}). If you meant to render a collection of children, use an array instead."

英文:

I'm trying to display my table head and table body as separate components and dynamically too, with some nested properties (like genre). I use lodash but I get an error with the tBody rendering. I won't mind another means to achieve my goal

data for the table body below


  export const data= [
    {
        _id: &quot;5b21ca3eeb7f6fbccd471815&quot;,
        title: &quot;Terminator&quot;,
        genre: [{_id: &quot;5b21ca3eeb7f6fbccd471818&quot;, name: &quot;Action&quot;}],
        numberInStock: 6,
        dailyRentalRate: 2.5,
        publishDate: &quot;2018-01-03T19:04:28.809z&quot;
        
    },
    {
        _id: &quot;5b21ca3eeb7f6fbccd471819&quot;,
        title: &quot;Trip to Italy&quot;,
        genre: [{_id: &quot;5b21ca3eeb7f6fbccd471814&quot;, name: &quot;Comedy&quot;}],
        numberInStock: 7,
        dailyRentalRate: 3.5,
        publishDate: &quot;2018-01-03T19:04:28.809z&quot;
    },
    
    {
        _id: &quot;5b21ca3eeb7f6fbccd47181b&quot;,
        title: &quot;Wedding Crashers&quot;,
        genre: [{_id: &quot;5b21ca3eeb7f6fbccd471814&quot;, name: &quot;Comedy&quot;}],
        numberInStock: 7,
        dailyRentalRate: 3.5,
        publishDate: &quot;2018-01-03T19:04:28.809z&quot;
    }]

here's columns prop from my parent componentto reduce adding much code

  columns = [
        {path: &quot;title&quot;, label: &quot;Title&quot;},
        {path: &quot;genre&quot;, label: &quot;Genre&quot;},
        {path: &quot;numberInStock&quot;, label: &quot;Stock&quot;},
        {path: &quot;dailyRentalRate&quot;, label: &quot;Rate&quot;},
        {
            key: &quot;like&quot;,
            content: movie =&gt; { &lt;Liked liked={movie.liked} onClick={() =&gt; this.props.onLike(movie)} /&gt; }
        },
        {
            key: &quot;delete&quot;,
            content: movie =&gt;
            {
                &lt;button
                    onClick={() =&gt; this.props.onDelete(movie)}
                    className=&quot;btn btn-danger btn-sm&quot;&gt;Delete
                &lt;/button&gt;
            }
        }
    ]
     
    import React, { Component } from &#39;react&#39;;
    import _ from &#39;lodash&#39;;
    class TableBody extends Component {
      renderCell = (item, column) =&gt; {
        if (column.content) return column.content(item);
    
        return _.get(item, column.path);
      };
    
      render() {
        const { data, columns } = this.props;
    
        return (
          &lt;&gt;
            &lt;tbody&gt;
              {data.map((item) =&gt; (
                &lt;tr key={item._id}&gt;
                  {columns.map((column) =&gt; (
                    &lt;td key={item._id + (column.path || column.key)}&gt;
                      {this.renderCell(item, column)}
                    &lt;/td&gt;
                  ))}
                &lt;/tr&gt;
              ))}
            &lt;/tbody&gt;
          &lt;/&gt;
        );
      }
    }
    
    export default TableBody;

I get this error when I try my code

Uncaught Error: Objects are not valid as a React child (found: object with keys {_id, name}). If you meant to render a collection of children, use an array instead.

答案1

得分: 0

问题在于你的数据中的genre是一个对象,这导致了这个错误。

如果你像这样做,它会修复这个问题:

 renderCell = (item, column) =&gt; {
    if (column.content) return column.content(item);

    if(column.path === &#39;genre&#39;) {
      return item.genre[0].name
    }
    return _.get(item, column.path);
  };
英文:

The problem is that genre in your data is an object, which is causing this error.

If you do something like this, it will fix the issue

 renderCell = (item, column) =&gt; {
    if (column.content) return column.content(item);

    if(column.path === &#39;genre&#39;) {
      return item.genre[0].name
    }
    return _.get(item, column.path);
  };

huangapple
  • 本文由 发表于 2023年5月17日 17:33:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270603.html
匿名

发表评论

匿名网友

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

确定