reactjs - 将可重用组件的更改传递到另一个可重用组件中
问题描述
我遇到了一个漂亮的库Cupertino-Pane
,它就像一个从底部打开的抽屉,就像您单击Twitter
底部可拖动抽屉上的选项图标时一样。欲了解更多信息:https://github.com/roman-rr/cupertino-pane
我有一个想要实现的逻辑,因为在某些情况下我将在我的应用程序中使用窗格,所以我决定将其设为可重用组件,如下所示:
import { CupertinoPane } from "cupertino-pane";
import { useEffect, useRef } from "react";
import { PanelProps } from "../../interfaces";
const IonDrawer = ({ panelKey, show, children }: PanelProps) => {
const drawerRef = useRef<CupertinoPane>();
const hidePanel = async () => {
console.log("Tapped");
await drawerRef.current?.hide();
};
useEffect(() => {
drawerRef.current = new CupertinoPane(`.${panelKey}`, {
backdrop: true,
bottomClose: true,
buttonClose: false,
parentElement: "body",
fastSwipeClose: true,
fitHeight: true,
onBackdropTap: () => hidePanel(),
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
const showPanel = async () => {
await drawerRef.current?.present({ animate: true });
};
if (show) {
showPanel();
} else {
hidePanel();
}
});
return <div className={panelKey}>{children}</div>;
};
export default IonDrawer;
这是主窗格。onBackdropTap
,我想始终禁用面板。
现在这个组件现在被另一个可重用的组件使用。
const Feed: FC<FeedProps> = ({
.....
panelKey,
......
}) => {
const slideRef = useRef<HTMLIonSlidesElement>(null);
const [show, setShow] = useState<boolean>(false);
<IonDrawer show={show} panelKey={panelKey}>
<h3>John Doe</h3>
</IonDrawer>
<IonButton className="btn" fill="clear" color="secondary">
<div className="d-flex align-center">
<IoEllipsisHorizontalCircleOutline
size="20"
// ShowPane When Button is Clicked, then Hide When Backdrop is Clicked, But i got confused
onClick={() => setShow(true)}
/>
</div>
</IonButton>
在我的 HomeComponent 中,Feed 组件是这样渲染的
const Feed1: FeedContent = {
...OtherProps
panelKey: "panel-1",
};
const Feed2: FeedContent = {
...otherProps
panelKey: "panel-2",
};
<Feed {...Feed1} />
<Feed {...Feed2} />
我想在单击“上方按钮”时显示窗格,并在单击 中时禁用它backDrop
,IonDrawer
但是单击背景时show
不会转到false
.
当从 IonDrawer 中点击 BackDrop 时,我怎么知道?
解决方案
棘手但解决了!!!
我传递了一个回调函数作为道具,IonDrawer component,
如下所示:
onClosePanel,
然后函数调用
hidepanel() function:
onClosePanel(() => {
hidePanel();
});
在另一个组件中使用 IonDrawer 时,
<IonDrawer
show={show}
....
onClosePanel={() => setShow(false)}
>
<IonItem>
<IonLabel>Join</IonLabel>
<p>dddndndndbdb</p>
<IonLabel>Join</IonLabel>
</IonItem>
</IonDrawer>
推荐阅读
- flutter - 在 Flutter 中实现卡片翻转动画
- html - 基于真假在同一个模板中应用CSS类的最佳方式
- powershell - Powershell更改所有文件夹和子文件夹中快捷方式的路径
- javascript - 如何在javascript中对对象的总和数组进行分组?
- xamarin.forms - Xamarin 在框架内形成导航
- javascript - 如何在 reactjs 中创建一个异步函数,该函数从 redux 操作中调用另外两个函数
- java - 在 spring 提供的这么多事件中 - 哪个事件是正确的事件来监听我的应用程序的数据缓存
- asp.net - 数据表 asp.net 的最后一行
- html - 基于最宽标签对齐表单输入元素的 CSS 方式
- c# - 是否可以在 App.xaml.cs 上自动注册 ViewModels 和 Views