2 回答
TA贡献2065条经验 获得超13个赞
import React from 'react'
function App({ callback }) {
// if we just mind about the active dialog,
// we don't need to save the state of the others
const [dialog, setDialog] = React.useState(null)
// here useEffect is called every time, dialog changed,
// we also need to put `callback` in the array otherwise
// React throw a warning.
// React.useEffect act like `ComponentDidMount`, so we better
// check dialog is not empty otherwise it will call our callback
// straight away AFTER the render and that's not what we want
React.useEffect(() => {
if (dialog) callback()
}, [dialog, callback])
// You said we were in a navBar so i can assume you can do
// something like this
return (
<div>
{['signUp', 'signInDialog', 'settingsDialog', 'signOutDialog'].map(
elem => (
<div key={elem} onClick={() => setDialog({ [elem]: true })}>
{elem}
</div>
),
)}
</div>
)
}
export default App
TA贡献1804条经验 获得超3个赞
const [dialog, setDialog] = useState({
isOpensignUp: false,
isOpenSignIn: false,
isOpenSignOut: false,
isOpenSetting: false
});
const handleDialog = e => {
setDialog({...dialog,[e.target.name]:true});
}
<button name= "isOpensignUp" onClick={handleDialog} />
<Dialog open={dialog.isOpensignUp}>
添加回答
举报