ReactJS-提交表格后如何自动刷新

发布于 2025-01-19 09:17:44 字数 2112 浏览 0 评论 0原文

当我提交页面永远不会停止加载时,我必须手动刷新页面以获取表中的输入。 table.js和subm.js分为两个不同的组件。因此,如果我可以在表组件中添加SOM EventListner以在提交后再次获取API,该如何使我的页面自动刷新替代。

类提交扩展组件{

render() {
    return (
        <>
            <form action='http://localhost:3001/api/post' method="POST" onsubmit="return false">
                <input type="input" class="text" name="brand" placeholder="Brand" required />
                <input type="input" class="text" placeholder="Model" required />
                <input type="input" class="text" name="price" placeholder="Price" required />
                <button type="submit" class='button'>Submit</button> 
            </form>
        </>
    )
}

}

function table(){

useEffect(() => {
    Axios.get("http://localhost:3001/api/get/carmodels").then((response) => { 
      setCarModelList(response.data)
    })
  }, [])

  const [carModelList, setCarModelList] = useState([])

  const DeleteCar = (val) => { 
    const next = [...carModelList];
    const removedItems = next.splice(next.indexOf(val), 1);
   const deleteCarModel = Axios.delete(`http://localhost:3001/api/delete/${val.id}`); 

   setCarModelList(next);
    return deleteCarModel

  }  

  const renderTableData = () => {
    return carModelList.map((val) => (
        <tr class>
           <td>{val.id}</td>
            <td>{val.brand}</td>
            <td>{val.model}</td>
            <td>{val.price}</td>
            <td>
                <button id="delete" onClick={() => DeleteCar(val)}>Delete</button> 
            </td>
        </tr>))
  }

return (
    <table id="table">
    <thead>
        <tr>
          <th>Id</th>
          <th>Brand</th>
          <th>Model</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
          {renderTableData()}
      </tbody>
      </table>
    );
  

}

When i submit the page never stops loading and i have to manually refresh the page to get the input in my table. the table.js and submit.js is in two different components. So how do i make my page automatically refresh alternativly if i could add som eventlistner in the table component to get the API again after a submit.

class Submit extends Component {

render() {
    return (
        <>
            <form action='http://localhost:3001/api/post' method="POST" onsubmit="return false">
                <input type="input" class="text" name="brand" placeholder="Brand" required />
                <input type="input" class="text" placeholder="Model" required />
                <input type="input" class="text" name="price" placeholder="Price" required />
                <button type="submit" class='button'>Submit</button> 
            </form>
        </>
    )
}

}

function Table() {

useEffect(() => {
    Axios.get("http://localhost:3001/api/get/carmodels").then((response) => { 
      setCarModelList(response.data)
    })
  }, [])

  const [carModelList, setCarModelList] = useState([])

  const DeleteCar = (val) => { 
    const next = [...carModelList];
    const removedItems = next.splice(next.indexOf(val), 1);
   const deleteCarModel = Axios.delete(`http://localhost:3001/api/delete/${val.id}`); 

   setCarModelList(next);
    return deleteCarModel

  }  

  const renderTableData = () => {
    return carModelList.map((val) => (
        <tr class>
           <td>{val.id}</td>
            <td>{val.brand}</td>
            <td>{val.model}</td>
            <td>{val.price}</td>
            <td>
                <button id="delete" onClick={() => DeleteCar(val)}>Delete</button> 
            </td>
        </tr>))
  }

return (
    <table id="table">
    <thead>
        <tr>
          <th>Id</th>
          <th>Brand</th>
          <th>Model</th>
          <th>Price</th>
        </tr>
      </thead>
      <tbody>
          {renderTableData()}
      </tbody>
      </table>
    );
  

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2025-01-26 09:17:44

代码示例为另一个在单击提交按钮时初始化为 false 的警报变量添加 UseState ,你将其设为 true,并在获取后将其恢复为 false。附图中的示例

const [alert, setAlert] = useState(true);

const handleSubmit= (e)=> {

setAlert(true)

useEffect(()=>{
// perform your fetch
 setAlert(false)
},[alert])
 
}

code exampleAdd UseState for another alert variable that is initialized to false, when you click the submit button, you make it true, and after you fetch, you turn it back to false. example in the image attached

const [alert, setAlert] = useState(true);

const handleSubmit= (e)=> {

setAlert(true)

useEffect(()=>{
// perform your fetch
 setAlert(false)
},[alert])
 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文