score:0
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,
})
}
}
Source: stackoverflow.com
Related Query
- Refactoring to make a function reusable with setState
- How to test useEffect with async function and setState inside
- React js single function with multiple setstate
- How can I create a reusable function that will setState for a variable object property?
- I fail to setState my ReactJS state dynamically with a function
- How can i reusable function with boolean state in class component
- Component not mounted when setState called in arrow function in React with ES2016
- How can I make tx.executeSql() function sync with async/await?
- Reuse a function with setState in several components
- How to execute a function AFTER an API-Call was made with setState within useEffect?
- How do I make a reusable radio button with reactJS?
- How to make my function more maintainable with Object.entries method in React
- Uncaught TypeError: Cannot read property 'setDomLayout' of undefined - Error when trying to make print function work with AG-Grid
- How can I make this function shorter with ES6?
- React init state array with init function that repeatedly calls setState
- How to make 3 column grid with map function react
- How to fire a setState function in react from child componenet to parent componenet with data from a button click
- Using setState in .forEach loop will only run callback function with most recent state?
- Is there a way to make searching function by URL with Online API
- Refactoring Function in React with Parameters
- How to make a reusable component with proper, testable event handlers in ReactJS
- Is there a way to make reusable modal with action buttons with Redux?
- How to make a reusable function that calls setUserContext? React Hooks useContext
- Refactoring a React function with repeated if else blocs
- How to synchronize setState with another function
- problem with getDerivedStateFromProps when child component make setstate in parent
- React - how to make only one increment function setState multiple variables?
- How to make this setState function non recursive
- Best practice using an arrow function in a React class component with setState
- Refactoring setState with callback for functional component
More Query from same tag
- Modifying each value of array with immutability helper
- How to use "await" for a generator function is ES6?
- Dynamic routing issue
- How to map through an object with array of object
- Stuck at "Cannot read property 'props' of undefined"
- Why do we add eventListeners in useEffect hook
- how to render an array of objects in React
- Why is my React checkbox onChange handler firing on render and then not when the box is clicked?
- How to disable undo (only from keyboard shortcut) in TinyMCE v5?
- How do I get the outerHTML of a cloned element?
- How to add zIndex in typescript
- Why won't my require() inside my img work correctly while using it with Bulma?
- When Reloading by Browser[F5] then,a state always will be undefined
- Hover in ReactJS
- Why isn't Disqus Universal Code working?
- How to load different components on a static URL
- Cant seem to access object property in array
- Checking if !isLoading and state exists with useTracker meteor subscribe and findOne mongo
- Why am I getting 'Each child in a list should have a unique "key" prop error' When I have a key assigned?
- How to load a specific class name on only first active UI react
- How to send props to state other components in React Hooks
- Data has stopped returning after I added a debounce
- React-Redux - passing down props before async data arrives
- How to create and deploy a React App properly?
- MongoDB insert data to an existing user in NodeJS
- React with Axios, for the response I got from a GET, I want to use another GET, I want to sum up values from certain fields
- How To Render Multiple 3D of Same 3D Model;
- Unable to debug React.js code in visual studio code
- Login Method in Django works fine with POSTMAN but with front end everything works except Login Method
- How to pass props to components when you use <Route components={} > attribute?