我遇到了这个问题,希望你能帮助我。所以,我创建了一个 React Hook 来从我的 MySQL 数据库中获取数据。为钩子编写的代码如下所示:
const useMySQLToView = (table) => {
const [rows, setRows] = useState([])
useEffect(() => {
async function fetchData() {
const result = await axios.get("http://localhost:4999/get"+table)
setRows(result)
}
fetchData()
}, [table])
return rows
}
然后我尝试使用组件上的钩子获取数据:
function ViewEmployees() {
const {employees} = useMySQLToView('Employees')
console.log(employees)
return (
<div>
View employees
</div>
)
}
问题是这个 console.log(employees) 返回 undefined。你知道为什么吗?我怎样才能解决这个问题?谢谢!
答案 0 :(得分:1)
用
替换const {employees} = useMySQLToView('Employees')
const employees = useMySQLToView('Employees')
由于您的钩子只是返回 rows
而不是您可以解构的对象