Reactjs 在firebase&;中设置所有类别;反应
我有两个按钮来过滤显示的类别。过滤工作正常,记录显示正确。现在只有一个类别显示在开始处。我的问题是如何设置类别,使其在开始时显示所有记录? 这是我的密码:Reactjs 在firebase&;中设置所有类别;反应,reactjs,firebase,Reactjs,Firebase,我有两个按钮来过滤显示的类别。过滤工作正常,记录显示正确。现在只有一个类别显示在开始处。我的问题是如何设置类别,使其在开始时显示所有记录? 这是我的密码: const [category, setCategory] = useState( "design"); function useCourses() { const [courses, setCourses] = useState([]); useEffect(() => { db
const [category, setCategory] = useState( "design");
function useCourses() {
const [courses, setCourses] = useState([]);
useEffect(() => {
db.collection("projects")
.where("category", "==", category)
.get()
.then((snapshot) => {
let data = [];
snapshot.forEach((doc) => {
data.push({
...doc.data(),
id: doc.id,
});
return data;
});
setCourses(data);
})
.catch((e) => {
console.log(e);
});
}, [category]);
return courses;
}
const courses = useCourses();
和按钮:
<button
className="projects__controls__button"
onClick={() => setCategory("web")}
style={{ color: theme.color, borderColor: theme.color }}
>
Web
</button>
<button
className="projects__controls__button"
onClick={() => setCategory("design")}
style={{ color: theme.color, borderColor: theme.color }}
>
Design
</button>
setCategory(“web”)}
样式={color:theme.color,borderColor:theme.color}
>
网状物
setCategory(“设计”)}
样式={color:theme.color,borderColor:theme.color}
>
设计
感谢您的帮助您可以将
类别
的初始值设置为null或空字符串
然后在随后的代码中
useEffect(() => {
let collection = db.collection("projects");
if (category) {
collection = collection.where("category", "==", category);
}
collection.get()...
.
.
.
因此,您试图在开始时显示所有类别,并且只希望在用户显式选择类别时进行筛选,对吗?