Extract a phpmyadmin table name using react - reactjs

ello,
I created a CRUD application based on mysql database and react as framework,
As i’m going to use multiple tables in the database, I want to create a first page that extracts from the database all the tables names on a list. Once the list with the tables names is created, i want to be able to click on the table name and be redirected to the CRUD page that I already created.
The code below is what I used to create the first page but it failed to work.
I attached phpmyadmin database and the capture of structure of the project
import React, { useState, useEffect } from "react";
import axios from "axios";
const TabList = () => {
const [tab, settab] = useState("");
useEffect(() => {
axios.get('http://localhost/phpmyadmin/index.php?
route=/database/structure&server=1&db=crud_db')
.then(res=>{
console.log(res)
settab(res.data)
})
.catch(err=>{
console.log(err)
})
}, []);
return (
<div className="columns mt-5 is-centered">
<div className="column is-half">
<table className="table is-striped is-fullwidth">
<thead>
<tr>
<th>N</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{tab.map((tab , index) => (
<tr key={tab.id}>
<td>{index + 1}</td>
<td>{tab.name}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
);
};
export default TabList;
tables
vsc
vsc

Related

unable to fetch firebase realtime database data into website UI using ReactJs

I am new to firebase (version-9) , and after trying alot , i am unable to fetch the data from firebase realtime database , i can see the data in console , but i want to render it into my website table, please help. below is my code snippet .
``
import React, {useState, useEffect} from 'react';
import { db } from '../firebase-config';
import { Link } from 'react-router-dom';
import { ref, onValue } from 'firebase/database';
const ViewAll = () => {
const [posts, setPosts] = useState([]);
useEffect(() => {
const posts = ref(db, 'superadmin/admins/')
onValue(posts,(snapshot) => {
const json = ({...snapshot.toJSON()})
const keys = Object.keys(json);
const postJSON = keys.map(key => {
const element = json[key];
return [element.FullName,element.doctors, element.technicians, element.patients]
});
setPosts(postJSON);
console.log({...snapshot.val()})
})
}, []);
``
``
return (
<div style={{marginTop: "100px"}}>
<table className='styled-table'>
<thead>
<tr>
<th style={{textAlign: "center"}}>No.</th>
<th style={{textAlign: "center"}}>Name</th>
<th style={{textAlign: "center"}}>Action</th>
</tr>
</thead>
<tbody>
{Object.keys(posts).map((id,index)=>{
return (
<tr key={id}>
<th scope='row'>{index + 1}</th>
<td>{posts[id].name}</td>
<td>
<Link to={'/SuperAdmin/ViewAll'}>
<button className='btn btn-view'>View</button>
</Link>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
)
}
export default ViewAll
``
This is my code snippet
code snippet part 2
I'm Using Firebase version 9 in reactjs.
I am able to view the data in the console , I just want to render it into my website , and since i am new to firebase , i dont know how to render it in the website,
any sort of help will be hihgly appreciated.

Frontend page doesn't display unless i comment out my table making code

Here is my code. I used typescript and my database is in a .json file. My page displays fine when I don't try to display the table and disappears completely
import React, { useState } from "react";
import "./viewAvailableShifts.css";
import "bootstrap/dist/css/bootstrap.min.css";
import MockData from "./data.json";
export class ViewAvailableShifts extends React.Component {
render() {
const [data] = useState(MockData);
return (
<>
<div className="row">
<div className="leftcolumn">
<div className="center">
<h1>Available Shifts</h1>
<div>
<table>
<thead>
<th>First Name</th>
<th>Last Name</th>
</thead>
<tbody>
{data.map((d) => (
<tr key={d.id}>
<td>{d.first_name}</td>
<td>{d.last_name}</td>
<td>{d.email}</td>
<td>{d.gender}</td>
</tr>
))}
</tbody>
</table>
</div>
Have you tried mapping the table rows outside of the return? Also wondering why data was in square brackets? Maybe curley braces or none at all, depending on how you return it from state? so if it's already an array just data if you need to make it an array maybe spread [...data]?
export class ViewAvailableShifts extends React.Component {
render() {
const data = useState(MockData)
const rows = data.map((d) => (
<tr key={d.id}>
<td>{d.first_name}</td>
<td>{d.last_name}</td>
<td>{d.email}</td>
<td>{d.gender}</td>
</tr>
))
return (
<>
<div className="row">
<div className="leftcolumn">
<div className="center">
<h1>Available Shifts</h1>
<div>
<table>
<thead>
<th>First Name</th>
<th>Last Name</th>
</thead>
<tbody>{rows}</tbody>
</table>
</div>
</div>
</div>
</div>
</>
)
}
}
Hooks doesn't work inside class component
import React, { useState } from "react";
import "./viewAvailableShifts.css";
import "bootstrap/dist/css/bootstrap.min.css";
import MockData from "./data.json";
export const ViewAvailableShifts = () => {
const [data] = useState(MockData);
return (
<>
<div className="row">
<div className="leftcolumn">
<div className="center">
<h1>Available Shifts</h1>
<div>
<table>
<thead>
<th>First Name</th>
<th>Last Name</th>
</thead>
<tbody>
{data.map((d) => (
<tr key={d.id}>
<td>{d.first_name}</td>
<td>{d.last_name}</td>
<td>{d.email}</td>
<td>{d.gender}</td>
</tr>
))}
</tbody>
</table>
</div>
What are you trying to accomplish with useState? useState is a hook that listens for changes to data and then changes the UI accordingly. Use state returns two values though, it would be used like this...
const [data, setData]= useState(someDataOrEmptyValueButNeverActuallyEmpty)
onSomeEvent((eventOrDataOrWhatNot) => setData(eventOrDataOrWhatNot))
and then whatever in your UI that was depending on data will adjust to the new values.
So, are you ready?You can't us hooks in class components
export const ViewAvailableShifts = () => {
const [data] = useState(MockData);
‏}
Should be
export default class ViewAvailableShifts extends Component {
constructor() {
super();
this.state: {data: MockData}
}
render(){...}
}

Cannot update a component (`App`) while rendering a different component (`UserTable`)

I'm trying to learn React Hooks in functional components, and am following along with React Hooks tutorial but am getting the error: Cannot update a component (App) while rendering a different component (UserTable), and the error stack indicates this is related to the onClick={props.deleteUser(user.id)} property in the delete button in UserTable.js. I saw several posts indicating that one should try useEffect() to get around this issue, so I tried having deleteUser update a state variable, and then have useEffects change the users array. While the code compiled fine, the page simply hung and eventually timed out with an "out of memory" error (I assume caused by an endless cycle of trying to render and re-render?). Any ideas how to fix this situation?
App.js
import React, { useState } from 'react';
import UserTable from './tables/UserTable';
import AddUserForm from './forms/AddUserForm';
const App= () => {
const usersData = [
{id: 1, name: "Tania", username: "floppydiskette"},
{id: 2, name: "Craig", username: "siliconeidolon" },
{id: 3, name: "Ben", username: "benisphere"}
]
const [users, setUsers] = useState(usersData);
const addUser = (user) => {
user.id = users.length+1;
setUsers([...users,user])
}
const deleteUser = (id) => {
setUsers(users.filter((user)=>user.id !== id))
}
return (
<div className="container">
<h1> SIMPLE CRUD APP WITH HOOKS</h1>
<div className="flex-row">
<div className = "flex-large">
<h2> Add User </h2>
<AddUserForm addUser={addUser}/>
</div>
<div className = "flex-large">
<h2>View Users</h2>
<UserTable users={users} deleteUser={deleteUser}/>
</div>
</div>
</div>
);
}
export default App;
UserTable.js
import React from 'react';
const UserTable = (props) => {
return(
<table>
<thead>
<tr>
<th>Name</th>
<th>UserName</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{props.users.length > 0 ? (
props.users.map((user) => (
<tr key={user.id}>
<td>{user.name}</td>
<td>{user.username}</td>
<td>
<button className="button muted-button">Edit</button>
>>> This triggers the `cannot update a component . . .` error:
<button className="button muted-button" onClick={props.deleteUser(user.id)}>Delete</button>
</td>
</tr>
))
) : (
<tr colspan={3}>No Users</tr>
)}
</tbody>
</table>
);
}
export default UserTable
You just have to change
onClick={props.deleteUser(user.id)}>Delete</button>
to
onClick={()=> props.deleteUser(user.id)}>Delete</button>
Otherwise your delete function will get automaticaly fired on render itself

Can someone explain in detail or provide a video link that can help me understand how to sort a mongodb in react

I saw different versions but honestly they make no sense. I tried to implement them so I can understand it myself but I didn't know where I should put it at. does it go in my view main.js file or what file and then where in that file does it go.
import React, { useState, useEffect } from 'react'
import axios from 'axios'
import { Link } from '#reach/router'
const Main = props => {
const [pets, setPet] = useState()
useEffect((req, res) => {
axios.get(`http://localhost:8000/api/users`)
.then(res => {// does the sort go here
// console.log(res.data.users)
setPet(res.data.users)
})
}, [])
// would the sort go before or in here ?
return (
<div>
These pets need a home: do you know any other pets Add Pet
<table className='table table-striped table-dark '>
<thead className="">
<tr>
<th>Name</th>
<th>Type</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{
pets ?
pets.map((pet, i) => {
return (
<tr key={i}>
<td>{pet.pet_name}</td>
<td>{pet.pet_type}</td>
<td><Link to={`/viewpet/${pet._id}`}>Detail</Link> | <Link to={`/update/${pet._id}`}>Edit</Link></td>
</tr>
)
})
: ''
}
</tbody>
</table>
</div>
)
}
export default Main;
Thanks to #hellogoodnight and some help to other folks to help me understand what you were saying.
the answer was what you said :)
setPet(res.data.users.sort((a, b) => a.pet_name < b.pet_name ? -1 : 1)

Why is my data not displaying in the table in react

Does anyone know why my data isn't displaying, it console.logs 'x[0]', 'x[1]', 'x[2]', and 'x[3]' fine in my return statement. I'm pretty new to react and programming in general so I have no idea why this doesn't work.
I would just expect it to fill the rows of the table like the 2 I've manually coded in.
import React, { useState, useEffect } from 'react'
import "./stylesheets/oddsmatcher-table.css"
const App = () => {
const [wow, setWow] = useState([])
useEffect(() => {
fetch(DATA)
.then(res => res.json())
.then(data => {
const newData = data.data.slice(0, 10)
const k = newData.map(x => {
return [x.date, x.event_name, x.bookmaker_name, x.exchange]
})
setWow(k)
console.log(k)
})
}, [])
return(
<table>
<tbody>
<tr>
<th>Date</th>
<th>Event</th>
<th>Bookie</th>
<th>Exchange</th>
</tr>
<tr>
<td>25-09-2020</td>
<td>Man United vs Liverpool</td>
<td>Bet365</td>
<td>Smarkets</td>
</tr>
<tr>
<td>26-09-2020</td>
<td>Arsenal vs Man City</td>
<td>Coral</td>
<td>Betfair Exchange</td>
</tr>
{wow.forEach(x => {
return(
<tr>
<td>{x[0]}</td>
<td>{x[1]}</td>
<td>{x[2]}</td>
<td>{x[3]}</td>
</tr>
)
})}
</tbody>
</table>
)
}
export default App
Update: Try switching your wow.forEach to this:
{wow.map((x, index) => {
return (
<tr key={index}>
{x.map((dataPiece) => (
<td key={dataPiece}>{dataPiece}</td>
))}
</tr>
);
})}
Here's the Codesandbox I was using to test. I replaced your async fetch with a global variable with what I think your wow data looks like:
https://codesandbox.io/s/suspicious-glitter-mf0tg?fontsize=14&hidenavigation=1&theme=dark
Let me know if that works. If it doesn't, can you post an example of what your fetched data looks like?

Resources