Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove React.createClass and React.PropTypes from docs #9355

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/_data/nav_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion docs/docs/addons-pure-render-mixin.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,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() {
Expand Down
17 changes: 12 additions & 5 deletions docs/docs/addons-two-way-binding-helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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!'};
},
Expand All @@ -57,7 +59,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!'};
Expand All @@ -83,8 +87,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!'};
},
Expand All @@ -107,8 +113,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!'};
Expand Down
20 changes: 15 additions & 5 deletions docs/docs/context.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 (
Expand All @@ -71,7 +77,7 @@ class Button extends React.Component {
}

Button.contextTypes = {
color: React.PropTypes.string
color: PropTypes.string
};

class Message extends React.Component {
Expand All @@ -98,7 +104,7 @@ class MessageList extends React.Component {
}

MessageList.childContextTypes = {
color: React.PropTypes.string
color: PropTypes.string
};
```

Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -196,7 +206,7 @@ class MediaQuery extends React.Component {
}

MediaQuery.childContextTypes = {
type: React.PropTypes.string
type: PropTypes.string
};
```

Expand Down
41 changes: 18 additions & 23 deletions docs/docs/react-without-es6.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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};
},
Expand Down Expand Up @@ -117,14 +110,14 @@ 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!'};
},

handleClick: function() {
alert(this.state.message);
},
Expand Down Expand Up @@ -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

Expand All @@ -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.

Expand All @@ -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};
Expand Down
3 changes: 2 additions & 1 deletion docs/docs/reference-pure-render-mixin.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
25 changes: 2 additions & 23 deletions docs/docs/reference-react-component.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -73,7 +73,6 @@ Each component also provides some other APIs:

- [`defaultProps`](#defaultprops)
- [`displayName`](#displayname)
- [`propTypes`](#proptypes)

### Instance Properties

Expand Down Expand Up @@ -122,7 +121,7 @@ constructor(props) {
this.state = {
color: props.initialColor
};
}
}
```

Beware of this pattern, as it effectively "forks" the props and can lead to bugs. Instead of syncing props to state, you often want to [lift the state up](/react/docs/lifting-state-up.html).
Expand Down Expand Up @@ -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`
Expand Down
Loading