reactjs - 如何使用 React Hooks 在 React 中填充对话
问题描述
我需要使用 Material UI 对话框显示一些信息。在 useEffect 钩子中调用 handleOpen 函数会在页面打开时默认打开对话框。有人可以帮助告诉我如何仅在单击按钮时填充对话框吗?请注意,postId 是作为来自父组件的 props 传递的。提前致谢
import { useState, useEffect } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import { getSinglePost } from '../../store/actions/postActions'
//MUI Components
import { Dialog, DialogHeader, Button, DialogContent, DialogActions, Grid, Typography, IconButton}
from '@material-ui/core'
import LaunchIcon from '@material-ui/icons/Launch';
const PostDialog = ({postId}) => {
const [ open, setOpen ] = useState(false)
const post = useSelector(state => state.posts.post)
const dispatch = useDispatch()
useEffect(() => {
handleOpen()
}, [])
const handleOpen = () => {
setOpen({ open: true })
dispatch(getSinglePost(postId))
}
const handleClose = () => {
setOpen({ open: false })
}
return (
<div>
<Button onClick={handleOpen}>
<LaunchIcon fontSize="small" color="primary" />
</Button>
<Dialog
open={open}
onClose={handleClose}
fullWidth
maxWidth="sm"
>
<DialogContent>
<Grid container>
<Grid item sm={5}>
{/* <img src={userImage} width="150" /> */}
</Grid>
<Grid>
<Typography variant="h6">@{post.handle}</Typography>
<Typography variant="h6">{post.job_title}</Typography>
<Typography variant="body2">{post.long_description}</Typography>
</Grid>
</Grid>
</DialogContent>
</Dialog>
</div>
)
}
export default PostDialog
解决方案
推荐阅读
- excel - 在 VBA 循环中使用多个变量在没有下一个错误的情况下遇到问题
- c# - 验证 Web api 中空值的表单数据
- jquery - 使用 JQuery onclick 切换反向视频
- flutter - 如何更改自定义可重用文本字段组件颤动中的focusedBorder颜色
- python - 如何从pytorch中给定位置的每一行获取值?
- sql - 我有一个查询,但它不起作用。我怎样才能正确地写它?
- asynchronous - Log4j2.xml - 队列大小和discardingThreshold
- flutter - 如何计算 querySnapshot 的总和?带有颤振/云火库
- angular - TestCafe 在登录过程中失去对弹出窗口的控制
- forms - 使用应用脚本将数据从一个谷歌表格发送到另一个谷歌表格