React数组映射到列表失败,没有错误警告。

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

React array map to list fails without error warning

问题

I have searched through all answers on Stack Overflow for a solution to my problem. I am just stuck because there isn't any error message in the console. I believe there is a bug which I cannot identify. Can you assist further?

我已经在Stack Overflow上搜索了所有答案,寻找解决我的问题的方法。我卡住了,因为控制台中没有任何错误消息。我相信有一个我无法识别的错误。你能提供进一步的帮助吗?

I use jQuery because I am more familiar with it and it's ease of accomplishing the same goal. Any alternative, ES6 will also be OK so far I can move forward.

我使用jQuery,因为我更熟悉它,并且它更容易实现相同的目标。任何替代方法,如ES6,只要我能继续前进,都可以。

Below is the code.

以下是代码。

英文:

I have searched through all answers on Stack Overflow for a solution to my problem. I am just stuck because there isn't any error message in the console. I believe there is a bug which I cannot identify. Can you assist further?

I use jQuery because I am more familiar with it and it's ease of accomplishing the same goal. Any alternative, ES6 will also be OK so far I can move forward.

Below is the code.

All console.logs are OK, but the list comes out empty.

import { useState, useEffect } from 'react';
import $ from 'jquery';

const PerformanceRecord = (props) => {
    const [performanceRecord, setPerformanceRecord] = useState([]);

    useEffect(() => {
        try {
            $.post('php/study_room/get_performance_record.php', () => {
            }).done((data) => {
                console.log(data); //This works fine
                setPerformanceRecord(JSON.parse(data));
                console.log(JSON.parse(data)); //This works fine
            }).fail((xhr) => {
                alert(`An error ${xhr.statusText} has occurred`);
            });
        } catch (error) {
              alert(`An error ${error} has occurred. Please try later`);
        };
    }, []);

    // performanceRecord is an array of records. Each record is also an array of elements
    const records = performanceRecord.map((record, index) => {
        <li key={index}>
            <span key={0}>{record[0]}</span>
            <span key={1}>{record[1]}</span>
            <span key={2}>{record[2]}</span>
            <span key={3}>{record[3]}</span>
            <span key={4}>{record[4]}</span>
        </li>
    });

    const printHandlder = () => console.log('Use your browser print');

    return (
        <div id="record">
            <h3>User Record</h3>
            <ol>
                {records}
            </ol>
        </div>
    )
};

export default PerformanceRecord;

答案1

得分: 1

地图函数应该返回您想要渲染的JSX元素。没有return语句,映射操作将不会产生期望的结果。

const records = performanceRecord.map((record, index) => {
  return (
    <li key={index}>
      <span key={0}>{record[0]}</span>
      <span key={1}>{record[1]}</span>
      <span key={2}>{record[2]}</span>
      <span key={3}>{record[3]}</span>
      <span key={4}>{record[4]}</span>
    </li>
  );
});
英文:

The map function should return the JSX elements that you want to render. Without the return statement, the mapping operation won't produce the desired result.

const records = performanceRecord.map((record, index) =&gt; {
   return (
    &lt;li key={index}&gt;
      &lt;span key={0}&gt;{record[0]}&lt;/span&gt;
      &lt;span key={1}&gt;{record[1]}&lt;/span&gt;
      &lt;span key={2}&gt;{record[2]}&lt;/span&gt;
      &lt;span key={3}&gt;{record[3]}&lt;/span&gt;
      &lt;span key={4}&gt;{record[4]}&lt;/span&gt;
    &lt;/li&gt;
  );
});

答案2

得分: 1

当您在箭头函数中使用花括号时,您正在定义一个可能包含多个语句的代码块。在这种情况下,箭头函数不是隐式返回函数,因此您需要明确指定使用return关键字从函数中返回什么值。如果在代码块中不包括返回语句,函数将不返回任何内容(即返回undefined)。以下代码将起作用。

const records = performanceRecord.map((record, index) => {
    return (
        <li key={index}>
            <span key={0}>{record[0]}</span>
            <span key={1}>{record[1]}</span>
            <span key={2}>{record[2]}</span>
            <span key={3}>{record[3]}</span>
            <span key={4}>{record[4]}</span>
        </li>
    );
});

或者,就像在您的情况下一样,由于您只有一个返回列表的单个JSX元素,您可以将它们括在括号()中,而不是花括号,并避免使用return关键字。

const records = performanceRecord.map((record, index) => (
    <li key={index}>
        <span key={0}>{record[0]}</span>
        <span key={1}>{record[1]}</span>
        <span key={2}>{record[2]}</span>
        <span key={3}>{record[3]}</span>
        <span key={4}>{record[4]}</span>
    </li>
));

括号将JSX隐式返回map函数中的li元素。当JSX很短并且可以放在一行上时,通常使用这种语法。

英文:

When you use curly braces in an arrow function, you are defining a block of code that may contain multiple statements. In this case, the arrow function is not an implicit return function, so you need to explicitly specify what value to return from the function using the return keyword. If you don't include a return statement in the block, the function will not return anything (i.e., it will return undefined). The below code would work.

const records = performanceRecord.map((record, index) =&gt; {
   return (
    &lt;li key={index}&gt;
      &lt;span key={0}&gt;{record[0]}&lt;/span&gt;
      &lt;span key={1}&gt;{record[1]}&lt;/span&gt;
      &lt;span key={2}&gt;{record[2]}&lt;/span&gt;
      &lt;span key={3}&gt;{record[3]}&lt;/span&gt;
      &lt;span key={4}&gt;{record[4]}&lt;/span&gt;
    &lt;/li&gt;
  );
});

Alternatively, as in your case, since you are having only a single JSX element which returns the list, you can enclose them in parenthesis () instead of curly braces and avoid using the return keyword.

const records = performanceRecord.map((record, index) =&gt; (
    &lt;li key={index}&gt;
        &lt;span key={0}&gt;{record[0]}&lt;/span&gt;
        &lt;span key={1}&gt;{record[1]}&lt;/span&gt;
        &lt;span key={2}&gt;{record[2]}&lt;/span&gt;
        &lt;span key={3}&gt;{record[3]}&lt;/span&gt;
        &lt;span key={4}&gt;{record[4]}&lt;/span&gt;
    &lt;/li&gt;
));

The parentheses around the JSX implicitly return the li element from the map function. This syntax is often used when the JSX is short and can fit on a single line.

huangapple
  • 本文由 发表于 2023年6月26日 15:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554371.html
匿名

发表评论

匿名网友

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

确定