我试图在页面上呈现25个条目(namecompanyskill等),但无法获得唯一的信息.

i、 我只是一遍又一遍地看到第一个条目.我不知道如何重申新信息,因为它只会是response.data.students[0].

这就是我的代码现在的样子,映射是:

export default function StudentList() {
  let [loaded, setLoaded] = useState(false);

  let [students, setStudent] = useState(" ");
  function doInfo(response) {
    console.log(response.data);
    setStudent(response.data.students);
    setLoaded(true);
  }

  if (loaded) {
    return (
      <div className="StudentList">
        <div className="row">
          <div className="col">
            {students.map(function (data, index) {
              return <StudentInfo data={data} key={index} />;
            })}
          </div>
        </div>
      </div>
    );
  } else {
    let url = "https://api.hatchways.io/assessment/students";
    axios.get(url).then(doInfo);
  }
}

以下是StudentInfo组件的编码,以供参考:

export default function StudentInfo() {
  const [info, addInfo] = useState(" ");

  function setInfo(response) {
    addInfo({
      number: response.data.students[0].id,
      first: response.data.students[0].firstName,
      last: response.data.students[0].lastName,
      email: response.data.students[0].email,
      company: response.data.students[0].company,
      skill: response.data.students[0].skill,
      average: response.data.students[0].grades[0],
    });
  }

  let url = "https://api.hatchways.io/assessment/students";
  axios.get(url).then(setInfo);

  return (
    <div className="StudentInfo">
      <h1>{info.number}.</h1>
      <h2>
        Name: {info.first} {info.last}
      </h2>
      <h2>Email: {info.email}</h2>
      <h2>Company: {info.company}</h2>
      <h2>Skill: {info.skill}</h2>
      <h2>Average: {info.average}</h2>
    </div>
  );
}

有人能帮我把它编码成25个条目,而不仅仅是第一个条目吗?

推荐答案

试试这样:

  • 将数组设置为状态
  • 使用effect(…,[])触发数据加载(仅一次).
  • 在映射的数组扩展上设置key={id}(或者在这种特殊情况下不适用的有充分理由的react投诉).
function StudentList() {
  const [students, setStudents] = useState(null);
 
  useEffect(() => {
    const url = "https://api.hatchways.io/assessment/students";
    axios.get(url).then(({ data: { students } }) => setStudents(students));
  }, []);

  if (!students) {
     return ( <div>loading...</div> );
  }

  return (
    <div className="StudentList">
      <div className="row">
        <div className="col">
          { students.map(student => ( <StudentInfo key={student.id} info={student} /> )) }
        </div>
      </div>
    </div>
  );
}

StudentInfo不需要重新加载任何内容,只需格式化已经获取的数据

export default function StudentInfo({ info }) { // edit: previous had { props: { info } } here, oops
  return (
    <div className="StudentInfo">
      <h1>{info.number}.</h1>
      <h2>
        Name: {info.first} {info.last}
      </h2>
      <h2>Email: {info.email}</h2>
      <h2>Company: {info.company}</h2>
      <h2>Skill: {info.skill}</h2>
      <h2>Average: {info.average}</h2>
    </div>
  );
}

Javascript相关问答推荐

订阅操作顺序

追踪执行顺序

JavaScript Date对象在UTC中设置为午夜时显示不正确的日期

我可以后增量超过1(最好是内联)吗?

nPM审计始终发现0个漏洞

如何提取Cypress中文本

禁用从vue.js 2中的循环创建的表的最后td的按钮

为什么我的includes声明需要整个字符串?

被CSS优先级所迷惑

为什么promise对js中的错误有一个奇怪的优先级?

如何使onPaste事件与可拖动的HTML元素一起工作?

使用Promise.All并发解决时,每个promise 的线性时间增加?

无法使用单击按钮时的useState将数据从一个页面传递到另一个页面

Use Location位置成员在HashRouter内为空

传递方法VS泛型对象VS事件特定对象

如何在一个对象Java脚本中获取不同键的重复值?

如果NetSuite中为空,则限制筛选

在GraphQL解析器中修改上下文值

JavaScript将字符串数字转换为整数

react 路由DOM有条件地呈现元素