Skip to content

Rename "functional component" to "function component" #863

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

Merged
merged 1 commit into from
Oct 4, 2018
Merged
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
6 changes: 3 additions & 3 deletions content/blog/2015-10-07-react-v0.14.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ If you can’t use `npm` yet, we provide pre-built browser builds for your conve

With this change, we’re deprecating `.getDOMNode()` and replacing it with `ReactDOM.findDOMNode` (see below). If your components are currently using `.getDOMNode()`, they will continue to work with a warning until 0.15.

- #### Stateless functional components
- #### Stateless function components

In idiomatic React code, most of the components you write will be stateless, simply composing other components. We’re introducing a new, simpler syntax for these components where you can take `props` as an argument and return the element you want to render:

```js
// A functional component using an ES2015 (ES6) arrow function:
// A function component using an ES2015 (ES6) arrow function:
var Aquarium = (props) => {
var fish = getFish(props.species);
return <Tank>{fish}</Tank>;
Expand All @@ -125,7 +125,7 @@ If you can’t use `npm` yet, we provide pre-built browser builds for your conve
// Then use: <Aquarium species="rainbowfish" />
```

These components behave just like a React class with only a `render` method defined. Since no component instance is created for a functional component, any `ref` added to one will evaluate to `null`. Functional components do not have lifecycle methods, but you can set `.propTypes` and `.defaultProps` as properties on the function.
These components behave just like a React class with only a `render` method defined. Since no component instance is created for a function component, any `ref` added to one will evaluate to `null`. Function components do not have lifecycle methods, but you can set `.propTypes` and `.defaultProps` as properties on the function.

This pattern is designed to encourage the creation of these simple components that should comprise large portions of your apps. In the future, we’ll also be able to make performance optimizations specific to these components by avoiding unnecessary checks and memory allocations.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ class Button extends React.Component {
}
```

When a component is defined as a class, it is a little bit more powerful than a functional component. It can store some local state and perform custom logic when the corresponding DOM node is created or destroyed.
When a component is defined as a class, it is a little bit more powerful than a function component. It can store some local state and perform custom logic when the corresponding DOM node is created or destroyed.

A functional component is less powerful but is simpler, and acts like a class component with just a single `render()` method. Unless you need features available only in a class, we encourage you to use functional components instead.
A function component is less powerful but is simpler, and acts like a class component with just a single `render()` method. Unless you need features available only in a class, we encourage you to use function components instead.

**However, whether functions or classes, fundamentally they are all components to React. They take the props as their input, and return the elements as their output.**

Expand Down Expand Up @@ -370,7 +370,7 @@ When a component receives some props as an input, it is because a particular par

An *instance* is what you refer to as `this` in the component class you write. It is useful for [storing local state and reacting to the lifecycle events](/docs/component-api.html).

Functional components don’t have instances at all. Class components have instances, but you never need to create a component instance directly—React takes care of this.
Function components don’t have instances at all. Class components have instances, but you never need to create a component instance directly—React takes care of this.

Finally, to create elements, use [`React.createElement()`](/docs/top-level-api.html#react.createelement), [JSX](/docs/jsx-in-depth.html), or an [element factory helper](/docs/top-level-api.html#react.createfactory). Don’t write elements as plain objects in the real code—just know that they are plain objects under the hood.

Expand Down
2 changes: 1 addition & 1 deletion content/blog/2016-04-07-react-v15.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ If you can’t use `npm` yet, we provide pre-built browser builds for your conve

<small>[@sophiebits](https://github.com/sophiebits) in [#5451](https://github.com/facebook/react/pull/5451)</small>

- #### Functional components can now return `null` too
- #### Function components can now return `null` too

We added support for [defining stateless components as functions](/blog/2015/09/10/react-v0.14-rc1.html#stateless-function-components) in React 0.14. However, React 0.14 still allowed you to define a class component without extending `React.Component` or using `React.createClass()`, so [we couldn’t reliably tell if your component is a function or a class](https://github.com/facebook/react/issues/5355), and did not allow returning `null` from it. This issue is solved in React 15, and you can now return `null` from any component, whether it is a class or a function.

Expand Down
4 changes: 2 additions & 2 deletions content/blog/2016-07-13-mixins-considered-harmful.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ function withSubscription(WrappedComponent) {
});
}

// Optional change: convert CommentList to a functional component
// Optional change: convert CommentList to a function component
// because it doesn't use lifecycle hooks or state.
function CommentList(props) {
var comments = props.comments;
Expand Down Expand Up @@ -467,7 +467,7 @@ Props keep component dependencies explicit, easy to replace, and enforceable wit

> **Note:**
>
> Defining components as functions is not required. There is also nothing wrong with using lifecycle hooks and state—they are first-class React features. We use functional components in this example because they are easier to read and we didn’t need those extra features, but classes would work just as fine.
> Defining components as functions is not required. There is also nothing wrong with using lifecycle hooks and state—they are first-class React features. We use function components in this example because they are easier to read and we didn’t need those extra features, but classes would work just as fine.

### Context

Expand Down
2 changes: 1 addition & 1 deletion content/blog/2017-04-07-react-v15.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ You may also consider using [Flow](https://flow.org/) to statically type check y

When React was initially released, there was no idiomatic way to create classes in JavaScript, so we provided our own: `React.createClass`.

Later, classes were added to the language as part of ES2015, so we added the ability to create React components using JavaScript classes. **Along with functional components, JavaScript classes are now the [preferred way to create components in React](/docs/components-and-props.html#functional-and-class-components).**
Later, classes were added to the language as part of ES2015, so we added the ability to create React components using JavaScript classes. **Along with function components, JavaScript classes are now the [preferred way to create components in React](/docs/components-and-props.html#functional-and-class-components).**

For your existing `createClass` components, we recommend that you migrate them to JavaScript classes. However, if you have components that rely on mixins, converting to classes may not be immediately feasible. If so, `create-react-class` is available on npm as a drop-in replacement:

Expand Down
2 changes: 1 addition & 1 deletion content/blog/2018-03-29-react-v-16-3.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Version 16.3 adds a new option for managing refs that offers the convenience of

Generally, React components are declarative, but sometimes imperative access to the component instances and the underlying DOM nodes is necessary. This is common for use cases like managing focus, selection, or animations. React provides [refs](/docs/refs-and-the-dom.html) as a way to solve this problem. However, component encapsulation poses some challenges with refs.

For example, if you replace a `<button>` with a custom `<FancyButton>` component, the `ref` attribute on it will start pointing at the wrapper component instead of the DOM node (and would be `null` for functional components). While this is desirable for "application-level" components like `FeedStory` or `Comment` that need to be encapsulated, it can be annoying for "leaf" components such as `FancyButton` or `MyTextInput` that are typically used like their DOM counterparts, and might need to expose their DOM nodes.
For example, if you replace a `<button>` with a custom `<FancyButton>` component, the `ref` attribute on it will start pointing at the wrapper component instead of the DOM node (and would be `null` for function components). While this is desirable for "application-level" components like `FeedStory` or `Comment` that need to be encapsulated, it can be annoying for "leaf" components such as `FancyButton` or `MyTextInput` that are typically used like their DOM counterparts, and might need to expose their DOM nodes.

Ref forwarding is a new opt-in feature that lets some components take a `ref` they receive, and pass it further down (in other words, "forward" it) to a child. In the example below, `FancyButton` forwards its ref to a DOM `button` that it renders:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ This design is fundamentally flawed, but it's also an easy mistake to make. ([I'

### Recommendation: Fully controlled component

One way to avoid the problems mentioned above is to remove state from our component entirely. If the email address only exists as a prop, then we don't have to worry about conflicts with state. We could even convert `EmailInput` to a lighter-weight functional component:
One way to avoid the problems mentioned above is to remove state from our component entirely. If the email address only exists as a prop, then we don't have to worry about conflicts with state. We could even convert `EmailInput` to a lighter-weight function component:
```js
function EmailInput(props) {
return <input onChange={props.onChange} value={props.email} />;
Expand Down
8 changes: 4 additions & 4 deletions content/docs/components-and-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Components let you split the UI into independent, reusable pieces, and think abo

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called "props") and return React elements describing what should appear on the screen.

## Functional and Class Components
## Function and Class Components

The simplest way to define a component is to write a JavaScript function:

Expand All @@ -30,7 +30,7 @@ function Welcome(props) {
}
```

This function is a valid React component because it accepts a single "props" (which stands for properties) object argument with data and returns a React element. We call such components "functional" because they are literally JavaScript functions.
This function is a valid React component because it accepts a single "props" (which stands for properties) object argument with data and returns a React element. We call such components "function components" because they are literally JavaScript functions.

You can also use an [ES6 class](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes) to define a component:

Expand All @@ -44,7 +44,7 @@ class Welcome extends React.Component {

The above two components are equivalent from React's point of view.

Classes have some additional features that we will discuss in the [next sections](/docs/state-and-lifecycle.html). Until then, we will use functional components for their conciseness.
Classes have some additional features that we will discuss in the [next sections](/docs/state-and-lifecycle.html). Until then, we will use function components for their conciseness.

## Rendering a Component

Expand Down Expand Up @@ -237,7 +237,7 @@ Extracting components might seem like grunt work at first, but having a palette

## Props are Read-Only

Whether you declare a component [as a function or a class](#functional-and-class-components), it must never modify its own props. Consider this `sum` function:
Whether you declare a component [as a function or a class](#function-and-class-components), it must never modify its own props. Consider this `sum` function:

```js
function sum(a, b) {
Expand Down
2 changes: 1 addition & 1 deletion content/docs/forwarding-refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Here is a step-by-step explanation of what happens in the above example:

>Note
>
>The second `ref` argument only exists when you define a component with `React.forwardRef` call. Regular functional or class components don't receive the `ref` argument, and ref is not available in props either.
>The second `ref` argument only exists when you define a component with `React.forwardRef` call. Regular function or class components don't receive the `ref` argument, and ref is not available in props either.
>
>Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.

Expand Down
4 changes: 2 additions & 2 deletions content/docs/higher-order-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function logProps(InputComponent) {
const EnhancedComponent = logProps(InputComponent);
```

There are a few problems with this. One is that the input component cannot be reused separately from the enhanced component. More crucially, if you apply another HOC to `EnhancedComponent` that *also* mutates `componentWillReceiveProps`, the first HOC's functionality will be overridden! This HOC also won't work with functional components, which do not have lifecycle methods.
There are a few problems with this. One is that the input component cannot be reused separately from the enhanced component. More crucially, if you apply another HOC to `EnhancedComponent` that *also* mutates `componentWillReceiveProps`, the first HOC's functionality will be overridden! This HOC also won't work with function components, which do not have lifecycle methods.

Mutating HOCs are a leaky abstraction—the consumer must know how they are implemented in order to avoid conflicts with other HOCs.

Expand All @@ -211,7 +211,7 @@ function logProps(WrappedComponent) {
}
```

This HOC has the same functionality as the mutating version while avoiding the potential for clashes. It works equally well with class and functional components. And because it's a pure function, it's composable with other HOCs, or even with itself.
This HOC has the same functionality as the mutating version while avoiding the potential for clashes. It works equally well with class and function components. And because it's a pure function, it's composable with other HOCs, or even with itself.

You may have noticed similarities between HOCs and a pattern called **container components**. Container components are part of a strategy of separating responsibility between high-level and low-level concerns. Containers manage things like subscriptions and state, and pass props to components that handle things like rendering UI. HOCs use containers as part of their implementation. You can think of HOCs as parameterized container component definitions.

Expand Down
2 changes: 1 addition & 1 deletion content/docs/implementation-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class DOMComponent {

The main difference after refactoring from `mountHost()` is that we now keep `this.node` and `this.renderedChildren` associated with the internal DOM component instance. We will also use them for applying non-destructive updates in the future.

As a result, each internal instance, composite or host, now points to its child internal instances. To help visualize this, if a functional `<App>` component renders a `<Button>` class component, and `Button` class renders a `<div>`, the internal instance tree would look like this:
As a result, each internal instance, composite or host, now points to its child internal instances. To help visualize this, if a function `<App>` component renders a `<Button>` class component, and `Button` class renders a `<div>`, the internal instance tree would look like this:

```js
[object CompositeComponent] {
Expand Down
4 changes: 2 additions & 2 deletions content/docs/legacy-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ If `contextTypes` is defined within a component, the following [lifecycle method
>
> As of React 16, `componentDidUpdate` no longer receives `prevContext`.

### Referencing Context in Stateless Functional Components
### Referencing Context in Stateless Function Components

> This section documents a legacy API. See the [new API](/docs/context.html).

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.
Stateless function 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 function component.

```javascript
import PropTypes from 'prop-types';
Expand Down
2 changes: 1 addition & 1 deletion content/docs/reference-react-dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ When a component renders to `null` or `false`, `findDOMNode` returns `null`. Whe
>
> `findDOMNode` only works on mounted components (that is, components that have been placed in the DOM). If you try to call this on a component that has not been mounted yet (like calling `findDOMNode()` in `render()` on a component that has yet to be created) an exception will be thrown.
>
> `findDOMNode` cannot be used on functional components.
> `findDOMNode` cannot be used on function components.

* * *

Expand Down
4 changes: 2 additions & 2 deletions content/docs/reference-test-renderer.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Unmount the in-memory tree, triggering the appropriate lifecycle events.
testRenderer.getInstance()
```

Return the instance corresponding to the root element, if available. This will not work if the root element is a functional component because they don't have instances.
Return the instance corresponding to the root element, if available. This will not work if the root element is a function component because they don't have instances.

### `testRenderer.root`

Expand Down Expand Up @@ -206,7 +206,7 @@ Find all descendant test instances with the provided `props`.
testInstance.instance
```

The component instance corresponding to this test instance. It is only available for class components, as functional components don't have instances. It matches the `this` value inside the given component.
The component instance corresponding to this test instance. It is only available for class components, as function components don't have instances. It matches the `this` value inside the given component.

### `testInstance.type`

Expand Down
Loading