React, by itself, is a library for generating the user interface of an application.
What did all of that mean? What functionality does React actually give us? It's actually pretty similar to what we've been doing with vanilla client side JavaScript:
We can use jsbin or codepen to learn the React API.
From there, you can use the React
object in your JavaScript as your entry point to the React API.
First… let's try something that should look really familiar. What do you think this code will do? →
ReactDOM.render(
React.createElement('div', {className: 'foo'}, 'Hey... familiar!'),
document.body
);
React.createElement
:
className
instead of class
)
ReactDOM.render
Let's try this… →
ReactDOM.render(
Um, whut?,
document.body
);
What looks strange about this code? →
ReactDOM.render(
Um, whut?,
document.body
);
It looks like there's an unquoted string of markup in the JavaScript!
JSX is an extension to JavaScript syntax that allows XML-like syntax without all of the fussy quoting!
So that means… this JSX
Um, whut?
…is equivalent to this vanilla JavaScript
React.createElement('div', {className: 'foo'}, 'Um, whut?'),
They both produce a ReactElement!
Er. Didn't we spend half of the semester separating our HTML from JavaScript? Why is JSX the standard? Did the react devs pull a prank on us? →
You can bundle elements together into a single component. Here's an example. →
var MyComponent = React.createClass({
render: function() {
return (
A Message
{this.props.message}
);
}
});
ReactDOM.render(
<MyComponent message="Hi there!" />,
document.getElementById('root');
);
// assuming element with id="root"
Using shorthand method definitions: →
const MyComponent = React.createClass({
render() {
return(
A Message
{this.props.message}
);
}
});
You can also use ES6 classes to create components by extending React.Component
→
class MyComponent extends React.Component {
render() {
return (
A Message
{this.props.message}
);
}
}
To make a component, use React.createClass
, which takes an object as a parameter.
Let's try to create a component that… →
For example… rendering…
<MyComponent times="5" />,
Gives us
hello hello hello hello hello
const MyComponent = React.createClass({
render: function() {
var text = "";
for(var i = 0; i < this.props.times; i++) {
text += "hello ";
}
return (
{text}
)
}
});
ReactDOM.render(
<MyComponent times="5" />,
document.body
);
What if we wanted to surround our hello with markup and add a number? →
const Greeting = React.createClass({
render: function() {
const paragraphs = [];
for(let i = 0; i < this.props.times; i++) {
paragraphs.push(<p key={i} >hello {i}</p>);
}
return (
{paragraphs}
);
}
});
It's also pretty common to use map to generate elements. →
const Greeting = React.createClass({
render: function() {
const greetings = ['hi', 'hello', 'hola'];
const greetingElements = greetings.map((g, i) => {
<p key={i}>{g}</p>
});
return (
{greetingElements}
);
}
});
Hey - did you notice that weird key attribute in both examples in the previous slides? →
for(let i = 0; i < this.props.times; i++) {
paragraphs.push(<p key={i} >hello {i}</p>);
}
const greetingElements = greetings.map((g, i) => {
<p key={i}>{g}</p>
});
When adding an Array of elements, the key
attribute should be defined so that React can determine DOM changes more easily. Check out the [details on why to use keys] (https://facebook.github.io/react/docs/reconciliation.html#recursing-on-children), and an in-depth article on Arrays and keys.
When adding an Array of elements to a component, you must keep the following things in mind… →
return (
only one elementbut you can nest!
);
key
attribute to each element!To add an event handler in JSX… add an inline attribute (wait, what!?). For example, click events would be represented by onClick
:
const MyButton = React.createClass({
onButtonClick: function(evt) {
alert("OMG! OMG!");
},
render: function() {
return <div onClick={this.onButtonClick}>Press This Button</div>;
}
});
ReactDOM.render(
<MyButton />,
document.body
)
If you're using ES6 classes and want to use this
in your click handler, remember to bind to this (or use arrow functions) →
Here's an alert that uses this.props
:
onButtonClick: function(evt) {
alert(this.props.someProp);
},
To enable the above code access this
, though, bind it when you assign the callback function for onClick
.
render: function() {
return <div onClick={this.onButtonClick.bind(this)}>Press This Button</div>;
}
We'll see more about this later on…