简体   繁体   English

如果我使用模板文字在 React 中切换 CSS 类,按钮将不起作用

[英]Button doesn't work if I use template literals to toggle CSS classes in React

I have a CSS module called styles and this React code.我有一个名为 styles 的 CSS 模块和这个 React 代码。 It's a div with two buttons inside.这是一个内部有两个按钮的 div。 I use the isMenuActive state to verify if the menu is active or not.我使用 isMenuActive state 来验证菜单是否处于活动状态。 If it is active, the CSS class 'active' gets in and the menu appears, otherwise not.如果它处于活动状态,则 CSS class 'active' 进入并显示菜单,否则不显示。

  <div className={`${styles.customerOptionsMenu} ${isMenuActive ? styles.active : null}`}>
    <button onClick={() => { console.log('hi') }}>
      <span className="material-icons">edit</span>Editar
    </button>
    <button onClick={() => {console.log('hi')}}>
      <span className="material-icons">delete</span>Deletar
    </button>
  </div>

When I click the buttons, nothing happens.当我单击按钮时,没有任何反应。

But If I store the button as a global variable in developer tools and run button.click() it works fine if I remove the template literals:但是,如果我将按钮存储为开发人员工具中的全局变量并运行 button.click() 如果我删除模板文字它工作正常:

  <div className={styles.customerOptionsMenu + styles.active}>
    <button onClick={() => { console.log('hi') }}>
      <span className="material-icons">edit</span>Editar
    </button>
    <button onClick={() => {console.log('hi')}}>
      <span className="material-icons">delete</span>Deletar
    </button>
  </div>

It works fine.它工作正常。

Why???为什么??? And what should I do to keep changing the classes when isMenuActive changes?当 isMenuActive 发生变化时,我应该怎么做才能继续更改类?

Edit: Full code with the button that changes isMenuActive编辑:带有更改 isMenuActive 按钮的完整代码

  const [isMenuActive, setIsMenuActive] = useState(false)
  const onBlur = () => { setIsMenuActive(!isMenuActive)}

  return(
    <td>
      <button onBlur={onBlur} className={styles.customerOptions} onClick={() => setIsMenuActive(!isMenuActive)}>
        <span className="material-icons">more_horiz</span>
      </button>
      <div className={`${styles.customerOptionsMenu} ${isMenuActive ? styles.active : null}`}>
        <button onClick={() => { console.log('hi') }}>
          <span className="material-icons">edit</span>Editar
        </button>
        <button onClick={() => {console.log('hi')}}>
          <span className="material-icons">delete</span>Deletar
        </button>
      </div>
    </td>
  )

New edit: The answer is in the comments by Pandaiolo.新编辑:答案在 Pandaiolo 的评论中。 The problem was the onBlur={onBlur} code, when I removed it from the button everything worked fine!问题是 onBlur={onBlur} 代码,当我从按钮中删除它时,一切正常!

To handle changing classnames you can use the classnames package to do something like this:要处理更改类名,您可以使用名 package 执行以下操作:

import cx from "classnames";

<div className={cx(styles.customerOptionsMenu, { styles["active"]: isMenuActive })}>
    <button onClick={() => { console.log('hi') }}>
      <span className="material-icons">edit</span>Editar
    </button>
    <button onClick={() => {console.log('hi')}}>
      <span className="material-icons">delete</span>Deletar
    </button>
</div>

The isMenuActive is not defined. isMenuActive 没有定义。 Place it in as a parameter as so:将其作为参数放入,如下所示:

export default function App(isMenuActive) {
  return (
    <div className="App">
  <div className={`${styles.customerOptionsMenu} ${isMenuActive ? styles.active : null}`}>
    <button onClick={() => { console.log('hi') }}>
      <span className="material-icons">edit</span>Editar
    </button>
    <button onClick={() => {console.log('hi')}}>
      <span className="material-icons">delete</span>Deletar
    </button>
  </div>
    </div>
  );
}

I think the space is fine in your template literal, because it references two different class names.我认为您的模板文字中的空格很好,因为它引用了两个不同的 class 名称。 You probably want to use ${isMenuActive? styles.active:''}您可能想使用${isMenuActive? styles.active:''} ${isMenuActive? styles.active:''} otherwise null becomes the string "null" , which is probably harmless unless you have a class .null that applies some styles, but that is basically not what you want. ${isMenuActive? styles.active:''} otherwise null becomes the string "null" , which is probably harmless unless you have a class .null that applies some styles, but that is basically not what you want.

But maybe the onBlur is called after the click?但也许 onBlur 在点击后被调用?

  1. click点击
  2. button becomes focus按钮成为焦点
  3. button blurs?按钮模糊?

Not sure.没有把握。 But in that case it would toggle the state two times, cancelling its effect.但在这种情况下,它会切换 state 两次,从而取消其效果。 Maybe try with just the onClick and without the onBlur at first?也许一开始只尝试onClick而没有onBlur

And you can add a console.log('isMenuActive', isMenuActive) before the return statement to see it's value along rerenders, see if it matches what you expect.您可以在 return 语句之前添加一个console.log('isMenuActive', isMenuActive)以查看它在重新渲染中的值,看看它是否符合您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM