【问题标题】:How can I disable the button at the end of this component?如何禁用此组件末尾的按钮?
【发布时间】:2022-01-05 14:12:44
【问题描述】:
import { useRef, useState } from 'react'    
import Input from '../../UI/Input'

import classes from './MealItemForm.module.css'

export default function MealItemForm(props) {
  const [amountIsValid, setAmountIsValid] = useState(true)

  const amountInputRef = useRef()

  const submitHandler = event => {
    event.preventDefault()

    const enteredAmount = amountInputRef.current.value
    const enteredAmountNumber = +enteredAmount

    if (enteredAmount.trim().length === 0 || enteredAmountNumber < 1 || enteredAmountNumber > 20) {
      setAmountIsValid(false)
      return
    }

    props.onAddToCart(enteredAmountNumber)
  }

尝试在 submitHandler 后添加一个函数,当输入的数量 > 20 时禁用按钮,但无法使其工作!

  return (
    <form className={classes.form} onSubmit={submitHandler}>
      <Input
        ref={amountInputRef}
        label='Amount'
        input={{
          id: 'amount_' + props.id,
          type: 'number',
          min: '1',
          max: '20',
          step: '1',
          defaultValue: '1'
        }}
      />
      {/* Two curly braces: one for embedding javascript expression and the other one is there because the value is object */}

我想在enteredAmount > 20时禁用这个按钮,请帮帮我!

      <button>+ Add</button>

      {!amountIsValid && <p>Please enter a valid amount(1~20)</p>}
    </form>
  )
}

【问题讨论】:

  • 添加属性disabled={enteredAmount&gt;20} to the button?
  • 添加一个useState,用于跟踪输入的金额,并为按钮添加一个属性disabled={enteredAmount &gt; 20}

标签: javascript reactjs button use-ref disabled-control


【解决方案1】:

你需要使用buttondisabled属性

<button disabled={enteredAmountNumber > 20}>Enter Amount</button>

基于您的评论的代码

import { useRef, useState } from 'react'    
import Input from '../../UI/Input'

import classes from './MealItemForm.module.css'

export default function MealItemForm(props) {
  const [amountIsValid, setAmountIsValid] = useState(true)

  const amountInputRef = useRef()

  const submitHandler = event => {
    event.preventDefault()

    const enteredAmount = amountInputRef.current.value
    const enteredAmountNumber = +enteredAmount

    if (enteredAmount.trim().length === 0 || enteredAmountNumber < 1 || enteredAmountNumber > 20) {
      setAmountIsValid(false)
      return
    }

    props.onAddToCart(enteredAmountNumber)
  }

return (
    <form className={classes.form} onSubmit={submitHandler}>
      <Input
        ref={amountInputRef}
        label='Amount'
        input={{
          id: 'amount_' + props.id,
          type: 'number',
          min: '1',
          max: '20',
          step: '1',
          defaultValue: '1'
        }}
      />
      {/* Two curly braces: one for embedding javascript expression and the other one is there because the value is object */}

   <button disabled={!amountIsValid}>+ Add</button>

      {!amountIsValid && <p>Please enter a valid amount(1~20)</p>}
    </form>
  )
}

根据您的评论进行更新

工作演示

import "./styles.css";
import react, { useState } from "react";

export default function App() {
  const [clickCount, setClickCount] = useState(0);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <button
        disabled={clickCount >= 20}
        onClick={() => {
          setClickCount((clickCount) => clickCount + 1);
        }}
      >
        {clickCount === 0 ? `Click Me` : `You clicked ${clickCount} times`}{" "}
      </button>
    </div>
  );
}

【讨论】:

  • 它给出了 --> 未定义输入的金额
  • 您需要定义该变量。或者在你的情况下它将是enteredAmountNumber。我已经在我的代码中改变了它。
  • 是的,我试过enteredAmountNumber,但这不起作用,因为它是在submitHandler方法中定义的,也许这就是为什么按钮没有进入enterAmountNumber
  • 如果我将 amountInputRef.current.value 提取到一个新变量中,然后将其设为数字​​并给它一个名称值,那么也会给出此错误 TypeError: Cannot read properties of undefined (reading 'value' )
  • 我已经更新了代码。请检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多