如何在 React - typescript - Styled-components 应用程序中使用单击事件修改 DOM?

方舟十三号

我有一个由 React、Typescript 和样式组件构建的应用程序(打字稿和样式组件的初学者)。我想创建一个简单的点击事件,在两个子组件中的哪一个在父组件内可见之间切换。你能帮我修复我的代码或建议另一种方法吗?

这是我在//评论中无法解决的部分的代码:

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>
  )
}
我是L

您可以在组件本身上使用状态和条件状态:

import React, { useState } from 'react';

export const myCard: React.FC<myProps> = (props) => {
  const [flipSwitch, setFlipSwitch] = useState(false)

  return (
    <Parent onClick={e => setFlipSwitch(!flipSwitch)}>
      
     {flipSwitch &&
       <Child1>
          <GrandChild>{props.whatever}</GrandChild>
          <GrandChild2>{props.whatever}</GrandChild2>
       </Child1>
     }

    {!flipSwitch &&
      <Child2>
          <GrandChild3>{props.whatever}</GrandChild3>
          <GrandChild4>{props.whatever}</GrandChild4>
      </Child2>
    }

    </Parent>
  )
}

我真的不知道应该在您的代码中显示哪一个,但请随意玩玩 ^^

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Styled-components vs Emotion - 如何在 Styled-components 上重新应用 css`style` 函数

如何在 Gatsby 2.0 中使用 styled-components 主题

如何在React上的Styled Component画布上正确使用TypeScript类型

如何在Meteor环境中使用React,Material UI和Styled-components管理服务器端渲染?

React Styled Components - 如何更改地图功能中单击元素的颜色?

导入 .less 变量以在 React Styled Components 中使用

使用 React Styled Components,如何根据通过 useSelector 收集的状态声明颜色?

React Styled Components-如何访问原始HTML

如何在React Styled-Components中设置子组件的样式

如何在 React Styled Components 中仅为一个道具制作多种样式?

如何在带有 TypeScript 的 React 应用程序中使用消费者?

如何在我的Typescript和React应用程序中使用@ types / bootstrap

如何在以TypeScript(TSX)编写的React应用程序中使用Facebook Relay?

React + Typescript + Styled Components 将道具传递给关键帧和条件

如何在 Typescript 中从 react-router-dom 键入 StaticRouterContext?

如何使用 styled-components 更改组件的颜色?

React Styled-Components 传递道具问题

如何在TypeScript中使用React 15.5?

如何在 React TypeScript 中使用 SignaturePad

如何在Typescript中使用React Native?

如何在 Typescript 中使用 React HOC

在新的React版本中,如何修复TypeError:styled_components__WEBPACK_IMPORTED_MODULE_3__.default.dev?

React Styled-Components-如何根据道具条件或伪类渲染样式?

React + Styled Components-分离并重复使用CSS“捆绑包”

使用React Styled Components在两种类型的组件之间共享相同的样式

使用状态来自定义样式的react + styled-components

将 calc() 与 react-native 和 styled-components 一起使用时出错

如何在 React 中使用 useRef 获取精确的 DOM 元素

如何在React Router Dom中使用嵌套路由