I have three different things going on in my application:
My question is this:
Where and When do I initialize the timer to being so that my Component can start receiving data after its
render()
method is invoked?
I've considered putting it inside the render
function before the return is reached but that doesn't make sense as I need the api data rendered to the page as soon as the data is received.
Am I structuring this application incorrectly? should my Timer and/or Api utilities be React Components too?
I am sure there are a few ways to it, but here is my 2 cents:
Lets assume you have LogViewer application and you want to fetch and display logs every 10 seconds.
LogStore
and a LogViewer
component.componentDidMount
method of the LogViewer
component, I would call LogStore.getLogs()
.LogStore.getLogs()
would call APIUtils.getLogs()
and setup a timer to be fired every 10 seconds which calls APIUtils.getLogs()
every time it is fired. After setting the timer LogStore.getLogs()
immediately returns an empty array.LogViewer
component (or a loading bar is shown).APIUtils.getLogs()
returns and GET_LOGS_SUCCESS
server action is dispatched to all stores.LogStore
saves the incoming logs and emits a change event for the LogViewer
component to call LogStore.getLogs()
function again.LogViewer
component I would have a stop button which in turn creates GET_LOGS_STOP
action when clicked. GET_LOGS_STOP
action is dispatched to the LogStore
, it would react by stoping the timer.I think that would fulfill your requirements.