* Don't use experimental syntax unless necessary
* Edit another page
* Update another page
* Unrelated bikeshed on virtual DOM
* Please, no *.jsx, we don't recommend it
* Update faq-structure.md
* Minor updates
* Update faq-ajax.md
* Update faq-functions.md
* Add binding explanation
@ -89,53 +90,86 @@ Generally speaking, yes, it is OK, and it is often the easiest way to pass param
If you do have performance issues, by all means, optimize!
### Why is binding necessary at all?
In JavaScript, these two code snippets are **not** equivalent:
```js
obj.method();
```
```js
var method = obj.method();
method();
```
Binding methods helps ensure that the second snippet works the same way as the first one.
With React, typically you only need to bind the methods you *pass* to other components. For example, `<button onClick={this.handleClick}>` passes `this.handleClick` so you want to bind it. However, it is unnecessary to bind the `render` method or the lifecycle methods: we don't pass them to other components.
[This post by Yehuda Katz](http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/) explains what binding is, and how functions work in JavaScript, in detail.
### Why is my function being called every time the component renders?
Make sure you aren't _calling the function_ when you pass it to the component:
```jsx
render() {
{/* handleClick is called instead of passed as a reference! */}
// Wrong: handleClick is called instead of passed as a reference!
@ -191,20 +230,26 @@ If you have an event handler such as `onClick` or `onScroll` and want to prevent
Throttling prevents a function from being called more than once in a given window of time. The example below throttles a "click" handler to prevent calling it more than once per second.
@ -213,33 +258,39 @@ class LoadMoreButton extends React.Component {
Debouncing ensures that a function will not be executed until after a certain amount of time has passed since it was last called. This can be useful when you have to perform some expensive calculation in response to an event that might dispatch rapidly (eg scroll or keyboard events). The example below debounces text input with a 250ms delay.
The virtual DOM (VDOM) is a programming concept where an ideal, or "virtual", representation of a UI is kept in memory and synced with the "real" DOM by a reconciliation engine/renderer (i.e. React Fiber + ReactDOM).
The virtual DOM (VDOM) is a programming concept where an ideal, or "virtual", representation of a UI is kept in memory and synced with the "real" DOM by a library such as ReactDOM. This process is called [reconciliation](/docs/reconciliation.html).
React uses the virtual DOM to enable its declarative API: You tell React what state you want the UI to be in, and it makes sure the DOM matches that state. This abstracts out the class manipulation, event handling, and manual DOM updating that you would otherwise have to use to build your app.
This approach enables the declarative API of React: You tell React what state you want the UI to be in, and it makes sure the DOM matches that state. This abstracts out the attribute manipulation, event handling, and manual DOM updating that you would otherwise have to use to build your app.
Since "virtual DOM" is more of a pattern than a specific technology, people sometimes say it to mean different things. In React world, the term "virtual DOM" is usually associated with [React elements](/docs/rendering-elements.html) since they are the objects representing the user interface. React, however, also uses internal objects called "fibers" to hold additional information about the component tree. They may also be considered a part of "virtual DOM" implementation in React.
### Is the Shadow DOM the same as the Virtual DOM?
@ -17,14 +17,15 @@ Calls to `setState` are asynchronous - don't rely on `this.state` to reflect the
Example of code that will not behave as expected:
```jsx
incrementCount = () => {
this.setState({count: this.state.count + 1})
incrementCount() {
// Note: this will *not* work as intended.
this.setState({count: this.state.count + 1});
}
handleSomething() {
// this.state.count is 1, then we do this:
this.incrementCount()
this.incrementCount() // state wasn't updated yet, so this sets 2 not 3
this.incrementCount();
this.incrementCount(); // state wasn't updated yet, so this sets 2 not 3
}
```
@ -39,16 +40,16 @@ Pass a function instead of an object to setState to ensure the call always uses
Passing an update function allows you to access the current state value inside the updater. Since `setState` calls are batched, this lets you chain updates and ensure they build on top of each other instead of conflicting:
It is common for CSS classes to depend on the component props or state:
```jsx
render() {
let className = 'menu';
if (this.props.isActive) {
className += ' menu-active';
}
return <spanclassName={className}>Menu</span>
}
```
If you often find yourself writing code like this, [classnames](https://www.npmjs.com/package/classnames) package can simplify it.
### Can I use inline styles?
Yes, see the docs on styling [here](/docs/dom-elements.html#style).
@ -32,4 +46,4 @@ CSS-in-JS refers to a pattern where CSS is written with Javascript, then extract
### Can I do animations in React?
React can be used to power animations. See [React Transition Group](https://reactcommunity.org/react-transition-group/), for example.
React can be used to power animations. See [React Transition Group](https://reactcommunity.org/react-transition-group/) and [React Motion](https://github.com/chenglou/react-motion), for example.