Function doesn't return true or false ReactJS

Advertisements

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')}

>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;
}

Leave a ReplyCancel reply