Full Stack Web Development Immersive Lead Instructor at Galvanize SF.
https://www.linkedin.com/in/hamid-aghdaee-442548
A JavaScript library for building User Interfaces
A library built and maintained by Facebook for composing user interfaces using small, single purpose components that have well-defined, predictable behavior, and as a result maximize reusability.
It is a virtualized representation of the DOM, at different points in time.
The virtualization is made up of light weight Javascript objects that represents the DOM tree.
It is inexpensive in memory costs, allowing the diffing algorithm to execute in milliseconds!
http://teropa.info/blog/2015/03/02/change-and-its-detection-in-javascript-frameworks.html
Every time the data representing your app changes, a new virtual DOM is generated, this is diffed against the old virtual DOM producing a minimum set of changes to the real DOM
It efficiently makes use of the virtual DOM, and does a selective rendering only on the components that need to change, thus avoiding frequent manipulation of actual DOM
Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.
// React in js
var HelloWorld = React.createClass({displayName: 'HelloWorld',
render: function() {
return (
React.createElement('div', {className: "react-example"},
"Hello, world."
)
);
}
});
ReactDOM.render(
React.createElement(HelloWorld, null),
document.getElementById('container')
);
// React in JSX
class HelloWorld extents React.Component {
render () {
return (
<div className="react-example">
Hello, world!
</div>
);
}
}
ReactDOM.render(
<HelloWorld />,
document.getElementById('container')
);
Which one looks better?
Browsers doesn't understand JSX, so we use a transpiler like babel to get it converted to JS
bit.ly/2k3DlpD
Now, code your first React Component!
You can fork the codepen that I've shared with you to save your changes. I'll be making updates to the presentation codepen throughout.
Now, refresh the presentation codepen and complete the next exercise, creating a tree of components.
bit.ly/2k3DlpD
In React, the view is a function of the state of your application.
f(state) = view
Passing props into a component looks a lot like HTML attributes we're already used to:
<MyComponent prop1={prop1Value} />
Accessing a prop inside a component:
class MyComponent extends React.Component {
render(){
return (
<div>
{this.props.prop1}
</div>
)
}
}
Now, refresh the presentation codepen and complete the exercise:
bit.ly/2k3DlpD
Refresh the presentation codepen and let's revisit the header menu...
Set its initial value in the constructor.
constructor(props) {
super(props);
this.state = {
count: 0
}
}
Consequently set it using setState.
incrementCount(){
this.setState({
count: this.state.count + 1
});
}
Please refresh the presentation codepen. Let's revisit the Menu component.
bit.ly/2k3DlpD
Now let's make a simple counter.
Go ahead and finish the exercise.
Every component in React goes through it own event life cycle.
It includes the set of events that takes place before render() as well as after render()
class CountDownTimer extends Component {
constructor(props) {
super(props);
/* initialize state here */
}
componentWillMount() {
/* invoked before mounting occurs, just before "render" */
}
componentDidMount() {
/* invoked immediately after a component is mounted,
* DOM can be accessed here too */
}
componentWillReceiveProps(nextProps) {
/* once mounted, invoked whenever new props are received */
/* changes to state can be performed here */
}
componentWillUpdate(nextProps, nextState) {
/* invoked before rendering, when new props or state are received */
}
componentWillUnmount() {
/* teardown code goes here, unsubscribe from event listeners */
}
render() {
/* invoked when state or props change */
}
}