【问题标题】:Infinite loop when using array map使用数组映射时的无限循环
【发布时间】:2020-10-19 23:48:47
【问题描述】:

自从我添加了

const dataList = dataSet.map(element => {
  return <div>{element}</div>;
});

它进入了一个无限循环,但这条线也是我的程序显示注释所必需的,我该怎么办?

import React, { useEffect, useState } from 'react';
import axios from 'axios';
import Navbar from './Navbar';

function Notes() {
  //This is just a string because we are sending just one
  //might have to make it an array at some point
  const [notes, setNote] = useState(String);

  var dataArr = [];

  const [dataSet, setDataSet] = useState([]);

  const [dataList, setDataList] = useState();

  useEffect(() => {
    console.log('Use Effect Notes.js');

    axios
      .post('/api/user/notes')
      .then(res => {
        dataArr = res.data[0].notes;
        //console.log(dataArr) ;
        console.log(dataArr);
        setDataSet(res.data[0].notes);
      })
      .catch(err => {
        console.log('it didnt work' + err);
      });

    console.log('The array that i got ');
  });

  const dataList = dataSet.map(element => {
    return <div>{element}</div>;
  });

  /*const taskList = task.map((object , index)=>{
        return <div className='row justify-content-center'>

            <h2>{object}</h2>

        </div>
    });*/

  function noteDown(event) {
    event.preventDefault();

    var newNote = {
      notes: notes,
    };

    console.log('note down ' + newNote);
    axios
      .post('/api/user/notes/add', newNote)
      .then(res => {
        console.log(res);
      })
      .catch(err => {
        console.log(err);
      });
  }

  function ffswork() {
    console.log('ffswork');
  }
  return (
    <div>
      <div>
        <h1> Notes </h1>
      </div>
      <form onSubmit={noteDown}>
        <input
          type="text"
          placeholder="Note"
          className="form-control"
          value={notes}
          onChange={e => {
            setNote(e.target.value);
          }}
        />
        <input type="submit" value="AddNote" className="btn btn-primary" />
      </form>
      <button className="btn btn-primary" onClick={ffswork}>
        getData
      </button>
    </div>
  );
}
export default Notes;

【问题讨论】:

  • 为什么dataList 被声明了两次? dataList 甚至不会在您的代码 sn-p 中使用。

标签: reactjs infinite-loop


【解决方案1】:

我想你忘了在 useEffect 的第二个参数中传递数组。

  useEffect(() =>{
        console.log("Use Effect Notes.js");

        axios.post('/api/user/notes' ).then(res=>{
            dataArr = res.data[0].notes ; 
            //console.log(dataArr) ;
            console.log(dataArr); 
            setDataSet(res.data[0].notes); 
           
        }).catch(err=>{
            console.log('it didnt work' + err); 
        }); 


        console.log("The array that i got ");

    }, []) // at this line

如果某些值,你可以告诉 React 跳过应用效果 重新渲染之间没有变化。

因此,如果您想在渲染后只请求一次获取数据,您只需在 useEffect 的第二个参数中传递一个空数组。

阅读更多here

【讨论】:

    猜你喜欢
    • 2011-07-06
    • 2015-10-01
    • 2020-09-04
    • 2011-08-05
    • 2021-02-13
    • 2019-12-11
    • 1970-01-01
    • 2017-12-10
    • 2018-06-23
    相关资源
    最近更新 更多