英文:
Why do the buttons open the same component instead of opening each button each component?
问题
Sure, here is the translated content:
父组件
export const Messages = () => {
const [showInfo, setShowInfo] = useState(false);
return (
<Stack>
<Button
onClick={() => setShowInfo(true)}
>
打开信息消息
</Button>
<InfoMessage open={showInfo} onClose={() => setShowInfo(false)} />
<Button
onClick={() => setShowInfo(true)}
>
打开成功消息
</Button>
<SuccessMessage open={showInfo} onClose={() => setShowInfo(false)} />
<Button
onClick={() => setShowInfo(true)}
>
打开错误消息
</Button>
<ErrorMessage open={showInfo} onClose={() => setShowInfo(false)} />
</Stack>
)
}
子组件(一个示例)
interface InfoMessageProps {
open: boolean;
onClose: () => void;
}
export const InfoMessage = ({ open, onClose }: InfoMessageProps) => {
const handleClose = (): void => {
onClose();
};
return (
<Snackbar
open={open}
autoHideDuration={4000}
anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
onClick={handleClose}
// TransitionComponent={transition}
>
<Alert
sx={{
width: '487px',
height: '104px',
paddingTop: '20px',
paddingLeft: '20px',
backgroundColor: '#FDFDFD',
}}
icon={false}
action={(
<IconButton
aria-label="close"
color="inherit"
size="small"
onClick={handleClose}
>
<CloseIcon fontSize="inherit" />
</IconButton>
)}
>
<AlertTitle sx={{ paddingRight:'80px' }}>
<Typography variant='headings.h4'>标题</Typography>
</AlertTitle>
<Typography variant='captions.default'>在这里插入消息</Typography>
</Alert>
</Snackbar>
);
};
请注意,我已经将代码中的 HTML 实体字符(如 <
和 >
)替换为相应的 JavaScript 标签(如 <
和 >
)以进行翻译。
英文:
I have one parent component with 3 buttons and 3 children components. Each button should open one component but with my code the 3 buttons open the same component when I click on the buttons.
The children components are in 3 different files and the parent component has one state. Can anyone tell me what's wrong in my code.
I pasted one child component example but they're 3 alerts.
Besides, my task for school specifies this: The state should have three values representing info, success and failed. How can I do it?
parent component
export const Messages = () => {
const [showInfo, setShowInfo] = useState(false);
return (
<Stack>
<Button
onClick={() => setShowInfo(true)}
>
open info message
</Button>
<InfoMessage open={showInfo} onClose={()=> setShowInfo(false)} />
<Button
onClick={() => setShowInfo(true)}
>
open success message
</Button>
<SuccessMessage open={showInfo} onClose={()=> setShowInfo(false)} />
<Button
onClick={()=> setShowInfo(true)}
>
open error message
</Button>
<ErrorMessage open={showInfo} onClose={() => setShowInfo(false)} />
</Stack>
)
}
Child component (one example)
interface InfoMessageProps {
open: boolean;
onClose: () => void;
}
export const InfoMessage = ({ open, onClose }: InfoMessageProps) => {
const handleClose = (): void => {
onClose();
};
return (
<Snackbar
open={open}
autoHideDuration={4000}
anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
onClick={handleClose}
// TransitionComponent={transition}
>
<Alert
sx={{
width: '487px',
height: '104px',
paddingTop: '20px',
paddingLeft: '20px',
backgroundColor: '#FDFDFD',
}}
icon={false}
action={(
<IconButton
aria-label="close"
color="inherit"
size="small"
onClick={handleClose}
>
<CloseIcon fontSize="inherit" />
</IconButton>
)}
>
<AlertTitle sx={{ paddingRight:'80px' }}>
<Typography variant='headings.h4'>title</Typography>
</AlertTitle>
<Typography variant='captions.default'>Insert message here</Typography>
</Alert>
</Snackbar>
);
};
答案1
得分: 1
我认为这可能是您的解决方案。
export const Messages = () => {
const [msgStatus, setMsgStatus] = useState("");
return (
<Stack>
<Button
onClick={() => setMsgStatus("info")}
>
打开信息消息
</Button>
<InfoMessage open={msgStatus === "info"} onClose={() => setMsgStatus("")} />
<Button
onClick={() => setMsgStatus("success")}
>
打开成功消息
</Button>
<SuccessMessage open={msgStatus === "success"} onClose={() => setMsgStatus("")} />
<Button
onClick={() => setMsgStatus("error")}
>
打开错误消息
</Button>
<ErrorMessage open={msgStatus === "error"} onClose={() => setMsgStatus("")} />
</Stack>
)
}
希望这能帮助您。
英文:
I think it could be your solution
export const Messages = () => {
const [msgStatus, setMsgStatus] = useState("");
return (
<Stack>
<Button
onClick={() => setMsgStatus("info")}
>
open info message
</Button>
<InfoMessage open={msgStatus === "info"} onClose={()=> setMsgStatus("")} />
<Button
onClick={() => setMsgStatus("success")}
>
open success message
</Button>
<SuccessMessage open={msgStatus === "success"} onClose={()=> setMsgStatus("")} />
<Button
onClick={()=> setMsgStatus("error")}
>
open error message
</Button>
<ErrorMessage open={msgStatus === "error"} onClose={() => setMsgStatus("")} />
</Stack>
)
}
答案2
得分: 0
因为你应该为每个组件使用不同的状态。发生这种情况是因为showInfo为true,每个组件都被渲染并重叠在一起。
编辑:
你可以更改你的onClick函数,并添加一个条件来渲染每个组件:
英文:
Because you should use different states for each component. What's happening is because showInfo is true, every component is being rendered and overlapping with each other.
EDIT:
You can change your onClick function and have a condition to render each component:
export const Messages = () => {
const [showInfo, setShowInfo] = useState(false);
const [conditionalState,setConditionState] =useState(false);
const setShow = (value) => {
setShowInfo(true);
setConditionState(value);
}
return (
<Stack>
<Button
onClick={() => {setShow(0)}
>
open info message
</Button>
{conditionalState === 0 &&
<InfoMessage open={showInfo} onClose={()=> setShowInfo(false)} />
}
<Button
onClick={() => {setShow(1)}
>
open success message
</Button>
{conditionalState === 1 &&
<SuccessMessage open={showInfo} onClose={()=> setShowInfo(false)} />
}
<Button
onClick={() => {setShow(2)}
>
open error message
</Button>
{conditionalState === 2 &&
<ErrorMessage open={showInfo} onClose={() => setShowInfo(false)} />
}
</Stack>
)
}
答案3
得分: 0
需要在您的代码中进行一个小的修正。您应该在parent.js
中为不同的警报状态创建不同的状态,就像这样:
export const Messages = () => {
const [showStatus, setShowStatus] = useState("");
return (
<Stack>
<Button
onClick={() => setShowStatus("info")}
>
打开信息消息
</Button>
<InfoMessage open={showStatus === "info"} onClose={() => setShowStatus("")} />
<Button
onClick={() => setShowStatus("success")}
>
打开成功消息
</Button>
<SuccessMessage open={showStatus === "success"} onClose={() => setShowStatus("")} />
<Button
onClick={() => setShowStatus("error")}
>
打开错误消息
</Button>
<ErrorMessage open={showStatus === "error"} onClose={() => setShowStatus("")} />
</Stack>
)
}
请注意,我只翻译了您提供的代码部分,没有添加其他内容。
英文:
A minor correction is needed in your code.
You should have different states for different alerts in your parent.js
like this :
export const Messages = () => {
const [showStatus, setShowStatus] = useState("");
return (
<Stack>
<Button
onClick={() => setShowStatus("info")}
>
open info message
</Button>
<InfoMessage open={showStatus=="info"} onClose={()=> setShowStatus("")} />
<Button
onClick={() => setShowStatus("success")}
>
open success message
</Button>
<SuccessMessage open={showStatus == "success"} onClose={()=> setShowStatus("")} />
<Button
onClick={()=> setShowStatus("error")}
>
open error message
</Button>
<ErrorMessage open={showStatus=="error"} onClose={() => setShowStatus("")} />
</Stack>
)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论