gecko-dev/devtools/docs/frontend/react.md
Patrick Brosset 5b282b009e Bug 1349256 - Moved docs into folders so it's easier to browse; r=sole
MozReview-Commit-ID: HzL3w8vp8iS

--HG--
rename : devtools/docs/backend.md => devtools/docs/backend/backend.md
rename : devtools/docs/backward-compatibility.md => devtools/docs/backend/backward-compatibility.md
rename : devtools/docs/debugger-api.md => devtools/docs/backend/debugger-api.md
rename : devtools/docs/protocol.md => devtools/docs/backend/protocol.md
rename : devtools/docs/frontend.md => devtools/docs/frontend/frontend.md
rename : devtools/docs/react-guidelines.md => devtools/docs/frontend/react-guidelines.md
rename : devtools/docs/react-tips.md => devtools/docs/frontend/react-tips.md
rename : devtools/docs/react.md => devtools/docs/frontend/react.md
rename : devtools/docs/redux-guidelines.md => devtools/docs/frontend/redux-guidelines.md
rename : devtools/docs/redux-tips.md => devtools/docs/frontend/redux-tips.md
rename : devtools/docs/redux.md => devtools/docs/frontend/redux.md
rename : devtools/docs/svgs.md => devtools/docs/frontend/svgs.md
rename : devtools/docs/img/box-model-highlighter-screenshot.png => devtools/docs/resources/box-model-highlighter-screenshot.png
rename : devtools/docs/svgs/expand-strokes.gif => devtools/docs/resources/expand-strokes.gif
rename : devtools/docs/svgs/pathfinder.gif => devtools/docs/resources/pathfinder.gif
rename : devtools/docs/svgs/sketch-position.png => devtools/docs/resources/sketch-position.png
rename : devtools/docs/debugger-panel.md => devtools/docs/tools/debugger-panel.md
rename : devtools/docs/highlighters.md => devtools/docs/tools/highlighters.md
rename : devtools/docs/http-inspector.md => devtools/docs/tools/http-inspector.md
rename : devtools/docs/inspector-panel.md => devtools/docs/tools/inspector-panel.md
rename : devtools/docs/inspector.md => devtools/docs/tools/inspector.md
rename : devtools/docs/memory-panel.md => devtools/docs/tools/memory-panel.md
rename : devtools/docs/responsive-design-mode.md => devtools/docs/tools/responsive-design-mode.md
rename : devtools/docs/tools.md => devtools/docs/tools/tools.md
extra : rebase_source : 96dfbd86b0294a041e0fc82158b1d91a8e9ba9e8
2017-03-22 15:49:14 +01:00

5.7 KiB

We use React to write our user interfaces. In here you can find an explanation of why we chose React and a short primer on it. Additionally, we list best practices that all devtools code should adhere to when writing React.

Quick Intro

This is a very quick introduction on how to use React, but does not explain in-depth the concepts behind it. If you want more in-depth articles, I recommend the following links:

React embraces components as a way of thinking about UIs. Components are the center of everything: they are composable like functions, testable like JSON data, and provide lifecycle APIs for more complex scenarios.

A component can represent anything from a single item in a list to a complete virtualized grid that is made up of sub-components. They can be used to abstract out "behaviors" instead of UI elements (think of a Selectable component). React's API makes it easy to break up your UI into whatever abstractions you need.

The core idea of a component is simple: it's something that takes properties and returns a DOM-like structure.

function Item({ name, iconURL }) {
  return div({ className: "item" },
             img({ className: "icon", href: iconURL }),
             name);
}

The div and span functions refer to React.DOM.div and React.DOM.span. React provides constructors for all DOM elements on React.DOM. These conform to the standard API for creating elements: the first argument takes properties, and the rest are children.

You can see component composition kick in when using Item:

const Item = React.createFactory(require('./Item'));

function List({ items }) {
  return div({ className: "list" },
             items.map(item => Item({ name: item.name, icon: item.iconURL)));
}

You can use custom components exactly the same way you use native ones! The only difference is we wrapped it in a factory when importing instead of using the React.DOM functions. Factories are just a way of turning a component into a convenient function. Without factories, you need to do do React.createElement(Item, { ... }), which is exactly the same as Item({ ... }) if using a factory.

Rendering and Updating Components

Now that we have some components, how do we render them? You use React.render for that:

let items = [{ name: "Dubois", iconURL: "dubois.png" },
             { name: "Ivy", iconURL: "ivy.png" }];

React.render(List({ items: items }),
             document.querySelector("#mount"));

This renders a List component, given items, to a DOM node with an id of mount. Typically you have a top-level App component that is the root of everything, and you would render it like so.

What about updating? First, let's talk about data. The above components take data from above and render out DOM structure. If any user events were involved, the components would call callbacks passed as props, so events walk back up the hierarchy. The conceptual model is data goes down, and events come up.

You usually want to change data in response to events, and rerender the UI with the new data. What does that look like? There are two places where React will rerender components:

1. Any additional React.render calls. Once a component is mounted, you can call React.render again to the same place and React will see that it's already mounted and perform an update instead of a full render. For example, this code adds an item in response to an event and updates the UI, and will perform optimal incremental updates:

function addItem(item) {
  render([...items, item]);
}

function render(items) {
  React.render(List({ items: items,
                      onAddItem: addItem }),
               document.querySelector("#mount"));
}

render(items);

2. Changing component local state. This is much more common. React allows components to have local state, and whenever the state is changed with the setState API it will rerender that specific component. If you use component local state, you need to create a component with createClass:

const App = React.createClass({
  getInitialState: function() {
    return { items: [] };
  },

  handleAddItem: function(item) {
    const items = [...this.props.items, item];
    this.setState({ items: items });
  },

  render: function() {
    return List({ items: this.state.items,
                  onAddItem: this.handleAddItem });
  }
});

If you are using something like Redux to manage state this is handled automatically for you with the library you use to bind Redux with React. See more in Redux.

DOM Diffing

What does it mean when React "updates" a component, and how does it know which DOM to change? React achieves this with a technique called DOM diffing. This alleviates the need for the programmer to worry about how updates are actually applied to the DOM, and components can render DOM structure declaratively in response to data. In the above examples, when adding an item, React knows to only add a new DOM node instead of recreating the whole list each time.

DOM diffing is possible because our components return what's called "virtual DOM": a lightweight JSON structure that React can use to diff against previous versions, and generate minimal changes to the real DOM.

This also makes it really east to test components with a real DOM: just make sure the virtual DOM has what it should.

Next

Read the React Guidelines next to learn how to write React code specifically for the devtools.