【发布时间】:2021-03-01 07:31:09
【问题描述】:
我有一个由 React、Typescript 和样式化组件构建的应用程序(初学者使用 typescript 和样式化组件)。我想创建一个简单的单击事件,在父组件中可见两个子组件中的哪一个之间切换。你能帮我修复我的代码或建议一种替代方法吗?
这是我在 //comment 中无法解决的部分的想法:
let flipSwitch: boolean = false;
export const myCard: React.FC<myProps> = (props) => {
return (
<Parent onClick={e => {
if (!flipSwitch) {
// dontn't know how to write this part:
//e.get Child1 element.style.display = 'none';
//e.get Child2 element.style.display = 'none';
flipSwitch = true;
} else {
//e.get Child2 element.style.display = 'none';
//e.get Child1 element.style.display = 'block';
flipSwitch = false;
}
}
}>
<Child1>
<GrandChild>{props.whatever}</GrandChild>
<GrandChild2>{props.whatever}</GrandChild2>
</Child1>
<Child2>
<GrandChild3>{props.whatever}</GrandChild3>
<GrandChild4>{props.whatever}</GrandChild4>
</Child2>
</Parent>
)
}
【问题讨论】:
标签: reactjs typescript onclick dom-events styled-components