React
What Is React?
React is
- a JavaScript library
- for building user interfaces (?)
- lets you compose complex UIs from small and isolated pieces of code called “components”.
A component
- takes in parameters, called
props(short for “properties”), and - returns a hierarchy of views to display via the
rendermethod.
Hello World
JSX
Embedding Expressions in JSX
Specifying Attributes with JSX
Rendering Elements
Unlike browser DOM elements, React elements are plain objects, and are cheap to create.
Rendering an Element into the DOM
Updating the Rendered Element
React elements are immutable. Once you create an element, you can’t change its children or attributes. The only way to update the UI is to create a new element, and pass it to ReactDOM.render().
React Only Updates What’s Necessary
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 and Props
Components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.
Function and Class Components
The simplest way to define a component is to write a JavaScript function:
This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. We call such components “function components” because they are literally JavaScript functions.
You can also use an ES6 class to define a component:
The above two components are equivalent from React’s point of view.
Rendering a Component
Elements can also represent user-defined components:
Props are Read-Only
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.
State and Lifecycle
Converting a Function to a Class
You can convert a function component like Clock to a class in five steps:
- Create an ES6 class, with the same name, that extends
React.Component. - Add a single empty method to it called
render(). - Move the body of the function into the
render()method. - Replace
propswiththis.propsin therender()body. - Delete the remaining empty function declaration.
Adding Local State to a Class
Adding Lifecycle Methods to a Class
We can declare special methods on the component class to run some code when a component mounts and unmounts:
These methods are called “lifecycle methods”.
Using State Correctly
Do Not Modify State Directly
State Updates May Be Asynchronous
React may batch multiple setState() calls into a single update for performance.
Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.
State Updates are Merged
When you call setState(), React merges the object you provide into the current state.
The Data Flows Down
Neither parent nor child components can know if a certain component is stateful or stateless, and they shouldn’t care whether it is defined as a function or a class.
A component may choose to pass its state down as props to its child components:
The FormattedDate component would receive the date in its props and wouldn’t know whether it came from the Clock’s state, from the Clock’s props, or was typed by hand:
Handling Events
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.
- Cannot return
falseto prevent default behavior in React. You must callpreventDefaultexplicitly.
In HTML:
In React:
When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class.
You can use an arrow function in the callback:
The problem with this syntax is that a different callback is created each time the LoggingButton renders.