【问题标题】:Material ui how to toggle open sidebar on searchMaterial ui如何在搜索时切换打开的侧边栏
【发布时间】:2021-12-06 16:31:39
【问题描述】:

当您在导航栏中单击右按钮时,我有一个侧边栏可以正常工作,而不是单击按钮我想在用户开始输入搜索输入时打开它,因为我提供给我的组件 searchInput 作为道具。有了这个 if 语句

if (!!searchInput) {
   toggleDrawer("right", true);
}

在我的代码中,它应该在用户开始输入时将其切换为打开侧边栏,但当我开始输入时没有任何反应。

import { Fragment, useEffect, useState } from "react";
import Box from "@mui/material/Box";
import Drawer from "@mui/material/Drawer";
import Button from "@mui/material/Button";
import List from "@mui/material/List";
import Divider from "@mui/material/Divider";
import ListItem from "@mui/material/ListItem";
import ListItemIcon from "@mui/material/ListItemIcon";
import ListItemText from "@mui/material/ListItemText";
import InboxIcon from "@mui/icons-material/MoveToInbox";
import MailIcon from "@mui/icons-material/Mail";
import { collection, getDocs } from "@firebase/firestore";
import db from "../../firebase";

export default function SearchDrawer({ searchInput }) {
  const [messages, setMessages] = useState([]);
  const [state, setState] = useState({
    right: false,
  });

  useEffect(() => {
    const getMessages = async () => {
      const channels = await getDocs(collection(db, "channels"));
      channels.forEach(async (channel) => {
        const msgs = await getDocs(
          collection(db, "channels", channel.id, "messages")
        );
        msgs.forEach((msg) => {
          setMessages((prev) => [...prev, msg.data()]);
        });
      });
    };
    return () => setMessages([]) && getMessages();
  }, []);
  // console.log(messages);

  const toggleDrawer = (anchor, open) => (event) => {
    if (
      event.type === "keydown" &&
      (event.key === "Tab" || event.key === "Shift")
    ) {
      return;
    }

    setState({ ...state, [anchor]: open });
  };

  if (!!searchInput) {
    toggleDrawer("right", true);
  }

  const list = (anchor) => (
    <Box
      sx={{ width: anchor === "top" || anchor === "bottom" ? "auto" : "45vw" }}
      role="presentation"
      onClick={toggleDrawer(anchor, false)}
      onKeyDown={toggleDrawer(anchor, false)}
    >
      <List>
        {["Inbox", "Starred", "Send email", "Drafts"].map((text, index) => (
          <ListItem button key={text}>
            <ListItemIcon>
              {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
            </ListItemIcon>
            <ListItemText primary={text} />
          </ListItem>
        ))}
      </List>
      <Divider />
      <List>
        {["All mail", "Trash", "Spam"].map((text, index) => (
          <ListItem button key={text}>
            <ListItemIcon>
              {index % 2 === 0 ? <InboxIcon /> : <MailIcon />}
            </ListItemIcon>
            <ListItemText primary={text} />
          </ListItem>
        ))}
      </List>
    </Box>
  );

  return (
    <>
      <Fragment key={"right"}>
        <Button onClick={toggleDrawer("right", true)}>{"right"}</Button>
        <Drawer
          anchor={"right"}
          open={state["right"]}
          onClose={toggleDrawer("right", false)}
        >
          {list("right")}
        </Drawer>
      </Fragment>
    </>
  );
}

【问题讨论】:

  • 不,我在没有清理功能时出错

标签: javascript reactjs material-ui use-effect drawer


【解决方案1】:

你正在返回一个函数

if (!!searchInput) {
   toggleDrawer("right", true);
}

你可以改成

if(searchInput && !state.right){
  setState({right:true})
}

【讨论】:

  • 为我工作,谢谢,另一个闪电战问题,现在我无法输入搜索输入,我该如何解决??
  • 能否分享一下 SearchBar 代码,因为我认为这与 SearchBar 处理 onChange 的方式有关
  • 当然我已经编辑了一点,因为我有无限循环,useEffect(() => { if (!!searchInput) { setState({ right: true }); } else { setState({ right: false }); } }, [searchInput]);
  • 等一下,我给你沙盒的链接
  • 我想我找到了一种解决方案,需要添加 disableEnforceFocus 属性,谢谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 1970-01-01
相关资源
最近更新 更多