-
Notifications
You must be signed in to change notification settings - Fork 0
26 React
Syntax extension to JavaScript
Produces React "elements"
-
React doesn't require using JSX, but most people find it helpful as a visual aid when working with UI inside the JavaScript code.
-
It also allows React to show more useful error and warning messages.
Can put any valid JavaScript expression inside the curly braces in JSX.
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
Can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions:
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}May use quotes to specify string literals as attributes:
const element = <div tabIndex="0"></div>;May use curly braces to embed a JavaScript expression in an attribute:
const element = <img src={user.avatarUrl}></img>;Don't put quotes around curly braces when embedding a JavaScript expression in an attribute. You should either use quotes (for string values) or curly braces (for expressions), but not both in the same attribute.
If a tag is empty, you may close it immediately with />, like XML:
const element = <img src={user.avatarUrl} />;JSX tags may contain children.
By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that's not explicitly written in your application. Everything is converted to a string before being rendered. Prevents XSS (cross-site-scripting) attacks.
Babel compiles JSX down to React.createElement() calls.
These two examples are identical:
const elements = (
<h1 className="greeting">
Hello, world!
</h1>
);const element = React.createElemet(
'h1',
{className: 'greeting'},
'Hello, world!'
);React.createElemet() performs a few checks to help you write bug-free code but essentially it creates an object like this:
// Note: this structure is simplified
const element = {
type: 'h1',
props: {
className: 'greeting',
children: 'Hello, world!'
}
};These objects are called "React elements". You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.
Elements are the smallest building blocks of React apps.
An element describes what you want to see on the screen:
const element = <h1>Hello, world</h1>;React elements are plain objects, and are cheap to create. React DOM takes care of updating the DOM to match the React elements.
Applications built with just React usually have a single root DOM node. If you are integrating React into an existing app, you may have as many isolated root DOM nodes as you like.
To render a React element into a root DOM node, pass both to ReactDOM.render():
const element = <h1>Hello, world</h1>;
ReactDOM.render(element, document.getElementById('root'));React elements are immutable. Once you create an element, you can't change its children or attributes. An element is like a single frame in a movie: it represents the UI at a certain point in time.
With our knowledge so far, the only way to update the UI is to create a new element, and pass it to ReactDOM.render().
React DOM compares the element and its children to the previous one, and only applies the DOM updates necessary to bring the DOM to the desired state.