question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting initial state and updating in an interval

See original GitHub issue

Sorry 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:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kickbkcommented, Aug 17, 2020

Thanks @dai-shi . Yes, it helped a lot.

0reactions
dai-shicommented, Aug 15, 2020

Does it solve your problem? Let me close this for now. Feel free to reopen.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found