Setting initial state and updating in an interval
See original GitHub issueSorry for the newb question here. I’m trying to store the current time with Zustand and update it every second without re-rendering the app.
Currently I have:
...
import create from 'zustand';
const [useStore] = create((set) => ({
currentDateTime: undefined,
setCurrentDateTime: () => set((state) => ({ currentDateTime: new Date() })),
}));
export default function App() {
const { currentDateTime, setCurrentDateTime } = useStore();
useEffect(() => {
const secondsTimer = setInterval(() => {
setCurrentDateTime();
}, 1000);
return () => clearInterval(secondsTimer);
}, [setCurrentDateTime]);
console.log('Setting the time');
...
I am seeing the console log every second, which means it is re-rendering. What am I doing wrong here?
Issue Analytics
- State:
- Created 3 years ago
- Comments:11 (6 by maintainers)
Top Results From Across the Web
Setting initial state and updating in an interval · Issue #140
I'm trying to store the current time with Zustand and update it every second without re-rendering the app. Currently I have: ... import...
Read more >Update React State variable inside an Interval
Since all you're doing is setting state, you can fix this by using the function version of set state. React will pass you...
Read more >How to work with intervals in React hooks | by Florian
The count variable is set to 0 (initial state); After the component is rendered and painted, React will execute the useEffect hook.
Read more >How to change state continuously after a certain amount of ...
First, make a function that is responsible for changing the state of the component. Then call the function from the constructor method for...
Read more >How to set an interval in React (with examples)
A pretty straightforward functional component that holds a state in counter . The state is incremented every second thanks to the setInterval ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks @dai-shi . Yes, it helped a lot.
Does it solve your problem? Let me close this for now. Feel free to reopen.