Skip to content

Commit d30e222

Browse files
committed
add create-stateless-function-component.md
1 parent 5b807f5 commit d30e222

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Creating Stateless Function Components
2+
3+
When a component is purely a result of `props` alone, no `state`, the component can be written as a pure function avoiding the need to create a React component instance.
4+
5+
In the example below `MyComponent` is the result of a function that returns the results from `React.createElement()`.
6+
7+
<p data-height="265" data-theme-id="dark" data-slug-hash="ELrJWN" data-default-tab="js,result" data-user="Bunlong" data-embed-version="2" data-pen-title="ELrJWN" class="codepen">See the Pen <a href="https://codepen.io/Bunlong/pen/ELrJWN/">ELrJWN</a> by Bunlong (<a href="https://codepen.io/Bunlong">@Bunlong</a>) on <a href="https://codepen.io">CodePen</a>.</p>
8+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
9+
10+
Having look at the same code not using JSX should clarify what is going on.
11+
12+
```javascript
13+
var MyComponent = function MyComponent(props) {
14+
return React.createElement(
15+
"div",
16+
null,
17+
"Hello ",
18+
props.name
19+
);
20+
};
21+
22+
ReactDOM.render(React.createElement(MyComponent, { name: "doug" }), app);
23+
```
24+
25+
Constructing a React component without create React class is typically referred to as a stateless function component.
26+
27+
Stateless function components can't be passed component options (i.e. `render`, `componentWillUnmount`, etc.). However `prop-types` can be used.
28+
29+
The example below demonstrates a stateless function component making use of `prop-types`.
30+
31+
<p data-height="265" data-theme-id="dark" data-slug-hash="OZdGEG" data-default-tab="js,result" data-user="Bunlong" data-embed-version="2" data-pen-title="OZdGEG" class="codepen">See the Pen <a href="https://codepen.io/Bunlong/pen/OZdGEG/">OZdGEG</a> by Bunlong (<a href="https://codepen.io/Bunlong">@Bunlong</a>) on <a href="https://codepen.io">CodePen</a>.</p>
32+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
33+
34+
Note:
35+
36+
* Make as many of your components as possible, as stateless components.

0 commit comments

Comments
 (0)