繁体   English   中英

为什么 jsonData 不读取文件?

[英]Why isn't jsonData reading the file?

我有一个 json 文件:

[ {
  "symbol" : "SPY",
  "type" : "etf"
}, {
  "symbol" : "CMCSA",
  "type" : "stock"
}, {
  "symbol" : "KMI",
  "type" : "stock"
}, {
  "symbol" : "INTC",
  "type" : "stock"
}, {
  "symbol" : "MU",
  "type" : "stock"
},
...

我正在尝试将其读入表格:


const Home = () =>{
    const displayStockCodes = (info) =>{
        JsonData.map(
            (info)=>{
                return(
                    <tr>
                        <td>{info.symbol}</td>
                        <td>{info.type}</td>
                    </tr>
                )
            }
        );
    };
    
    return (
        <div>
            <table class="table table-striped">
                <thead>
                    <tr>
                    <th>Symbol</th>
                    <th>Type</th>
                    </tr>
                </thead>
                <tbody>                
                    {displayStockCodes}               
                </tbody>
            </table>
             
        </div>

    );
};

export default Home;

我试着按照指南去做,但最终页面上只显示了SymbolType ,数据本身并没有输出。 也许我需要添加其他东西?

  1. displayStockCodes是一个函数,但您没有在需要调用该函数的tbody中调用它。

  2. displayStockCodes也不返回任何你需要的东西来确保它返回一些JSX代码。

const Home = () =>{
    const displayStockCodes = (info) =>{
        // 2. you need to return 
        return JsonData.map(
            (info)=>{
                return(
                    <tr>
                        <td>{info.symbol}</td>
                        <td>{info.type}</td>
                    </tr>
                )
            }
        );
    };
    
    return (
        <div>
            <table className="table table-striped"> <!-- use className here instead of class -->
                <thead>
                    <tr>
                    <th>Symbol</th>
                    <th>Type</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- you need to call this -->     
                    {displayStockCodes()}               
                </tbody>
            </table>
             
        </div>

    );
};

export default Home;
const Home = () => {
    const JsonData = [
        {
            symbol: 'SPY',
            type: 'etf',
        },
        {
            symbol: 'CMCSA',
            type: 'stock',
        },
        {
            symbol: 'KMI',
            type: 'stock',
        },
        {
            symbol: 'INTC',
            type: 'stock',
        },
        {
            symbol: 'MU',
            type: 'stock',
        },
    ];

    const displayStockCodes = () => {
        return JsonData.map((info) => (
            <tr>
                <td>{info.symbol}</td>
                <td>{info.type}</td>
            </tr>
        ));
    };


    return (
        <div>
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th>Symbol</th>
                        <th>Type</th>
                    </tr>
                </thead>
                <tbody>{displayStockCodes()}</tbody>
            </table>
        </div>
    );
};

export default Home;

输出:

在此处输入图像描述

暂无
暂无

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

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