iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0
Software Development

.NET Core與React組合開發技系列 第 7

.NET Core與React組合開發技_第07天_child元件補上key

  • 分享至 

  • xImage
  •  

在此要來修正console報的錯誤
Warning: Each child in a list should have a unique "key" prop

https://ithelp.ithome.com.tw/upload/images/20220918/20107452E54pHxz4nZ.png

這是因為
React element 在render時候若不用 key
則React 就會報以下error message

~\ClientApp\src\components\Home.js
會有錯誤的程式碼

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import Rating from './Rating';

export class Home extends Component {
    static displayName = Home.name;

    constructor(props) {
        super(props);
        this.state = {
            products: [
                { title: "some product", description: "interesting", id: 1, averageRating: 3 },
                { title: "another product", description: "more", id: 2, averageRating: 4 }
            ]
        };
    }

    render() {
        return (
            <div className="row">
                {this.state.products.map(product =>
                    <div className="col-4">
                        <div className="card product-card">
                            <div className="card-body">
                                <h5 className="card-title">{product.title}</h5>
                                <p className="card-text">{product.description}</p>
                                <Link to={`/product/${product.id}`}>Detail</Link>
                                <p>rate:<Rating rating={product.averageRating} /></p>
                            </div>
                        </div>
                    </div>
                )
                }
            </div>
        );
    }

}

修正好的程式碼

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import Rating from './Rating';

export class Home extends Component {
    static displayName = Home.name;

    constructor(props) {
        super(props);
        this.state = {
            products: [
                { title: "some product", description: "interesting", id: 1, averageRating: 3 },
                { title: "another product", description: "more", id: 2, averageRating: 4 }
            ]
        };
    }

    render() {
        return (
            <div className="row">
                {this.state.products.map(product =>
                    <div key={product.id} className="col-4">
                        <div className="card product-card">
                            <div className="card-body">
                                <h5 className="card-title">{product.title}</h5>
                                <p className="card-text">{product.description}</p>
                                <Link to={`/product/${product.id}`}>Detail</Link>
                                <p>rate:<Rating rating={product.averageRating} /></p>
                            </div>
                        </div>
                    </div>
                )
                }
            </div>
        );
    }

}

https://ithelp.ithome.com.tw/upload/images/20220918/20107452pIU9fGMdRr.png


上一篇
.NET Core與React組合開發技_第06天_單一組件內部state_跨組件溝通props
下一篇
.NET Core與React組合開發技_第08天_Class元件和Function元件
系列文
.NET Core與React組合開發技30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言