【发布时间】:2020-06-09 17:30:05
【问题描述】:
我正在使用语义 UI React。 以下 JS 代码对我不起作用:
import React, { useState, useEffect } from "react";
import { Dropdown, Form, Button } from "semantic-ui-react";
export const MovieDropdown = () => {
const [movie, setMovie] = useState("");
const [person, setPerson] = useState("");
const [movieOptions, setMovieOptions] = useState([]);
const [personOptions, setPersonOptions] = useState([]);
useEffect(() => {
Promise.all([
fetch("/people").then(res =>
res.json()
),
fetch("/movies").then(res =>
res.json()
)
])
.then(([res1, res2]) => {
console.log(res1, res2);
var make_dd = (rec) => {
rec.map(x => {
return {'key': x.name, 'text': x.name, 'value': x.name}
})
}
setPersonOptions(make_dd(res1))
setMovieOptions(make_dd(res2))
})
.catch(err => {
console.log(err);
});
});
return (
<Form>
<Form.Field>
<Dropdown
placeholder="Select Movie"
search
selection
options={movieOptions}
onChange={(e, {value}) => setMovie(value)}
/>
</Form.Field>
<Form.Field>
<Dropdown
placeholder="Select Person"
search
selection
options={personOptions}
onChange={(e, {value}) => setPerson(value)}
/>
</Form.Field>
</Form>
);
};
export default MovieDropdown;
问题是我在运行此组件时丢失了数据库连接。我尝试使用 MySQL 和 SQLite,它给出了同样的问题。 如何解决这个问题?我应该对每个组件进行 1 次提取吗? 提前谢谢你。
亲切的问候, 西奥
【问题讨论】:
-
useEffect可能缺少作为第二个参数的依赖数组 - 现在它在每次组件呈现时都在运行,这意味着它只是不断发送请求。 -
虽然这会导致数据库连接失败,但您可能需要使用您正在使用的任何数据库库查看连接池。
标签: javascript reactjs semantic-ui-react