Ad

How To Get Rid Of The This React's "unique Key" Error

- 1 answer

React newbie alert!!

I'm getting this error: Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `resultTable`. See https://fb.me/react-warning-keys for more information. As suggested in https://fb.me/react-warning-keys, I have added keys to parent elements, but I may be overlooking something. Please suggest what I'm doing wrong

        var ResultTable = React.createClass({
            displayName: 'resultTable',
            propTypes: {
                table: React.PropTypes.object.isRequired
            },
            getDefaultProps: function() {
                return {
                    table: {
                        rows: [],
                        cols: [],
                    }
                };
            },
            getInitialState: function() {
                return {
                    table: {
                        rows: this.props.table.rows,
                        cols: this.props.table.cols,
                    }
                };
            },
            handleChange: function(event) {
                console.log('data changed');
                this.setState({
                    table: event.target.value
                });
            },
            getValue: function(bug, property) {
                //console.log('property', property);
                try {
                    property = property.split('.');
                    if (property.length === 3) {
                        return bug[property[0]][property[1]][property[2]];
                    }
                    if (property.length === 2) {
                        if (property[1] === 'tickets') {
                            return bug[property[0]][property[1]].join(',');
                        } else {
                            return bug[property[0]][property[1]];
                        }
                    }
                    if (property.length === 1) {
                        if (/(updatedAt|createdAt|fixedAt|shippedAt|closedAt)/.test(property)) {
                            // return $filter('date')(bug[property[0]], 'shortDate');
                        } else if (property[0] === 'clones') {
                            return bug.clones.join(', ');
                        } else {
                            return bug[property[0]];
                        }

                    }
                } catch (e) {
                    return '';
                }
            },
            order: function(event) {
                // event.preventDefault();
                var hash = event.target.attributes.value.value + '_';
                if (event.target.attributes['data-reverse'].value === 'true') {
                    hash += 'desc';
                    angular.element('a#' + event.currentTarget.attributes.id.value).attr('data-reverse', 'false');
                } else {
                    hash += 'asc';
                    angular.element('a#' + event.currentTarget.attributes.id.value).attr('data-reverse', 'true');
                }
                window.location.hash = hash;
                //this.setState({table: {rows: this.props.table.rows, cols:this.props.table.cols}});
            },
            render: function() {

                 var that = this;
                 var columns = this.props.table.cols;
                 var rows = this.props.table.rows;
                //console.log(this.props.table.cols);
                var selectedColumns = _.filter(columns, 'selected', true);

                var cols = selectedColumns.map(function(col, i) {
                    return React.DOM.th({
                        key: 'col-' + i,
                        className: col.cssClass,
                    }, React.DOM.a({
                        key: 'a-' + i,
                        id: 'a-' + i,
                        href: '#',
                        value: col.value,
                        'data-reverse': 'false',
                        onClick: that.order
                    }, col.name));
                });
                var header = React.DOM.thead(null, React.DOM.tr({
                    key: 'header'
                }, cols));

                var body = React.DOM.tbody(null, rows.map(function(bug, i) {
                    return React.DOM.tr({
                            key: bug.id
                        },
                        selectedColumns.map(function(column, j) {
                            return React.DOM.td({
                                key: j
                            }, that.getValue(bug, column.value));
                        }));

                }));

                return React.DOM.table({
                    key: 'table-body',
                    className: 'table table-striped table-condensed pull-left resultTable'
                }, [header, body]);
            }
        });
Ad

Answer

My guess is the problem is with the last line:

return React.DOM.table({
    key: 'table-body',
    className: 'table table-striped table-condensed pull-left resultTable'
}, [header, body]);

You're passing the array [header, body] as the children, and items in arrays need to have keys; however, neither header nor body has a key property.

However, you shouldn't add keys to fix this; instead, simply pass the two elements as individual arguments instead of an array:

return React.DOM.table({
    key: 'table-body',
    className: 'table table-striped table-condensed pull-left resultTable'
}, header, body);
Ad
source: stackoverflow.com
Ad