【发布时间】:2021-10-11 10:03:11
【问题描述】:
我有一个这样的父组件,只是为了显示对话框
子组件(主显示对话框)
export const MedicalRecord = memo(function MedicalRecord() {
// const onPressViewAll = useCallback(() => {}, [])
const [show, setShow] = useState(false) ///to show dialog
function hanndleDialog() {
setShow(!show) set to show dialog
}
// useEffect(() => {
// if (show == true) {
// setShow(!show)
// }
// },[show])
return (
<SummaryViewContainer
count={5}
title={"dashboardScreen.medicalRecords.title"}
onPress={() => {
hanndleDialog()
}}
>
<View>
{show && (
<ProgressDialog
show={show} //pass t
callback={() => {
hanndleDialog()
}}
/>
)}
<RecordItem />
<RecordItem />
<RecordItem />
</View>
</SummaryViewContainer>
)
})
以及显示此对话框的父组件
export default function DialogTesting(show: boolean, { callback }) {
const [showDialog, doShow] = useState(show) //show to present show in child
return (
<View>
{/* <Button
title="click"
onPress={() => {
setShow(true)
}}
>
<Text>Show dialog</Text>
</Button> */}
<Dialog
visible={showDialog}
title="Record New Progress"
style={DIALOG}
onClose={() => {
doShow(false)
callback()
}}
>
但我不知道如何在关闭对话框时再次打开对话框,它只打开一次,我尝试React Hook : Send data from child to parent component 但不起作用!
我如何显示对话框,当我点击关闭按钮时,孩子们会返回原始状态,所以我可以再次点击它,非常感谢你们
这是这个问题的简短视频 https://recordit.co/0yOaiwCJvL
【问题讨论】:
-
你能分享一个最低工作的sn-p吗?
-
不幸的是,这段代码是整个项目,不是我的 :((( ,所以很难分享工作代码
-
然后尝试删除
memo,因为 props 和 states 是相同的,所以它可以渲染记忆的组件。我只是猜测。 -
它不起作用 :( ,我整天都在尝试这样做
-
This is the sandbox 建议的最后一个解决方案。如果我正确理解了问题陈述,我希望这会有所帮助。
标签: reactjs react-native react-hooks