Browse Source

All doc updates forv15.5 (#9359)

* `react-addons-test-utils` -> `react-dom/test-utils`

Updating all references and docs on the `React.addons.TestUtils` and the
shallow renderer to refer to the correct targets.

Instead of:
```
const React = require('react');

// ...
React.addons.Testutils
// or

const ReactTestUtils = require('react-addons-test-utils');
```
we now show:
```
const ReactTestUtils = require('react-dom/test-utils');
```

And for shallow renderer, instead of:
```
const shallowRenderer = TestUtils.createRenderer();
```

we now show:
```
const shallowRenderer = require('react-test-renderer/shallow');
```

* Update the 'prev' and 'next' attributes of 'add-ons' docs

These flags are used to set arrow links to easily navigate through the
documents. They were wrong or missing in some of the 'add-ons' pages and
this bothered me when manually testing the updates from the previous
commit.

* Update syntax for instantiating shallow renderer

Missed this when updating the docs for the changes to shallow-renderer
in React 15.5.

* Fix pointers in addons docs

Thanks @bvaughn for catching this

* Make example of shallow renderer more consistent

We should show using the same variable names between code samples.

* Make names in example even more consistent

We should use the same variable name for the same thing across examples.
`renderer` -> `shallowRenderer`.

* Update docs to deprecate React<CSS>TransitionGroup

 - removes link to the docs about `ReactCSSTransitionGroup` and
   `ReactTransitionGroup` from the main navigation
 - updates 'prev' and 'next' pointers to skip this page
 - adds deprecation warning to the top of the page
 - remove references to these modules from the packages README
 - updates 'add-ons' main page to list this as a deprecated add-on

* Update `React.createClass` to `createReactClass` in the docs

The `React.createClass` method is being deprecated in favor of
`createReactClass`.

* Remove 'React.createClass' from top level API docs

It no longer makes sense to have a section for the 'createClass' method
in this page, since it won't be available as a top level method on
'React'.

I initially was going to pull the section about 'createClass' into a
separate page to add under 'addons' but it was short and duplicative of
the 'react-without-es6' docs. So I just linked to those.

* Remove *most* `React.PropTypes` from the docs

I am doing the docs for `context` in a separate commit because that case
was a bit less clear-cut.

We will no longer support `React.PropTypes` as a built-in feature of
React, and instead should direct folks to use the `PropTypes` project
that stands alone.

Rather than retaining the `React.PropTypes` examples and just revamping
them to show the use of the stand-alone `PropTypes` library with React,
it makes more sense to direct people to that project and reduce the
perceived API area and complexity of React core. The proper place to
document `PropTypes` is in the README or docs of that project, not in
React docs.

* Update `context` docs to not use `React.PropTypes`

We use `React.PropTypes` to define the `contextType` for the `context`
feature of React. It's unclear how this will work once `React.PropTypes`
is replaced by the external `PropTypes` library. Some options;

a) Deprecate `context`, either in v16 or shortly after. Seems reasonable
based on the intense warnings against using context that we have in the
docs -
https://facebook.github.io/react/docs/context.html#why-not-to-use-context
**Except** that probably some widely used libraries depend on it, like
`React-Router`.

b) Expect users will use external `PropTypes` library when defining
`contextTypes` and just don't do our `checkReactTypeSpec` against them
any more in v16.

c) Stop masking context and pass the whole context
unmasked everywhere. Worst option, do not recommend.

I went with `b` and assume that, for now, we will get users to use the
external `PropTypes` when defining context. I will update this PR if we
want a different approach.

* Remove 'addons' items from left nav, and deprecate 'addons' doc page

The plan:
[X] Remove links to 'addons' items from main navigation
[X] Add deprecation notices where appropriate, and update syntax to show
using the separate modules.
[ ] Update other references to 'React.addons' in docs. Coming in next
commit.
--- blocked but coming in future PRs
[ ] Link to a blog post describing the new locations of add-ons in the
deprecation notice on the '/docs/addons.html' page. Blocked until we
actually publish that blog post.
[ ] Move the docs for each add-on to the actual github repo where it now
lives.
[ ] Redirect the old add-ons doc permalinks to the docs in the separate
github repos for those modules.
[ ] Remove the old add-ons doc markdown files from React core docs.

* Remove references to `React.addons` from docs

Just misc. places where we referenced the 'addons' feature. All gone!
main
Flarnie Marchan 8 years ago
committed by Andrew Clark
parent
commit
803e5da950
  1. 21
      _data/nav_docs.yml
  2. 6
      contributing/codebase-overview.md
  3. 7
      docs/addons-animation.md
  4. 3
      docs/addons-create-fragment.md
  5. 3
      docs/addons-perf.md
  6. 5
      docs/addons-pure-render-mixin.md
  7. 1
      docs/addons-shallow-compare.md
  8. 60
      docs/addons-shallow-renderer.md
  9. 53
      docs/addons-test-utils.md
  10. 20
      docs/addons-two-way-binding-helpers.md
  11. 1
      docs/addons-update.md
  12. 9
      docs/addons.md
  13. 2
      docs/components-and-props.md
  14. 2
      docs/composition-vs-inheritance.md
  15. 2
      docs/conditional-rendering.md
  16. 20
      docs/context.md
  17. 39
      docs/react-without-es6.md
  18. 3
      docs/reference-pure-render-mixin.md
  19. 23
      docs/reference-react-component.md
  20. 259
      docs/reference-react.md
  21. 5
      docs/typechecking-with-proptypes.md
  22. 3
      warnings/dont-call-proptypes.md

21
_data/nav_docs.yml

@ -30,8 +30,6 @@
items:
- id: jsx-in-depth
title: JSX In Depth
- id: typechecking-with-proptypes
title: Typechecking With PropTypes
- id: refs-and-the-dom
title: Refs and the DOM
- id: uncontrolled-components
@ -65,22 +63,3 @@
title: DOM Elements
- id: events
title: SyntheticEvent
- id: addons
title: Add-Ons
subitems:
- id: perf
title: Performance Tools
- id: test-utils
title: Test Utilities
- id: animation
title: Animation
- id: create-fragment
title: Keyed Fragments
- id: update
title: Immutability Helpers
- id: pure-render-mixin
title: PureRenderMixin
- id: shallow-compare
title: Shallow Compare
- id: two-way-binding-helpers
title: Two-way Binding Helpers

6
contributing/codebase-overview.md

@ -401,12 +401,6 @@ React implements a synthetic event system which is agnostic of the renderers and
There is a [video with a deep code dive into it](https://www.youtube.com/watch?v=dRo_egw7tBc) (66 mins).
### Add-ons
Each of the [React add-ons](/react/docs/addons.html) ships as a separate package on npm with a `react-addons-` prefix. Their source is located in [`src/addons`](https://github.com/facebook/react/tree/master/src/addons) with the exception of [`ReactPerf`](https://github.com/facebook/react/blob/master/src/renderers/shared/ReactPerf.js) and [`ReactTestUtils`](https://github.com/facebook/react/blob/master/src/test/ReactTestUtils.js).
Additionally, we provide a standalone build called `react-with-addons.js` which includes React core *and* all add-ons exposed on the `addons` field of the `React` global object.
### What Next?
Read the [next section](/react/contributing/implementation-notes.html) to learn about the current implementation of reconciler in more detail.

7
docs/addons-animation.md

@ -4,14 +4,15 @@ title: Animation Add-Ons
permalink: docs/animation.html
layout: docs
category: Add-Ons
prev: addons.html
next: create-fragment.html
redirect_from:
- "docs/animation-ja-JP.html"
- "docs/animation-ko-KR.html"
- "docs/animation-zh-CN.html"
---
>Note:
> `ReactTransitionGroup` and `ReactCSSTransitionGroup` are both deprecated as of React v15.5.0. The recommendation is to use `TransitionGroup` and `CSSTransitionGroup` from ['react-transition-group'](https://github.com/reactjs/react-transition-group) instead.
The [`ReactTransitionGroup`](#low-level-api-reacttransitiongroup) add-on component is a low-level API for animation, and [`ReactCSSTransitionGroup`](#high-level-api-reactcsstransitiongroup) is an add-on component for easily implementing basic CSS animations and transitions.
## High-level API: ReactCSSTransitionGroup
@ -23,7 +24,6 @@ The [`ReactTransitionGroup`](#low-level-api-reacttransitiongroup) add-on compone
```javascript
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'; // ES6
var ReactCSSTransitionGroup = require('react-addons-css-transition-group'); // ES5 with npm
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup; // ES5 with react-with-addons.js
```
```javascript{31-36}
@ -231,7 +231,6 @@ You can disable animating `enter` or `leave` animations if you want. For example
```javascript
import ReactTransitionGroup from 'react-addons-transition-group' // ES6
var ReactTransitionGroup = require('react-addons-transition-group') // ES5 with npm
var ReactTransitionGroup = React.addons.TransitionGroup; // ES5 with react-with-addons.js
```
`ReactTransitionGroup` is the basis for animations. When children are declaratively added or removed from it (as in the [example above](#getting-started)), special lifecycle hooks are called on them.

3
docs/addons-create-fragment.md

@ -4,8 +4,6 @@ title: Keyed Fragments
permalink: docs/create-fragment.html
layout: docs
category: Add-Ons
prev: animation.html
next: perf.html
---
**Importing**
@ -13,7 +11,6 @@ next: perf.html
```javascript
import createFragment from 'react-addons-create-fragment'; // ES6
var createFragment = require('react-addons-create-fragment'); // ES5 with npm
var createFragment = React.addons.createFragment; // ES5 with react-with-addons.js
```
## Overview

3
docs/addons-perf.md

@ -4,8 +4,6 @@ title: Performance Tools
permalink: docs/perf.html
layout: docs
category: Add-Ons
prev: create-fragment.html
next: test-utils.html
---
**Importing**
@ -13,7 +11,6 @@ next: test-utils.html
```javascript
import Perf from 'react-addons-perf'; // ES6
var Perf = require('react-addons-perf'); // ES5 with npm
var Perf = React.addons.Perf; // ES5 with react-with-addons.js
```

5
docs/addons-pure-render-mixin.md

@ -14,7 +14,6 @@ category: Add-Ons
```javascript
import PureRenderMixin from 'react-addons-pure-render-mixin'; // ES6
var PureRenderMixin = require('react-addons-pure-render-mixin'); // ES5 with npm
var PureRenderMixin = React.addons.PureRenderMixin; // ES5 with react-with-addons.js
```
## Overview
@ -24,7 +23,9 @@ If your React component's render function renders the same result given the same
Example:
```js
React.createClass({
const createReactClass = require('create-react-class');
createReactClass({
mixins: [PureRenderMixin],
render: function() {

1
docs/addons-shallow-compare.md

@ -14,7 +14,6 @@ category: Reference
```javascript
import shallowCompare from 'react-addons-shallow-compare'; // ES6
var shallowCompare = require('react-addons-shallow-compare'); // ES5 with npm
var shallowCompare = React.addons.shallowCompare; // ES5 with react-with-addons.js
```
## Overview

60
docs/addons-shallow-renderer.md

@ -0,0 +1,60 @@
---
id: shallow-renderer
title: Shallow Renderer
permalink: docs/shallow-renderer.html
layout: docs
category: Reference
---
**Importing**
```javascript
import ReactShallowRenderer from 'react-test-renderer/shallow'; // ES6
var ReactShallowRenderer = require('react-test-renderer/shallow'); // ES5 with npm
```
### Shallow Rendering
Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.
- [`shallowRenderer.render()`](#shallowrenderer.render)
- [`shallowRenderer.getRenderOutput()`](#shallowrenderer.getrenderoutput)
You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output.
[`shallowRenderer.render()`](#shallowrenderer.render) is similar to [`ReactDOM.render()`](/react/docs/react-dom.html#render) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.
After `shallowRenderer.render()` has been called, you can use [`shallowRenderer.getRenderOutput()`](#shallowrenderer.getrenderoutput) to get the shallowly rendered output.
You can then begin to assert facts about the output. For example, if you have the following component:
```javascript
function MyComponent() {
return (
<div>
<span className="heading">Title</span>
<Subcomponent foo="bar" />
</div>
);
}
```
Then you can assert:
```javascript
const ReactShallowRenderer = require('react-test-renderer/shallow');
const shallowRenderer = new ReactShallowRenderer();
shallowRenderer.render(<MyComponent />);
const result = shallowRenderer.getRenderOutput();
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
<span className="heading">Title</span>,
<Subcomponent foo="bar" />
]);
```
Shallow testing currently has some limitations, namely not supporting refs.
We also recommend checking out Enzyme's [Shallow Rendering API](http://airbnb.io/enzyme/docs/api/shallow.html). It provides a nicer higher-level API over the same functionality.
* * *

53
docs/addons-test-utils.md

@ -4,15 +4,13 @@ title: Test Utilities
permalink: docs/test-utils.html
layout: docs
category: Reference
prev: perf.html
---
**Importing**
```javascript
import ReactTestUtils from 'react-addons-test-utils'; // ES6
var ReactTestUtils = require('react-addons-test-utils'); // ES5 with npm
var ReactTestUtils = React.addons.TestUtils; // ES5 with react-with-addons.js
import ReactTestUtils from 'react-dom/test-utils'; // ES6
var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
```
## Overview
@ -39,53 +37,6 @@ var ReactTestUtils = React.addons.TestUtils; // ES5 with react-with-addons.js
- [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype)
- [`findRenderedComponentWithType()`](#findrenderedcomponentwithtype)
### Shallow Rendering
Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.
- [`createRenderer()`](#createrenderer)
- [`shallowRenderer.render()`](#shallowrenderer.render)
- [`shallowRenderer.getRenderOutput()`](#shallowrenderer.getrenderoutput)
Call [`createRenderer()`](#createrenderer) in your tests to create a shallow renderer. You can think of this as a "place" to render the component you're testing, and from which you can extract the component's output.
[`shallowRenderer.render()`](#shallowrenderer.render) is similar to [`ReactDOM.render()`](/react/docs/react-dom.html#render) but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.
After `shallowRenderer.render()` has been called, you can use [`shallowRenderer.getRenderOutput()`](#shallowrenderer.getrenderoutput) to get the shallowly rendered output.
You can then begin to assert facts about the output. For example, if you have the following component:
```javascript
function MyComponent() {
return (
<div>
<span className="heading">Title</span>
<Subcomponent foo="bar" />
</div>
);
}
```
Then you can assert:
```javascript
const renderer = ReactTestUtils.createRenderer();
renderer.render(<MyComponent />);
const result = renderer.getRenderOutput();
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
<span className="heading">Title</span>,
<Subcomponent foo="bar" />
]);
```
Shallow testing currently has some limitations, namely not supporting refs.
We also recommend checking out Enzyme's [Shallow Rendering API](http://airbnb.io/enzyme/docs/api/shallow.html). It provides a nicer higher-level API over the same functionality.
* * *
## Reference
### `Simulate`

20
docs/addons-two-way-binding-helpers.md

@ -4,8 +4,6 @@ title: Two-way Binding Helpers
permalink: docs/two-way-binding-helpers.html
layout: docs
category: Add-Ons
prev: pure-render-mixin.html
next: update.html
---
> Note:
@ -16,7 +14,6 @@ next: update.html
```javascript
import LinkedStateMixin from 'react-addons-linked-state-mixin'; // ES6
var LinkedStateMixin = require('react-addons-linked-state-mixin'); // ES5 with npm
var LinkedStateMixin = React.addons.LinkedStateMixin; // ES5 with react-with-addons.js
```
## Overview
@ -40,7 +37,9 @@ Two-way binding -- implicitly enforcing that some value in the DOM is always con
Here's a simple form example without using `LinkedStateMixin`:
```javascript
var NoLink = React.createClass({
var createReactClass = require('create-react-class');
var NoLink = createReactClass({
getInitialState: function() {
return {message: 'Hello!'};
},
@ -57,7 +56,9 @@ var NoLink = React.createClass({
This works really well and it's very clear how data is flowing, however, with a lot of form fields it could get a bit verbose. Let's use `LinkedStateMixin` to save us some typing:
```javascript{4,9}
var WithLink = React.createClass({
var createReactClass = require('create-react-class');
var WithLink = createReactClass({
mixins: [LinkedStateMixin],
getInitialState: function() {
return {message: 'Hello!'};
@ -83,8 +84,10 @@ There are two sides to `LinkedStateMixin`: the place where you create the `value
### valueLink Without LinkedStateMixin
```javascript{5-7,9-12}
var WithoutMixin = React.createClass({
```javascript{7-9,11-14}
var createReactClass = require('create-react-class');
var WithoutMixin = createReactClass({
getInitialState: function() {
return {message: 'Hello!'};
},
@ -107,8 +110,9 @@ As you can see, `valueLink` objects are very simple objects that just have a `va
```javascript
var LinkedStateMixin = require('react-addons-linked-state-mixin');
var createReactClass = require('create-react-class');
var WithoutLink = React.createClass({
var WithoutLink = createReactClass({
mixins: [LinkedStateMixin],
getInitialState: function() {
return {message: 'Hello!'};

1
docs/addons-update.md

@ -14,7 +14,6 @@ category: Add-Ons
```javascript
import update from 'react-addons-update'; // ES6
var update = require('react-addons-update'); // ES5 with npm
var update = React.addons.update; // ES5 with react-with-addons.js
```
## Overview

9
docs/addons.md

@ -4,9 +4,11 @@ title: Add-Ons
permalink: docs/addons.html
---
>Note:
> `React.addons` is deprecated as of React v15.5. The add-ons have now all either been deprecated or moved to separate modules.
The React add-ons are a collection of useful utility modules for building React apps. **These should be considered experimental** and tend to change more often than the core.
- [`TransitionGroup` and `CSSTransitionGroup`](animation.html), for dealing with animations and transitions that are usually not simple to implement, such as before a component's removal.
- [`createFragment`](create-fragment.html), to create a set of externally-keyed children.
The add-ons below are in the development (unminified) version of React only:
@ -24,11 +26,12 @@ The add-ons below are considered legacy and their use is discouraged.
### Deprecated Add-ons
[`LinkedStateMixin`](two-way-binding-helpers.html) has been deprecated.
- [`LinkedStateMixin`](two-way-binding-helpers.html) has been deprecated.
- [`TransitionGroup` and `CSSTransitionGroup`](animation.html) have been deprecated.
## Using React with Add-ons
If using npm, you can install the add-ons individually from npm (e.g. `npm install react-addons-test-utils`) and import them:
You can install the add-ons individually from npm (e.g. `npm install react-addons-create-fragment`) and import them:
```javascript
import Perf from 'react-addons-perf'; // ES6

2
docs/components-and-props.md

@ -12,8 +12,6 @@ redirect_from:
- "docs/transferring-props-zh-CN.html"
- "tips/props-in-getInitialState-as-anti-pattern.html"
- "tips/communicate-between-components.html"
prev: rendering-elements.html
next: state-and-lifecycle.html
---
Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

2
docs/composition-vs-inheritance.md

@ -3,8 +3,6 @@ id: composition-vs-inheritance
title: Composition vs Inheritance
permalink: docs/composition-vs-inheritance.html
redirect_from: "docs/multiple-components.html"
prev: lifting-state-up.html
next: thinking-in-react.html
---
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.

2
docs/conditional-rendering.md

@ -2,8 +2,6 @@
id: conditional-rendering
title: Conditional Rendering
permalink: docs/conditional-rendering.html
prev: handling-events.html
next: lists-and-keys.html
redirect_from: "tips/false-in-jsx.html"
---

20
docs/context.md

@ -4,11 +4,15 @@ title: Context
permalink: docs/context.html
---
>Note:
> As of React v15.5 the `React.PropTypes` helper is deprecated, and we recommend using the [`prop-types` library](https://github.com/aackerman/PropTypes) to define `contextTypes`.
With React, it's easy to track the flow of data through your React components. When you look at a component, you can see which props are being passed, which makes your apps easy to reason about.
In some cases, you want to pass data through the component tree without having to pass the props down manually at every level.
You can do this directly in React with the powerful "context" API.
## Why Not To Use Context
The vast majority of applications do not need to use context.
@ -59,7 +63,9 @@ class MessageList extends React.Component {
In this example, we manually thread through a `color` prop in order to style the `Button` and `Message` components appropriately. Using context, we can pass this through the tree automatically:
```javascript{4,11-13,19,26-28,38-40}
```javascript{6,13-15,21,28-30,40-42}
const PropTypes = require('prop-types');
class Button extends React.Component {
render() {
return (
@ -71,7 +77,7 @@ class Button extends React.Component {
}
Button.contextTypes = {
color: React.PropTypes.string
color: PropTypes.string
};
class Message extends React.Component {
@ -98,7 +104,7 @@ class MessageList extends React.Component {
}
MessageList.childContextTypes = {
color: React.PropTypes.string
color: PropTypes.string
};
```
@ -151,12 +157,14 @@ If `contextTypes` is defined within a component, the following [lifecycle method
Stateless functional components are also able to reference `context` if `contextTypes` is defined as a property of the function. The following code shows a `Button` component written as a stateless functional component.
```javascript
const PropTypes = require('prop-types');
const Button = ({children}, context) =>
<button style={{'{{'}}background: context.color}}>
{children}
</button>;
Button.contextTypes = {color: React.PropTypes.string};
Button.contextTypes = {color: PropTypes.string};
```
## Updating Context
@ -168,6 +176,8 @@ React has an API to update context, but it is fundamentally broken and you shoul
The `getChildContext` function will be called when the state or props changes. In order to update data in the context, trigger a local state update with `this.setState`. This will trigger a new context and changes will be received by the children.
```javascript
const PropTypes = require('prop-types');
class MediaQuery extends React.Component {
constructor(props) {
super(props);
@ -196,7 +206,7 @@ class MediaQuery extends React.Component {
}
MediaQuery.childContextTypes = {
type: React.PropTypes.string
type: PropTypes.string
};
```

39
docs/react-without-es6.md

@ -14,45 +14,38 @@ class Greeting extends React.Component {
}
```
If you don't use ES6 yet, you may use the `React.createClass` helper instead:
If you don't use ES6 yet, you may use the `create-react-class` module instead:
```javascript
var Greeting = React.createClass({
var createReactClass = require('create-react-class');
var Greeting = createReactClass({
render: function() {
return <h1>Hello, {this.props.name}</h1>;
}
});
```
The API of ES6 classes is similar to `React.createClass` with a few exceptions.
The API of ES6 classes is similar to `createReactClass()` with a few exceptions.
## Declaring Prop Types and Default Props
## Declaring Default Props
With functions and ES6 classes, `propTypes` and `defaultProps` are defined as properties on the components themselves:
With functions and ES6 classes `defaultProps` is defined as a property on the component itself:
```javascript
class Greeting extends React.Component {
// ...
}
Greeting.propTypes = {
name: React.PropTypes.string
};
Greeting.defaultProps = {
name: 'Mary'
};
```
With `React.createClass()`, you need to define `propTypes` as a property on the passed object, and `getDefaultProps()` as a function on it:
With `createReactClass()`, you need to define `getDefaultProps()` as a function on the passed object:
```javascript
var Greeting = React.createClass({
propTypes: {
name: React.PropTypes.string
},
var Greeting = createReactClass({
getDefaultProps: function() {
return {
name: 'Mary'
@ -78,10 +71,10 @@ class Counter extends React.Component {
}
```
With `React.createClass()`, you have to provide a separate `getInitialState` method that returns the initial state:
With `createReactClass()`, you have to provide a separate `getInitialState` method that returns the initial state:
```javascript
var Counter = React.createClass({
var Counter = createReactClass({
getInitialState: function() {
return {count: this.props.initialCount};
},
@ -117,10 +110,10 @@ class SayHello extends React.Component {
}
```
With `React.createClass()`, this is not necessary because it binds all methods:
With `createReactClass()`, this is not necessary because it binds all methods:
```javascript
var SayHello = React.createClass({
var SayHello = createReactClass({
getInitialState: function() {
return {message: 'Hello!'};
},
@ -172,7 +165,7 @@ If you'd rather play it safe, you have a few options:
* Bind methods in the constructor.
* Use arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`.
* Keep using `React.createClass()`.
* Keep using `createReactClass`.
## Mixins
@ -184,7 +177,7 @@ If you'd rather play it safe, you have a few options:
>
>This section exists only for the reference.
Sometimes very different components may share some common functionality. These are sometimes called [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). [`React.createClass`](/react/docs/top-level-api.html#react.createclass) lets you use a legacy `mixins` system for that.
Sometimes very different components may share some common functionality. These are sometimes called [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). [`createReactClass`](/react/docs/top-level-api.html#react.createclass) lets you use a legacy `mixins` system for that.
One common use case is a component wanting to update itself on a time interval. It's easy to use `setInterval()`, but it's important to cancel your interval when you don't need it anymore to save memory. React provides [lifecycle methods](/react/docs/working-with-the-browser.html#component-lifecycle) that let you know when a component is about to be created or destroyed. Let's create a simple mixin that uses these methods to provide an easy `setInterval()` function that will automatically get cleaned up when your component is destroyed.
@ -201,7 +194,9 @@ var SetIntervalMixin = {
}
};
var TickTock = React.createClass({
var createReactClass = require('create-react-class');
var TickTock = createReactClass({
mixins: [SetIntervalMixin], // Use the mixin
getInitialState: function() {
return {seconds: 0};

3
docs/reference-pure-render-mixin.md

@ -16,7 +16,8 @@ Example:
```js
var PureRenderMixin = require('react-addons-pure-render-mixin');
React.createClass({
var createReactClass = require('create-react-class');
createReactClass({
mixins: [PureRenderMixin],
render: function() {

23
docs/reference-react-component.md

@ -31,7 +31,7 @@ class Greeting extends React.Component {
}
```
If you don't use ES6 yet, you may use the [`React.createClass`](/react/docs/react-api.html#createclass) helper instead. Take a look at [Using React without ES6](/react/docs/react-without-es6.html) to learn more.
If you don't use ES6 yet, you may use the [`create-react-class`](/react/docs/react-api.html#createclass) module instead. Take a look at [Using React without ES6](/react/docs/react-without-es6.html) to learn more.
### The Component Lifecycle
@ -73,7 +73,6 @@ Each component also provides some other APIs:
- [`defaultProps`](#defaultprops)
- [`displayName`](#displayname)
- [`propTypes`](#proptypes)
### Instance Properties
@ -315,26 +314,6 @@ The `displayName` string is used in debugging messages. JSX sets this value auto
* * *
### `propTypes`
`propTypes` can be defined as a property on the component class itself, to define what types the props should be. It should be a map from prop names to types as defined in [`React.PropTypes`](/react/docs/react-api.html#react.proptypes). In development mode, when an invalid value is provided for a prop, a warning will be shown in the JavaScript console. In production mode, `propTypes` checks are skipped for efficiency.
For example, this code ensures that the `color` prop is a string:
```js
class CustomButton extends React.Component {
// ...
}
CustomButton.propTypes = {
color: React.PropTypes.string
};
```
We recommend using [Flow](https://flowtype.org/) when possible, to get compile-time typechecking instead of runtime typechecking. [Flow has built-in support for React](https://flowtype.org/docs/react.html) so it's easy to run static analysis on a React app.
* * *
## Instance Properties
### `props`

259
docs/reference-react.md

@ -25,9 +25,7 @@ React components let you split the UI into independent, reusable pieces, and thi
- [`React.Component`](#react.component)
- [`React.PureComponent`](#react.purecomponent)
If you don't use ES6 classes, you may use this helper instead.
- [`createClass()`](#createclass)
If you don't use ES6 classes, you may use the `create-react-class` module instead. See [Using React without ES6](/react/docs/react-without-es6.html) for more information.
### Creating React Elements
@ -46,38 +44,6 @@ See [Using React without JSX](/react/docs/react-without-jsx.html) for more infor
- [`isValidElement()`](#isvalidelement)
- [`React.Children`](#react.children)
### Typechecking with PropTypes
You can use `React.PropTypes` to run typechecking on the props for a component.
- [`React.PropTypes`](#react.proptypes)
- [`React.PropTypes.array`](#react.proptypes.array)
- [`React.PropTypes.bool`](#react.proptypes.bool)
- [`React.PropTypes.func`](#react.proptypes.func)
- [`React.PropTypes.number`](#react.proptypes.number)
- [`React.PropTypes.object`](#react.proptypes.object)
- [`React.PropTypes.string`](#react.proptypes.string)
- [`React.PropTypes.symbol`](#react.proptypes.symbol)
- [`React.PropTypes.node`](#react.proptypes.node)
- [`React.PropTypes.element`](#react.proptypes.element)
- [`React.PropTypes.instanceOf()`](#react.proptypes.instanceof)
- [`React.PropTypes.oneOf()`](#react.proptypes.oneof)
- [`React.PropTypes.oneOfType()`](#react.proptypes.oneoftype)
- [`React.PropTypes.arrayOf()`](#react.proptypes.arrayof)
- [`React.PropTypes.objectOf()`](#react.proptypes.objectof)
- [`React.PropTypes.shape()`](#react.proptypes.shape)
- [`React.PropTypes.any`](#react.proptypes.any)
Validators treat props as optional by default. You can use `isRequired` to make sure a warning is shown if the prop is not provided.
- [`isRequired`](#isrequired)
### Add-Ons
If you're using [`react-with-addons.js`](/react/docs/addons.html), the React Add-Ons will be available via `React.addons`.
- [`React.addons`](#react.addons)
* * *
## Reference
@ -112,26 +78,6 @@ If your React component's `render()` function renders the same result given the
* * *
### `createClass()`
```javascript
React.createClass(specification)
```
If you don't use ES6 yet, you may use the `React.createClass()` helper instead to create a component class.
```javascript
var Greeting = React.createClass({
render: function() {
return <h1>Hello, {this.props.name}</h1>;
}
});
```
See [Using React without ES6](/react/docs/react-without-es6.html) for more information.
* * *
### `createElement()`
```javascript
@ -245,206 +191,3 @@ Returns the `children` opaque data structure as a flat array with keys assigned
> Note:
>
> `React.Children.toArray()` changes keys to preserve the semantics of nested arrays when flattening lists of children. That is, `toArray` prefixes each key in the returned array so that each element's key is scoped to the input array containing it.
* * *
### `React.PropTypes`
`React.PropTypes` exports a range of validators that can be used with a component's `propTypes` object to validate props being passed to your components.
For more information about `PropTypes`, see [Typechecking with PropTypes](/react/docs/typechecking-with-proptypes.html).
#### `React.PropTypes.array`
```javascript
React.PropTypes.array
```
Validates that a prop is a JavaScript array primitive.
#### `React.PropTypes.bool`
```javascript
React.PropTypes.bool
```
Validates that a prop is a JavaScript bool primitive.
#### `React.PropTypes.func`
```javascript
React.PropTypes.func
```
Validates that a prop is a JavaScript function.
#### `React.PropTypes.number`
```javascript
React.PropTypes.number
```
Validates that a prop is a JavaScript number primitive.
#### `React.PropTypes.object`
```javascript
React.PropTypes.object
```
Validates that a prop is a JavaScript object.
#### `React.PropTypes.string`
```javascript
React.PropTypes.string
```
Validates that a prop is a JavaScript string primitive.
#### `React.PropTypes.symbol`
```javascript
React.PropTypes.symbol
```
Validates that a prop is a JavaScript symbol.
#### `React.PropTypes.node`
```javascript
React.PropTypes.node
```
Validates that a prop is anything that can be rendered: numbers, strings, elements or an array (or fragment) containing these types.
#### `React.PropTypes.element`
```javascript
React.PropTypes.element
```
Validates that a prop is a React element.
#### `React.PropTypes.instanceOf()`
```javascript
React.PropTypes.instanceOf(class)
```
Validates that a prop is an instance of a class. This uses JavaScript's `instanceof` operator.
#### `React.PropTypes.oneOf()`
```javascript
React.PropTypes.oneOf(arrayOfValues)
```
Validates that a prop is limited to specific values by treating it as an enum.
```javascript
MyComponent.propTypes = {
optionalEnum: React.PropTypes.oneOf(['News', 'Photos']),
}
```
#### `React.PropTypes.oneOfType()`
```javascript
React.PropTypes.oneOfType(arrayOfPropTypes)
```
Validates that a prop is an object that could be one of many types.
```javascript
MyComponent.propTypes = {
optionalUnion: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
React.PropTypes.instanceOf(Message)
]),
}
```
#### `React.PropTypes.arrayOf()`
```javascript
React.PropTypes.arrayOf(propType)
```
Validates that a prop is an array of a certain type.
```javascript
MyComponent.propTypes = {
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
}
```
#### `React.PropTypes.objectOf()`
```javascript
React.PropTypes.objectOf(propType)
```
Validates that a prop is an object with property values of a certain type.
```javascript
MyComponent.propTypes = {
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
}
```
#### `React.PropTypes.shape()`
```javascript
React.PropTypes.shape(object)
```
Validates that a prop is an object taking on a particular shape.
```javascript
MyComponent.propTypes = {
optionalObjectWithShape: React.PropTypes.shape({
color: React.PropTypes.string,
fontSize: React.PropTypes.number
}),
}
```
#### `React.PropTypes.any`
```javascript
React.PropTypes.any
```
Validates that a prop has a value of any data type. Usually followed by `isRequired`.
```javascript
MyComponent.propTypes = {
requiredAny: React.PropTypes.any.isRequired,
}
```
### `isRequired`
```javascript
propType.isRequired
```
You can chain any of the above validators with `isRequired` to make sure a warning is shown if the prop is not provided.
```javascript
MyComponent.propTypes = {
requiredFunc: React.PropTypes.func.isRequired,
}
```
* * *
### `React.addons`
```javascript
React.addons
```
`React.addons` exports a range of add-ons when using [`react-with-addons.js`](/react/docs/addons.html).

5
docs/typechecking-with-proptypes.md

@ -2,8 +2,13 @@
id: typechecking-with-proptypes
title: Typechecking With PropTypes
permalink: docs/typechecking-with-proptypes.html
redirect_from:
- "docs/react-api.html#typechecking-with-proptypes"
---
> Note:
> `React.PropTypes` is deprecated as of React v15.5. Please use [the `prop-types` library instead](https://github.com/aackerman/PropTypes).
As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like [Flow](https://flowtype.org/) or [TypeScript](https://www.typescriptlang.org/) to typecheck your whole application. But even if you don't use those, React has some built-in typechecking abilities. To run typechecking on the props for a component, you can assign the special `propTypes` property:
```javascript

3
warnings/dont-call-proptypes.md

@ -4,6 +4,9 @@ layout: single
permalink: warnings/dont-call-proptypes.html
---
> Note:
> `React.PropTypes` is deprecated as of React v15.5. Please use [the `prop-types` library instead](https://github.com/aackerman/PropTypes).
In a future major release of React, the code that implements PropType validation functions will be stripped in production. Once this happens, any code that calls these functions manually (that isn't stripped in production) will throw an error.
### Declaring PropTypes is still fine

Loading…
Cancel
Save