diff --git a/docs/02-displaying-data.md b/docs/02-displaying-data.md index 3f5983a3..26fe0ac4 100644 --- a/docs/02-displaying-data.md +++ b/docs/02-displaying-data.md @@ -77,12 +77,50 @@ React components are very simple. You can think of them as simple function that We strongly believe that components are the right way to separate concerns rather than "templates" and "display logic." We think that markup and the code that generates it are intimately tied together. Additionally, display logic is often very complex and using template languages to express it becomes cumbersome. -We've found that the best solution for this problem is to generate markup directly from the JavaScript code such that you can use all of the expressive power of a real programming language to build UIs. In order to make this easier, we've added a very simple, **optional** HTML-like syntax for the function calls that generate markup called JSX. +We've found that the best solution for this problem is to generate HTML and component trees directly from the JavaScript code such that you can use all of the expressive power of a real programming language to build UIs. -**JSX lets you write JavaScript function calls with HTML syntax.** To generate a link in React using pure JavaScript you'd write: `React.createElement('a', {href: 'http://facebook.github.io/react/'}, 'Hello React!')`. With JSX this becomes `Hello React!`. We've found this has made building React apps easier and designers tend to prefer the syntax, but everyone has their own workflow, so **JSX is not required to use React.** +In order to make this easier, we've added a very simple, **optional** HTML-like syntax to create these React tree nodes. -JSX is very small; the "hello, world" example above uses every feature of JSX. To learn more about it, see [JSX in depth](/react/docs/jsx-in-depth.html). Or see the transform in action in [our live JSX compiler](/react/jsx-compiler.html). +**JSX lets you create JavaScript objects using HTML syntax.** To generate a link in React using pure JavaScript you'd write: + +`React.createElement('a', {href: 'http://facebook.github.io/react/'}, 'Hello!')` + +With JSX this becomes: + +`Hello!` + +We've found this has made building React apps easier and designers tend to prefer the syntax, but everyone has their own workflow, so **JSX is not required to use React.** + +JSX is very small. To learn more about it, see [JSX in depth](/react/docs/jsx-in-depth.html). Or see the transform in action in [our live JSX compiler](/react/jsx-compiler.html). JSX is similar to HTML, but not exactly the same. See [JSX gotchas](/react/docs/jsx-gotchas.html) for some key differences. The easiest way to get started with JSX is to use the in-browser `JSXTransformer`. We strongly recommend that you don't use this in production. You can precompile your code using our command-line [react-tools](http://npmjs.org/package/react-tools) package. + + +## React without JSX + +JSX is completely optional. You don't have to use JSX with React. You can create these trees through `React.createElement`. The first argument is the tag, pass a properties object as the second argument and children to the third argument. + +```javascript +var child = React.createElement('li', null, 'Text Content'); +var root = React.createElement('ul', { className: 'my-list' }, child); +React.render(root, document.body); +``` + +As a convenience you can create short-hand factory function to create elements from custom components. + +```javascript +var Factory = React.createFactory(ComponentClass); +... +var root = Factory({ custom: 'prop' }); +React.render(root, document.body); +``` + +React already have built-in factories for common HTML tags: + +```javascript +var root = React.DOM.ul({ className: 'my-list' }, + React.DOM.li(null, 'Text Content') + ); +``` diff --git a/docs/02.1-jsx-in-depth.md b/docs/02.1-jsx-in-depth.md index f3411359..f83cc0fb 100644 --- a/docs/02.1-jsx-in-depth.md +++ b/docs/02.1-jsx-in-depth.md @@ -3,32 +3,51 @@ id: jsx-in-depth title: JSX in Depth permalink: jsx-in-depth.html prev: displaying-data.html -next: jsx-gotchas.html +next: jsx-spread.html --- -JSX is a JavaScript XML syntax transform recommended for use -with React. +[JSX](http://facebook.github.io/jsx/) is a JavaScript syntax extension that looks similar to XML. You can use a simple JSX syntactic transform with React. ## Why JSX? -React works out of the box without JSX. Simply construct your markup using the -functions on `React.DOM`. For example, here's how to construct a simple link: +You don't have to use JSX with React. You can just use plain JS. However, we recommend using JSX because it is a concise and familiar syntax for defining tree structures with attributes. + +It's more familiar for casual developers such as designers. + +XML has the benefit of balanced open and closing tags. This helps make large trees easier to read than function calls or object literals. + +It doesn't alter the semantics of JavaScript. + +## HTML Tags vs. React Components + +React can either render HTML tags (strings) or React components (classes). + +To render a HTML tag, just use lower-case tag names in JSX: ```javascript -var link = React.DOM.a({href: 'http://facebook.github.io/react'}, 'React'); +var myDivElement =
; +React.render(myDivElement, document.body); ``` -We recommend using JSX for many reasons: +To render a React Component, just a local variable that starts with an upper-case letter: -* It's easier to visualize the structure of the DOM. -* Designers are more comfortable making changes. -* It's familiar for those who have used MXML or XAML. +```javascript +var MyComponent = React.createClass({/*...*/}); +var myElement =