【问题标题】:Mouse click and enter keyboard disable after one click in React在 React 中单击后鼠标单击并输入键盘禁用
【发布时间】:2021-04-30 07:56:13
【问题描述】:

我正在尝试做的事情:我想做到这一点,当您用鼠标单击按钮或从键盘输入时,您以后无法再次单击它。也就是说,按一次按钮后禁用它。

用鼠标第二次点击有效,但我无法让它与 enter 一起工作。

我的代码如下。

function HomeFood() {
    const [food, setFood] = useState(null);
    const [calories, setCalories] = useState('');
    const [disabled, setDisabled] = useState(false);

    async function foodData() {
        const result = await axios.get(`link`);
        console.log('DATA RESULTS:', result.data);
        setFood(result.data)
    }

    function handleChange(e) {
        setCalories(e.target.value);
    }

    function handleKeyPress(e) {
        if (e.key === 'Enter') {
            foodData()
        }
    }

    return (
        <div className="container">
            <section className='food'>
                <input
                    type='number'
                    onChange={handleChange}
                    onKeyPress={handleKeyPress}
                />
                <button
                    onClick={() => {
                        foodData();
                        setDisabled(true);
                    }}
                    disabled={disabled}
                >
                    Click here
                </button>
            </section>
            {food && <FoodNutrients mealsData={food}/>}
        </div>
    );
}

我尝试在 onKeyPress 函数中使用禁用状态,但只得到错误。我也试过把它放在onKeyPress的输入中,但又出现了更多错误。

【问题讨论】:

    标签: reactjs click enter mouseclick-event disable


    【解决方案1】:

    就像onClick一样,在handleKeyPress中调用setDisabled

    function handleKeyPress(e) {
        if (e.key === 'Enter') {
    
           if (disabled) { // do nothing if already disabled
               return;
            }
    
            foodData();
            setDisabled(true);
        }
    }
    

    【讨论】:

    • 是的,就是这个!现在,当我使用两者之一时,我也无法使用 enter 或 mouse。谢谢!
    【解决方案2】:

    添加第二个禁用状态,即inputDisabled,并以相同方式切换它。这允许彼此独立地禁用。

    function HomeFood() {
      const [food, setFood] = useState(null);
      const [calories, setCalories] = useState('');
      const [disabled, setDisabled] = useState(false);
      const [inputDisabled, setInputDisabled] = useState(false); // <-- new state
    
      async function foodData() {
          const result = await axios.get(`link`);
          console.log('DATA RESULTS:', result.data);
          setFood(result.data)
      }
    
      function handleChange(e) {
          setCalories(e.target.value);
      }
    
      function handleKeyPress(e) {
          if (e.key === 'Enter') {
              foodData();
              setInputDisabled(true); // <-- set input disabled
          }
      }
    
      return (
          <div className="container">
              <section className='food'>
                  <input
                      type='number'
                      onChange={handleChange}
                      onKeyPress={handleKeyPress}
                      disabled={inputDisabled} // <-- attach disabled prop
                  />
                  <button
                      onClick={() => {
                          foodData();
                          setDisabled(true);
                      }}
                      disabled={disabled}
                  >
                      Click here
                  </button>
              </section>
              {food && <FoodNutrients mealsData={food}/>}
          </div>
      );
    }
    

    如果您希望同时禁用它们,那么只需为两者使用相同的 disabled 状态和设置器。

    【讨论】:

    • 天哪,我真是太愚蠢了,我什至没有想过尝试另一种状态。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多