This is a first stab at constructing a front end to incorporate the previously discussed Lesser Evil CLI gem. It consists of a Tweet component, which, for now, displays the key elements of a tweet (text, author, and timestamp), and a TweetList component, which is designed to respond to changes in the database as the gem trawls for the tweets it's looking for and adds them. As of yet, there is no database, so I built a seed file and a simulator that adds tweets from the seed file to the TweetList state at random intervals.
- After the TweetList is attached to the DOM (in a separate file), it's rendered by mapping an array of Tweet components to the tweets array in the state (line 50). This array is empty at first (line 23).
- Once the component is mounted the
componentDidMount
method is called (line 45). Here we iterate over the tweets from the seed file and call the wrapper method for the setTimeout functionality. setTimeoutWrapper
(line 36) sets up a delay window of 2 seconds and makes a call toreadNextTweet
at a random point within that window.readNextTweet
(line 27) grabs the next tweet from the seed array and pushes it onto the state's tweets array. This state update triggers a re-render of the TweetList component in the DOM, i.e., the addition of the next tweet to the list.