The hand-on project to get myself familiar with React.js, which pulls the latest post from Twitter by using a nodejs module in client-side javascript and allows users to form their own collections and share them via Codepen or other platforms.
Some important details you want to learn on this project
Browserify
used for building all the dependency file together in such a way that we can use Node.js module to build up out client-side application. We install it in this way:1
npm install - - save-dev browserify
Notice, here is a save-dev flag, which tells npm that it is a development dependency, it will write this dependency in package.json file. Writing a module name into package.json tells npm to install when executed command: npm install. Here is a distinction between running dependency and developing dependency. And since Browserify is used during build time, so it belongs to developing dependency.
Writing style
1 | var React = require('react'); |
It is in out Application.react.js. The reason we adopt a CommonJS writing style is that it will be convenient for other part of the file to require this react component we just build. So we use such module.exports = Application; to allow other file when used to require this component.
Children elements change parent’s elements
Focus on this part:
1 | <Stream onAddTweetToCollection={this.addTweetToCollection} /> |
the function defined in this react component is passed in, which indicates that this function will be called inside Stream component where since we know addTweetToCollection embodies a setState method, it illustrates how child element can update its parent component’s state.
Thus, in Stream component, we can access this method by using this.props.onAddTweetToCollection. Child component feed the arguments to parent’s state-changing function which will trigger render() function to re-render the whole children component again.
React lifecycle method API like componentDidMount()
This function is part of react API, that will be called once after the initial rendering finished. At that time, we have a initialed DOM tree, and it will be a perfect time for using another js library!
1 | componentDidMount: function(){ |
componentWillUnmount() will be called when react unmount the components.
And in general, react lifecycle methods can be grouped into three phases.
mounting, updating and unmounting.
And in this coding example, we see componentDidMount and componentWillUnmount. Also, we have componentWillMount and other methods
Note that
componentDidMountwill be the method when DOM is ready. Thus we usually will introduce other JS library at this method to do something on the DOM tree. For example, Jquery, setInterval, setTimeout and so on.
say in this example, if we want to use setInterval method:
1 | componentDidMount: function() { |
And we use react-don node.js module here.
Some of other lifecycle methods are: shouldComponentUpdate, this will determine whether or not to call the render function, and forceUpdate can skip this function checking. Note that it is a good way to enhance app’s speed when in hundreds of components app. componentWillRecieveProps will invoke wen receiving new props can use setState inside this method while the similar method compontWillUpdate cannot. componentDidUpdate will invoke after each update.
getDefaultProps
it will set the props if it receive no props from parent components
Checking for props exist?
the predicate statement at parent component may sometimes determine whether or not to render a children component. If not, then in later children component part, it should check whether it receives props from parent component.
Validation function for properties
propTypes is used for validating if a property is used and has value. If not, it will report an error to console.
1 | propTypes: { |
So in propTypes, we are validating the existence of Tweet by obtaining the tweet property object from properties[propertyName]. And for validating the onImageClick part, we only need to ensure that it is a function. so we did:
1 | onImageClick: React.propTypes.func |
componentDidMount: function(){
this.refs.collectionName.focus();
},
render: function(){
return (
<div className="form-group" style={inputStyle} onChange={this.handleInputValueChange}
value={this.state.inputValue} ref="collectionName" />
</div>
<Button label="Change" handleClick={this.handleFormSubmit} />
<Button label="Cancel" handleClick={this.handleFormCancel} />
</form>
)
}1
2
3
4
5
we know that `componentDidMount` will be called instantly after DOM tree is ready.
## Bootstrap usage in this project
```
It is where we use some Bootstrap property to quickly add style to the components. Note that we only add those style at the wrapper element for example we always wrap a div outside the target element.
references this post for more Bootstrap property.
container-fluidfor a full width container, spanning the entire width of viewport.text-centerto put the innerHTML text at the center of the element.rowwill create a horizontal groups of elements.col-md-8andcol-md-4for sparing width horizontally.form-groupfor optimum spacing for labels and input controls.form-controlfor element inside the form wrapper, usually appear with theform-groupclass.
Try to add color palette using Mustache.js and ColorThief.js
Due to my technical issue, I can only make it a local version. I will try me best to host it online.