有人可以详细解释或提供视频 link 来帮助我理解如何在反应中对 mongodb 进行排序
Can someone explain in detail or provide a video link that can help me understand how to sort a mongodb in react
我看到了不同的版本,但老实说它们没有任何意义。我试图实现它们以便自己理解,但我不知道应该把它放在哪里。它会进入我的视图 main.js 文件还是哪个文件,然后它会进入那个文件的什么位置。
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 <a href="addpet"> Add Pet</a>
<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;
感谢@hellogoodnight 和其他人的一些帮助,帮助我理解你在说什么。
答案就是你说的:)
setPet(res.data.users.sort((a, b) => a.pet_name < b.pet_name ? -1 : 1)
我看到了不同的版本,但老实说它们没有任何意义。我试图实现它们以便自己理解,但我不知道应该把它放在哪里。它会进入我的视图 main.js 文件还是哪个文件,然后它会进入那个文件的什么位置。
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 <a href="addpet"> Add Pet</a>
<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;
感谢@hellogoodnight 和其他人的一些帮助,帮助我理解你在说什么。
答案就是你说的:)
setPet(res.data.users.sort((a, b) => a.pet_name < b.pet_name ? -1 : 1)