【问题标题】:Endless axios requests无休止的 axios 请求
【发布时间】:2021-03-18 22:27:46
【问题描述】:

我已经正常工作了,从 React 中的 firebase 数据库读取和显示数据。

但是,它无休止地请求数据并吃掉 ram 和网络。

我哪里做错了?我已经检查了这里的其他问题和答案,但无法解决。

function Requests() {


    const [RequestList, setRequests] = useState("");

    axios.get('firebasedatabase.json')
        .then(response => {
            const fetchedRequests = [];
            for (let key in response.data) {
                fetchedRequests.push({
                    ...response.data[key],
                    key: key
                });
            }
            setRequests(fetchedRequests);
            
        })
        .catch(error => console.log(error));
console.log(Requests, "requests");
    

    return (
        <>
            <div id="main">
                <header>
                    <h1>Requests</h1>
                </header>
                <p>
                    List of current requests
                </p>
                <div className="requestsContainer">
                    <table>
                        <thead>
                            <th>Title</th>
                            <th>Request By</th>
                            <th>Date</th>
                            <th>Remove</th>
                        </thead>
                        <tbody>
                            {
                                RequestList && RequestList.map((data, index) => {
                                    return (
                                        <tr key={index}>
                                        <td><a href={data.requestURL}>{data.requestTitle}</a></td>
                                        <td><a href={"mailto:" + data.contactEmail}>{data.requestUser}</a></td>
                                        <td>{data.requestDate}</td>
                                        <td>icon</td>
                                    </tr>
                                    )
                                })
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </>
    )
}


export default Requests;

【问题讨论】:

    标签: reactjs firebase firebase-realtime-database axios


    【解决方案1】:

    你需要将你的 axios 放在一个函数中并在 useEffect 中调用它。所以这种方式只会调用 axios 一次。 确保在顶层导入 useEffect。

    const callApi = () => {
       axios
          .get('firebasedatabase.json')
          .then(response => {
             const fetchedRequests = [];
             for (let key in response.data) {
                fetchedRequests.push({
                   ...response.data[key],
                   key: key,
                });
             }
             setRequests(fetchedRequests);
          })
          .catch(error => console.log(error));
       console.log(Requests, 'requests');
    };
    
    useEffect(() => {
       callApi();
    }, []);
    

    因为每次发生更改时都会重新渲染组件。在您的情况下,当您调用 Axios 并且您正在设置需要对重新渲染做出反应的状态时。因此,当它重新渲染组件时,Axios 会被一次又一次地调用。

    【讨论】:

    • 谢谢!这现在看起来很明显...... ^^;
    • 不用担心,您可以通过单击对勾图标来接受我的回答,这样其他人就会知道这是一个可行的解决方案,谢谢 :)
    • 可能应该将 RequestList 添加到 useEffecr 依赖项
    • @eamanola 不,因为这将再次无限次调用 Axios,因为状态会更新。
    • 真的,我的错
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 2021-11-13
    • 1970-01-01
    • 2021-06-01
    • 2020-07-15
    • 2018-06-28
    • 2022-01-28
    相关资源
    最近更新 更多