how can we check if condition in reactjs - reactjs

handleClick=()=>{
console.log("final course values"+ " "+this.state.selectCourseValue)
console.log("selected teacher"+ " "+this.state.selectedTeacher)
if(this.state.selectCourseValue ===null && this.state.selectedTeacher !== undefined)
{
console.log("please select")
message.warning('Add course')
return;
}
else{
console.log("Sucessfully updated")
}
}

If selectCourseValue is an array, then your condition should be probably this:
if (this.state.selectedTeacher && (this.state.selectCourseValue || []).length === 0) {
}
Checking that selectedTeacher is not empty and selectCourseValue is empty.

Related

If/Else statement not working, giving undefined

I'm working on a project, and I need the English version of countries' names. I'm trying to have an if/else statement that will give the English spelling when the non-English spelling of a country is given, but it's not working. I would really appreciate any help or advice on why I am having this issue. Thank you!
let countryName;
if (country === 'Shqipëria' || 'hqipëria') {
let countryName ='Albania';
}
else if (country === 'Hayastán' || 'hayastán') {
let countryName ='Armenia';
}
...
else if (country === 'Italia' || 'italia') {
let countryName ='Italy';
}
...
else if (country === 'Türkiye' || 'türkiye') {
let countryName = 'Turkey';
}
else if (country === 'Ukraїna' || 'Україна' || 'ukraїna' || 'yкраїна') {
let countryName = 'Ukraine';
}
else {
let countryName = country;
}
console.log(countryName)
console.log(country)
console.log(country) gives Italia, but console.log(countryName) is undefined.
I have also tried doing this:
const [countryName, setCountryName] = useState('')
if (country === 'Shqipëria' || 'hqipëria') {
setCountryName('Albania');
}
else if (country === 'Hayastán' || 'hayastán') {
setCountryName('Armenia');
}
...
else if (country === 'Italia' || 'italia') {
setCountryName('Italy');
}
...
else if (country === 'Türkiye' || 'türkiye') {
setCountryName('Turkey');
}
else if (country === 'Ukraїna' || 'Україна' || 'ukraїna' || 'yкраїна') {
setCountryName('Ukraine');
}
else {
setCountryName(country);
}
console.log(countryName)
console.log(country)
But this was giving me an error stating Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.
as mentioned in other answers your conditions only contain one equality check, so it wold always return true because a string's value by-itself is truthy, so you never get passed the first condition. for simplifying if statements, you can use the includes method. for example:
if (['Ukraїna','Україна','ukraїna','yкраїна'].includes(country))
and definitely declare country variable once & re-assign it in every condition.
let countryName;
if (...) {
countryName = "x"
}
Cancerning your first code: countryname shouldn't be declared many times
so your code should be like this:
let countryName;
if (country === 'Shqipëria' || country === 'hqipëria') {
countryName ='Albania';
}
else if (country === 'Hayastán' || country === 'hayastán') {
countryName ='Armenia';
}
else if (country === 'Italia' || country === 'italia') {
countryName ='Italy';
}
else if (country === 'Türkiye' || country === 'türkiye') {
countryName = 'Turkey';
}
else if (country === 'Ukraїna' || country === 'Україна' || country === 'ukraїna' || country === 'yкраїна') {
countryName = 'Ukraine';
}
else {
countryName = country;
}
console.log(countryName);
console.log(country);
Also, you should use
if(variable === 'value' || variable === 'value')
instead of
if (variable === 'value' || 'value')

hi i have a problem whit my handleSubmit in react js

I want to submit some data but if the textarea (name) is empty or null i want to use the value of the select to search, but for some reason my if/else doesn't work and always search using the name even if its empty and this gives an error beaacause it's empty
e.preventDefault();
if (name != null || name !== '') {
try {
setNameIntroduced(true);
let cardFound = completeArrayCards.filter((e) => e.name.toLowerCase() === name.toLowerCase());
setCardFoundIndividual(cardFound);
let arrayImages = Object.keys(cardFound[0].image)
.map(function (key) {
return cardFound[0].image[key];
});
if (cardFound[0].name !== null) {
setFound(true);
setName(cardFound[0].name);
setType(cardFound[0].type);
setImage(arrayImages[1]);
setAttribute(cardFound[0].attribute || cardFound[0].type);
setDescription(cardFound[0].desc);
setLevel(cardFound[0].level);
setRace(cardFound[0].race);
setAtk(cardFound[0].atk);
setDef(cardFound[0].def);
setCardsets(cardFound[0].card_sets);
setCardPrices(Object.keys(cardFound[0].card_prices)
.map(function (key) {
return cardFound[0].card_prices[key];
}));
if (type === 'Spell Card' || type === 'Trap Card') setIsMagicTrap(true)
}
} catch (err) {
console.log(err);
setFound(false);
}
} else if (name == null && type != null) {
try {
setMultipleResults(true);
setFound(true);
setNameIntroduced(false);
let cardsFound = completeArrayCards.filter((e) => e.type === type);
//console.log(cardsFound);
console.log('Multiple found: ' + cardsFound[0].name + '\n' + cardsFound[0].type + '\n' + cardsFound[0].image.image_url);
} catch (err) {
console.log(err);
setFound(false);
}
}
}```

ReactJS - Validate Nested-IF/ELSE Statement

In my react app I'm using IF statement inside another IF statement to validate a condition that I want to get data filtered only from a specific column not from all.
Basically I'm using AntD table in which I'm displaying students data. In a table I do have few columns namely Age, Marks and Attendance. On each column(Age, Marks and Attendance) I'm applying a range filter with aim to fetch student(s) when applied search on a specific column i.e. either Age, Marks or on Attendance.
The problem I'm getting that my Nested-IF condition is not validating to true. Rest of code is working fine. I also tried to run code without Nested-IF(I mean with the simple IF/ELSE statement). That is also working fine.
I don't know what did I wrong or what I'm missing/skipping in my attempt.
Help me to validate my Nested-IF/ELSE statement(s).
What I'm trying is:
const handleSearch = (firstInput, secondInput, dataIndex) => {
setSearchStu(
student.filter((obj) => {
if (dataIndex === "age") {
if (obj.age >= firstInput && obj.age <= secondInput) {
return true;
} else {
return false;
}
} else {
if (dataIndex === "marks") {
if (obj.marks >= firstInput && obj.marks <= secondInput) {
return true;
} else {
return false;
}
} else {
if (dataIndex === "attendance") {
if (obj.attendance >= firstInput && obj.attendance <= secondInput) {
return true;
} else {
return false;
}
} else {
return false;
}
}
}
})
);
};
Where
firstInput is the 1st input field in a range filter
secondInput is the 2nd input field in a range filter
dataIndex is like a key to validate my 1st IF statement in Nested-IF case
Values "age", "marks" and "attendance" are the defined values which are uniquely correspond to the each columns(Age, Marks and Attendance) in the table
and so on..
Thank you in advance for your assistance.
to simplfy your code, you can try this :
const handleSearch = (firstInput, secondInput, dataIndex) => {
setSearchStu(
student.filter((obj) => {
if (dataIndex === "age") {
return (obj.age >= firstInput && obj.age <= secondInput)
} else {
if (dataIndex === "marks") {
return (obj.marks >= firstInput && obj.marks <= secondInput)
} else {
if (dataIndex === "attendance") {
return (obj.attendance >= firstInput && obj.attendance <= secondInput)
} else {
return false;
}
}
}
})
);
};
or like this :
switch(dataIndex){
case "age" :
return (obj.age >= firstInput && obj.age <= secondInput)
break;
case "marks" :
return (obj.marks >= firstInput && obj.marks <= secondInput)
break;
case "attendance" :
return (obj.attendance >= firstInput && obj.attendance <= secondInput)
break;
default :
return false
}
The function does work given the right inputs and data - I made a sandbox on replit to verify. Possibly there is an issue with the student data (consider renaming to students) or with the inputs to the function. Also, the function can be simplified to the following (see replit sandbox):
const handleSearch = (firstInput, secondInput, dataIndex) => {
return (
student.filter((obj) => {
if (["age", "marks", "attendance"].includes(dataIndex)) {
return obj[dataIndex] >= firstInput && obj[dataIndex] <= secondInput
}
return false
})
);
};

equivalent of following code in angular 2

I'm having trouble updating this code from angularjs ad angular 2, thank you for any help
if (settings != null && settings.checkSubscriptionForVip === true) {
return this.user.hasVipAccess().then(function(hasVipAccess) {
hasVipAccess ? deferred.resolve() : deferred.reject("NO_VIP");
return;
});
} else {
deferred.resolve();
return;
}
You neeed to have a boolean variable defined in your component and assign the result in to that,
hasVipAccess : boolean = false;
if (settings != null && settings.checkSubscriptionForVip === true) {
this.user.hasVipAccess().then(function(access) {
this.hasVipAccess = access;
});
} else {
this.hasVipAccess = access;
}
However if you do not need to set boolean variable, just return the result

php for loops how to start again after else

I want to do something like this. Do you have ideas for this?
=== a ===
if( Something ){
=== b ===
{
else
{
go back to === a ===
}
Thanks.
It is usual while loop.
$something = false;
while (!$something) {
//here are ====a==== actions
}
//here are b actions
You want something like this:
while (true) {
if( Something ){
=== b ===
}
else
{
=== a ===
}
if (we're done) {
break;
}
}
Alternatively, you could use a proper condition in your while loop to exit.

Resources