score:1
Accepted answer
the generaterandomstock
function creates a closure with initial data
value. that's always an empty object {}
. you can access the data
received to the callback function to `setdata method where it gives updated values.
try like below
const generaterandomstock = () => {
const randnumber = math.floor(math.random() * 9 + 1).tostring();
setdata((prevdata) => {
console.log(prevdata);
if (prevdata.hasownproperty(randnumber)) {
return {
...prevdata,
[randnumber]: prevdata[randnumber] + 1
};
} else {
console.log("does not have");
return {
...prevdata,
[randnumber]: 1
};
}
});
};
score:0
you need to add generaterandomstock
function as a dependency in useeffect
hook.
since this function uses the data
value, its content is updated when the data
value is updated.
const [data, setdata] = usestate({});
//generates a random number and updates the state
const generaterandomstock = () => {
const randnumber = math.floor(math.random() * 9 + 1).tostring();
console.log(data);
if (data.hasownproperty(randnumber)) {
console.log("has");
setdata((prevstate) => ({
...prevstate,
[randnumber]: prevstate[randnumber] + 1
}));
} else {
console.log("does not have");
setdata((prevstate) => ({
...prevstate,
[randnumber]: 1
}));
}
};
useeffect(() => {
//interval to generate random number every 2 seconds
const interval = setinterval(() => {
generaterandomstock();
}, 2000);
return () => {
clearinterval(interval);
};
}, [generaterandomstock]);
Source: stackoverflow.com
Related Query
- Update object every second with useState hook
- React efficiently update object in array with useState hook
- update an object with useState hook in React
- How to update useState hook with Object which gets an array? with typescript
- React js useState hook. How to update state of a json object with an a array in it when a checkbox is clicked
- update the array of object using useState hook
- How to update state with the useState hook
- Updating object with useState Hook after rendering text input
- Update an array inside an object with unique value in react with hook
- update scroll position every 8 seconds with useState and useEffect?
- How do I update 2 objects with React Hook useState
- Change value, add and remove fields of deep nested object with useState hook in react
- How to update an existing array of objects (add a new object to the array) with the useState hook?
- How to make a custom hook with useState which can update multiple elements if state changes?
- How to update nested object having array with useState in Reactjs?
- Usestate hook with object arrays
- modify and insert new object in array of objects with useState hook
- Append a item to an array that is inside a object with useState Hook
- React useState with object update several states in object
- Unable to update a list with react when its value is being changed using the hook useState function from React TypeError: map is not a function
- React useState hook - update state of a object
- Updating an object with useState hook
- How to update value of a key in object in an array with the new value in React useState in TypeScript
- react useState update not triggering with custom hook
- Update value in object array with useState
- How to update object state using useState hook
- Array does not update with useState hook and for loop
- React - update object using useState hook
- Maximum update depth exceeded with useReducer in custom hook but not with useState
- I want an onClick button to update my state with a random object from array on every click
More Query from same tag
- Unable to access React Application from Minikube
- React input loses focus after each keystroke
- TypeError: Cannot read property 'searchField' of null
- Getting the state with rematch
- Blinking Modal with Semantic UI React
- push dynamically coming id's and value's in javascript
- Div does not follow Mouse due to parent div
- How to use setState with nested JSON array object
- How to initate post request in React without mounting a form?
- setting the state at the beginning
- Uploading Images in Browser using react issue
- Hey i am very new in react i have to do a form validation using 'onChange' and if valid then only state should be change.How to do that in right way?
- React MUI components (Slider/Select) "onChange" doesn't change value immediately (provide result of previous click)
- How to sum values by grouping by account/id/category?
- Typescript React TS2307 cannot find module json
- useState hook updating without using the set method
- Memory leak error while calling fetch API in react
- I need help (Api calls in React Js Hooks) Why is this nort working?
- Cannot convert object to primitive value error in react application?
- Can the vanilla JS date object be used normally in React.js?
- jest.requireActual is not a function
- Material-UI changing IMG based on breakpoint
- Feed multiple parameters from node to mssql using node-mssql and stored procedures
- Component written as a class not rendering
- Cors Issue with React Frontend and Django Backend
- React test componentWillReceiveProps using enzyme
- How to get the value of the checkbox lable in material-ui?
- How to deploy React build folder to Heroku
- React JavaScript with ES6 compiling issue under browserify and babel
- Can I have react-hooks/exhaustive-deps for a custom hook?