Home > Net >  how to put if conditional in specify object in JSX?
how to put if conditional in specify object in JSX?

Time:02-16

let say i have object

  const [product, setProduct] = useState({
    productName: '',
    quantity: '',
  });

how to make if conditional with specify objectname inside? my expextation is

if(product.productName != null && product.quantity!= null){
alert('its empty')
}else{
setProduct(bla bla bla)}

with this code if i put data in quantity but the other one (productname) is empty the code is always run else (if conditional) is there any advise to strict it? thank you please advise

CodePudding user response:

You have to go this way

if(!product.productName && !product.quantity){
alert('its not empty')
}else{
setProduct(bla bla bla)}

CodePudding user response:

Try This for checking if product is empty

  if (product.productName === "" && product.quantity === "") {
    alert("its empty");
  }else{
    setProduct({ ...product, productName: e.target.value });
  }

and Set you product like this

    setProduct({ ...product, productName: e.target.value });
  • Related