Reactjs 使用Typescript将React组件显示为弹出窗口
我对React和Typescript非常熟悉,我尝试在单击按钮时渲染以下组件Reactjs 使用Typescript将React组件显示为弹出窗口,reactjs,typescript,Reactjs,Typescript,我对React和Typescript非常熟悉,我尝试在单击按钮时渲染以下组件 const MyComponent = (props: ComponentProps) => { <GridContainer> <GridItem> <Table> <TableHead> <TableRow> <HeaderCell>
const MyComponent = (props: ComponentProps) => {
<GridContainer>
<GridItem>
<Table>
<TableHead>
<TableRow>
<HeaderCell>Attribute</HeaderCell>
<HeaderCell>Value</HeaderCell>
</TableRow>
</TableHead>
<TableBody>
<TableRow>
<DataCell>A</DataCell>
<DataCell>B</DataCell>
</TableBody>
</Table>
</GridItem>
</GridContainer>
);
};
constmycomponent=(props:ComponentProps)=>{
属性
价值
A.
B
);
};
我希望该组件在单击按钮时显示为弹出窗口,但我不知道如何使用Typescript实现这一点,需要帮助
<Button onClick={() => MyComponent }>Click</Button>
MyComponent}>单击
如果您能帮我解决这个问题,我将不胜感激。您可以使用“状态”将弹出窗口设置为在单击按钮时可见 在组件中创建一段状态
const MyConponent = () => {
//....
const [popupVisible, setPopupVisible] = useState<boolean>(false)
//....
function togglePopup() {
setPopupVisible(!popupVisible)
}
//....
}
//In the button, add the function
<Button onClick={togglePopup}>Click</Button>
constmyconpont=()=>{
//....
常量[popupVisible,setPopupVisible]=useState(false)
//....
函数togglePopup(){
setPopupVisible(!popupVisible)
}
//....
}
//在按钮中,添加函数
点击
您的问题并不完全清楚。“弹出”是指对话框还是模态?或者你的意思是只有点击后它才可见?无论哪种方式,通常的方法都是在状态中设置一个值,指示它是否可见,并基于该值进行渲染。如果你的意思是对话框/模式中的“弹出”,大多数库都会有一个“可见”属性。谢谢你,@Gabriel。如果我的按钮位于MyComponent之外,我将如何访问onClick中的状态和函数?@Lara将函数作为属性传递,就像任何其他解除状态一样。@Lara,如果它对您有帮助,请向上投票并接受该问题:),我将非常感激。