JSX is a syntax extension to JavaScript. it is similar to template language, but it comes with the full power of JavaScript. JSX produces React elements
Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both. We will come back to components in a further section, but if you’re not yet comfortable putting markup in JS, this talk might convince you otherwise
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
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. This helps prevent XSS (cross-site-scripting) attacks
Unlike browser DOM elements, 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
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.
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation. they are like JavaScript functions. They accept arbitrary inputs called props
and return React elements describing what should appear on the screen
When React sees an element representing a user-defined component, it passes JSX attributes and children to this component as a single object. We call this object props
.
Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail. A button, a form, a dialog, a screen: in React apps, all those are commonly expressed as components. Typically, new React apps have a single App component at the very top
Whether you declare a component as a function or a class, it must never modify its own props. All React components must act like pure functions with respect to their props.
- Create an
ES6 class
, with the same name, that extendsReact.Component
. - Add a single empty method to it called
render()
. - Move the body of the function into the
render()
method. - Replace
props
withthis.props
in therender()
body. - Delete the remaining empty function declaration.
In applications with many components, it’s very important to free up resources taken by the components when they are destroyed.
- Do Not Modify State Directly
- State Updates May Be Asynchronous
- State Updates are Merged
Handling events with React elements is very similar to handling events on DOM elements. There are some syntax differences:
- React events are named using camelCase, rather than lowercase.
- With JSX you pass a function as the event handler, rather than a string.