Shahariar Rahman Sajib
Shahariar Rahman Sajib

Reputation: 157

How to apply condiation in react?

 if ((newFile.type != "image/gif" ) || (newFile.type !=="image/jpg") || (newFile.type !=="image/png") (newFile.type !=="image/jpeg")) 
{ setFileErr(true) }

    else if((newFile.type == "image/gif") || (newFile.type === "image/jpg") || (newFile.type === "image/png") || (newFile.type === "image/jpeg")) 
{ setFileErr(false) }

I have used conditions like this. Only first condition newFile.type == "image/gif" is applying. Others are not working. Can anybody help?

Upvotes: 0

Views: 58

Answers (3)

RiTeSh
RiTeSh

Reputation: 521

A better way to manage this.

const allowedTypes = ["image/gif", "image/png", "image/jpeg"]
if (allowedTypes.includes(newFile.type)) {
  setFileErr(false)
} else {
  setFileErr(true)
}

Upvotes: 1

Cadmus
Cadmus

Reputation: 154

if ((newFile.type != "image/gif" ) || (newFile.type !=="image/jpg") || (newFile.type !=="image/png")  (newFile.type !=="image/jpeg")) 
{ setFileErr(true) }

You just miss a || at the last condition (newFile.type !=="image/png") || (newFile.type !=="image/jpeg")) and make sure newFile.type is a string so === can work (=== is can be equal if only both of value is the same type).

Upvotes: 1

looppool
looppool

Reputation: 9

You're using double equal signs for the correct condition, for the others you're using triple equal signs, which have a different meaning. Change them to == and != and your code should work.

Upvotes: -1

Related Questions