小编bab*_*830的帖子

React 删除按钮后刷新页面

我的应用程序的删除功能工作正常,但是它要求用户在单击删除按钮后手动刷新页面才能查看数据库中的新元素列表。我想在点击事件后自动刷新。我在这个项目中使用反应钩子。然而,如果我删除,我找到了一个解决方案useEffect's [],但在我的后端它显示,它的请求疯狂。我不知道,删除 useffect 的[ ]是否明智?

这是从后端获取数据并将 props 传递给另一个组件的组件

 import React, { useState, useEffect } from "react";
    import axios from "axios";
    import Table from "../Table/Table";
    import "./Display.css";
    const Display = () => {
      const [state, setState] = useState({ students: [], count: "" });
      const [searchItem, setsearchItem] = useState({
        item: ""
      });

      const Search = e => {
        setsearchItem({ item: e.target.value });
      };

      useEffect(() => {
        axios
          .get("/students")
          .then(response => {
            setState({
              students: response.data.students,
              count: response.data.count
            });
          }) …
Run Code Online (Sandbox Code Playgroud)

reactjs use-effect

2
推荐指数
1
解决办法
1万
查看次数

React Hook useEffect 缺少依赖项:'props.match.params.id'

我已经创建了一个 React 应用程序。对于数据获取,我使用了 axios。我的应用程序按预期工作正常。但是在我的终端中,我收到了这样的警告Line 34:6: React Hook useEffect has a missing dependency: 'props.match.params.id'. Either include it or remove the dependency array react-hooks/exhaustive-deps。我不想使用// eslint-disable-next-line react-hooks/exhaustive-deps. 有什么替代解决方案吗?

  useEffect(() => {
    axios
      .get("http://localhost:5000/students/" + props.match.params.id)
      .then(response => {
        setState({
          name: response.data.name,
          birthday: response.data.birthday,
          address: response.data.address,
          zipcode: response.data.zipcode,
          city: response.data.city,
          phone: response.data.phone,
          email: response.data.email
        });
      })
      .catch(function(error) {
        console.log(error);
      });

  }, []);
Run Code Online (Sandbox Code Playgroud)

axios react-hooks use-effect

-1
推荐指数
1
解决办法
899
查看次数

标签 统计

use-effect ×2

axios ×1

react-hooks ×1

reactjs ×1