如何使用forwardRef从另一个组件访问底部表格?
我有一个底部的纸,根据按按钮出现在不同的屏幕中,这就是为什么我需要将底部存储在单独的组件中,以便我可以将其称为不同的屏幕。 有关更多插图,请说我有一个主屏幕,并在单击该按钮时带有一个添加按钮,我需要查看底部表。搜索后,我找到了一种通过forwardRef的方法,如下一个代码中所述,问题是onpress = {()=> SheetRef.current.snapto(0)}
底部表格未出现。 请随时提出一种更好的方法来处理这种情况或告诉我有什么问题,谢谢!
import { BottomSheets } from "../components/BottomSheets";
export default function HomeScreen() {
const sheetRef = React.createRef<any>();
.
.
return(
<View>
.
.
<BottomSheets ref={sheetRef} />
<TouchableOpacity style={{ marginTop: 4 }} onPress={() =>
sheetRef.current.snapTo(0)}>
<Text>Add</Text>
</TouchableOpacity>
.
.
</View>
}
和底部表组件
export const BottomSheets = React.forwardRef((props, ref: any) => {
const createRef = React.createRef<any>();
const renderContent = () => ()
const renderCreateContent = () => ()
.
.
return (
<View>
<BottomSheet
ref={ref}
snapPoints={[220, 0]}
renderContent={renderContent}
initialSnap={1}
enabledGestureInteraction={true}
borderRadius={20}
/>
<BottomSheet
ref={createRef}
snapPoints={[650, 0]}
renderContent={renderCreateContent}
renderHeader={renderHeader}
initialSnap={1}
enabledGestureInteraction={true}
enabledContentTapInteraction={false}
enabledBottomInitialAnimation={true}
/>
</View>
);
});
I have a bottom sheet that appears in different screens based on pressing buttons that's why I need to store the bottom sheet in a separate component so I can call it on different screens.
for more illustration say I have a home screen with an add button on clicking that button, I need to view the bottom sheet. After searching I found a way to do so by forwardRef as explained in the next code but the problem is with onPress={() => sheetRef.current.snapTo(0)}
the bottomsheet doesn't appear.
Please feel free to suggest a better way to handle this situation or tell me what's wrong with it, Thanks!
import { BottomSheets } from "../components/BottomSheets";
export default function HomeScreen() {
const sheetRef = React.createRef<any>();
.
.
return(
<View>
.
.
<BottomSheets ref={sheetRef} />
<TouchableOpacity style={{ marginTop: 4 }} onPress={() =>
sheetRef.current.snapTo(0)}>
<Text>Add</Text>
</TouchableOpacity>
.
.
</View>
}
and the bottomSheet component
export const BottomSheets = React.forwardRef((props, ref: any) => {
const createRef = React.createRef<any>();
const renderContent = () => ()
const renderCreateContent = () => ()
.
.
return (
<View>
<BottomSheet
ref={ref}
snapPoints={[220, 0]}
renderContent={renderContent}
initialSnap={1}
enabledGestureInteraction={true}
borderRadius={20}
/>
<BottomSheet
ref={createRef}
snapPoints={[650, 0]}
renderContent={renderCreateContent}
renderHeader={renderHeader}
initialSnap={1}
enabledGestureInteraction={true}
enabledContentTapInteraction={false}
enabledBottomInitialAnimation={true}
/>
</View>
);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论