Prefetch data from mysql and display in react app whenever the url i requested

Viewed 31

Im pretty new to reactjs.I want to show a list of buses and their data from Mysql db in the frontend React. I've understood to it with a function by using Axios.get, but the problem is i want to show these whenever the url goes like localhost:3000/admin/viewbuses, so that i used {getbuses()} in the body of react function based component. But that is freezing the browser and im pissed off. Is there a better way to do this?

Like run the function only when changes are made or something.

Viewbuses.js

import Axios  from 'axios'
import React, { useState } from 'react'
import { Link } from 'react-router-dom';
import './Viewbuses.css'
export default function Aaddbus() {

  const [buses, setbuses] = useState([])

  const getbuses=()=>{
    Axios.get('http://localhost:3001/showbuses').then((response)=>{
        // console.log(response);
        setbuses(response.data)
    })
    
  }
  return (
    <>
    <br /><br />
    <h1 className='text-center' style={{color:'white'}}>Buses List</h1>
    {/* <button onClick={getbuses}>View all</button> */}

    {/* { getbuses()} */}
    <br /><br /><br /><br />
    
      <table style={{border:'1px solid white',marginLeft:'25%',marginRight:'50%',width:'800px'}}>
        <tbody>
          <tr>
            <th className='text-center' style={{color:'gold',fontSize:'25px'}}>Bus Id</th>
            <th className='text-center' style={{color:'gold',fontSize:'25px'}}>Bus Name</th>
            <th className='text-center' style={{color:'gold',fontSize:'25px'}}>From City</th>
            <th className='text-center' style={{color:'gold',fontSize:'25px'}}>To City</th>
            <th className='text-center' style={{color:'gold',fontSize:'25px'}}>Capacity</th>
            <th className='text-center' style={{color:'gold',fontSize:'25px',width:'50px'}}>Action</th>
          </tr>
          
          {
            buses.map((val,id)=>{
              return <tr>
              <td className='text-center' style={{color:'white',marginRight:'20px',fontSize:'25px',width:'100px'}}>{val.busid}</td>
              <td className='text-center' style={{color:'white',marginRight:'20px',fontSize:'25px'}}>{val.busname}</td>
              <td className='text-center' style={{color:'white',marginRight:'20px',fontSize:'25px'}}>{val.fromcity}</td>
              <td className='text-center' style={{color:'white',marginRight:'20px',fontSize:'25px'}}>{val.tocity}</td>
              <td className='text-center' style={{color:'white',marginRight:'20px',fontSize:'25px'}}>{val.capacity}</td>
              <td className='text-center' style={{color:'white',marginRight:'20px',fontSize:'25px'}}>
                <div>
                <Link to={`/admin/editbus/${id}`} style={{marginLeft:'35px',width:'80px'}} className='  btn btn-small btn-primary me-5'>
                <img src="https://img.icons8.com/offices/30/000000/edit.png" style={{width:'20px',height:'20px'}} alt=''/> </Link>
                </div>
              </td>

              </tr>
       
            })
          }

        </tbody>
      </table>
     
      
    


    </>
  )

}

0 Answers
Related