Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function doesn't return true or false ReactJS

When I call my permissionCheck it doesn’t return true or false.
The permissions useState is full.

This is the part of my code:

const [permissions, setPermissions] = useState({});

const permissionCheck = (permission) =>
    {
        var i;
        for (i = 0; i < permissions.length; i++)
        {
            if (permissions[i].name === permission)
            {
                return false;
            } else if (permissions[i].name !== permission) return true;
        }
    }


// the outcome will be set on the hidden component. So if the outcome is true it will be hidden and if it is false it will be shown
hidden={permissionCheck('TEST')}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You try to iterate through an object which does not work

Change permissions from object

const [permissions, setPermissions] = useState({});

To array:

const [permissions, setPermissions] = useState([]);

And amend your function as follows:

const permissionCheck = (permission) =>
{
    var i;
    for (i = 0; i < permissions.length; i++)
    {
        if (permissions[i].name === permission)
        {
            return false;
        }
    }
   return true;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading