【问题标题】:React useState hook isn't updating state as desiredReact useState 钩子没有根据需要更新状态
【发布时间】:2020-05-15 13:42:14
【问题描述】:

当鼠标悬停在按钮上时,我正在尝试将按钮的背景颜色更改为黑色,如果不是,则将其更改回白色。我在字符串上使用了状态,该状态将变为黑色或白色,并将其传递给 style 属性。知道我哪里出错了吗?

谢谢。

import React, { useState } from "react";

function App() {
  const [headingText, setHeadingText] = useState("Hello");
  const [buttonColor, setButtonColor] = useState("White");    //setting the state

  function handleClick() {
    setHeadingText("Submitted");
  }

  function mouseOver() {
    setButtonColor("Black");             //changing state
  }
  function mouseOut() {
    setButtonColor("White");             //changing state
  }

  return (
    <div className="container">
      <h1>{headingText}</h1>
      <input type="text" placeholder="What's your name?" />
      <button
        style={{ backgroundColor: { buttonColor } }}    //where I want it to apply
        onClick={handleClick}
        onMouseOver={mouseOver}
        onMouseOut={mouseOut}
      >
        Submit
      </button>
    </div>
  );
}

【问题讨论】:

    标签: javascript reactjs react-hooks react-state


    【解决方案1】:

    您应该改用 onMouseEnter 和 onMouseLeave 事件。

    尝试如下:

    <button style={{ backgroundColor: buttonColor }}
            onClick={handleClick}
            onMouseEnter={mouseOver}
            onMouseLeave={mouseOut} >
    

    在Mouse Events 的 React 文档中进一步阅读。

    我希望这会有所帮助!

    【讨论】:

    • 我找到了错误的地方,我将 buttonColor 不必要地放在了大括号内。不管怎样,谢谢。 :)
    • 只是一个评论 - 或者可能是使用 useHover() 钩子的更好方法,查看这里:usehooks.com/useHover
    • @VineetPillai 哦,我在回答中自动更正了,但忘了提及,是的。 :D
    【解决方案2】:
    1. 您应该使用“去抖动”来延迟 mouseOver 事件的过程。可以参考链接:https://levelup.gitconnected.com/debounce-in-javascript-improve-your-applications-performance-5b01855e086
    2. Lo​​dash 库支持去抖动功能。使用方便:https://lodash.com/docs/4.17.15#debounce

    import React, { useState } from "react";

    import { debounce } from 'lodash';
    
    function App() {
      const [headingText, setHeadingText] = useState("Hello");
      const [buttonColor, setButtonColor] = useState("White");    //setting the state
    
      function handleClick() {
        setHeadingText("Submitted");
      }
    
      function mouseOver() {
        setButtonColor("Black");             //changing state
      }
      function mouseOut() {
        setButtonColor("White");             //changing state
      }
    
      return (
        <div className="container">
          <h1>{headingText}</h1>
          <input type="text" placeholder="What's your name?" />
          <button
            style={{ backgroundColor: { buttonColor } }}    //where I want it to apply
            onClick={handleClick}
            onMouseOver={debounce(mouseOver, 200)}
            onMouseOut={debounce(mouseOut, 200)}
          >
            Submit
          </button>
        </div>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2020-11-29
      相关资源
      最近更新 更多