score:0

Accepted answer

i've noticed a few things here:

1) you have some repetitive code -> filteredemployee[math.floor(math.random() * filteredemployee.length)] it would be a good idea to abstract it out. you could do this like:

function getrandomindex(dataarray) => dataarray[math.floor(math.random() * dataarray.length)]

then you could just call the function like: const randomoption = this.getrandomindex(filteredemployee)

2) for your setstate to work, it depends on where this method of yours is located. is it inside of the component that is handling the state? if it's not, one option is to have your getrandomemployee simply return the object you need and have the component invoking it setstate instead.

score:1

i would make this function pure and use it when you need to generate these random names, e.g.

class somecomponent extends component {
    handleclick = () => {
        const {random, randomoptions} = getrandomemployee()
        this.setstate({
           randomoptions,
           random,
        })
    }
}


Related Query

More Query from same tag