1

I'm trying to use a spinbox in my code but whenever I try changing the values using onChange, the app crashes. I also tried using onValueChange but it's getting ignored and I don't know what the problem is:

item.js

import NumericInput from "react-numeric-input";
import { Row, Col, ListGroup } from "react-bootstrap";

function ItemScreen() {
 
  const [qty, setQty] = useState(1);

  return (
    <div>
      <h2 className="text-center mb-3">Order</h2>
      <hr></hr>
      <Row>
        <Col md={8}>
          <ListGroup variant="flush">
            <ListGroup.Item>
              <Row>
                  <Col>Apple</Col>
                <Col>
                  <NumericInput
                    min={1}
                    max={100}
                    onChange={(e) => setQty(e.target.value)}
                    value={qty}
                  />
                </Col>
                <Col>
                  500 x {qty} = {qty * 500}
                </Col>
              </Row>
            </ListGroup.Item>
          </ListGroup>
        </Col>
      </Row>
    </div>
  );
}

export default ItemScreen;

3 Answers 3

2

The value will always be of type string.

So you need to cast it as a number.

onChange={(e) => setQty(Number(e.target.value))}
Sign up to request clarification or add additional context in comments.

1 Comment

It's still crashing with the error " Cannot read property 'value' of undefined "
0

the NumericInput component event just return the numeric value so you can this I guess :

<NumericInput
   min={1}
   max={100}
   onChange={(value) => setQty(value)}
   />

Comments

0

it works for me lik this :

onChange={setQty}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.