8000 add define-node-attributes-and-props.md · learnreactjs/learnreactjs@2925647 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2925647

Browse files
committed
add define-node-attributes-and-props.md
1 parent fd84f82 commit 2925647

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Defining Node Attributes/Props
2+
3+
The second argument that is passed to `React.createElement(type, props, children)` is an object containing name value properties (props).
4+
5+
Props has several roles:
6+
7+
* Props can become HTML attributes. If a prop matches a known HTML attribute then it will be added to the final HTML element in the DOM.
8+
* Props passed to `createElement()` become values stored in a `prop` object as an instance property of `React.createElement()` instances (i.e. `[INSTANCE].props.[NAME OF PROP]`). Props are normally used to input values into components.
9+
* A few special props have side effects (e.g. `key`, `ref`).
10+
11+
You can think of props as the configuration options for React nodes or you can think of them as HTML attributes.
12+
13+
In the example below I am defining a React `<option>` element node with five props. One is a non-standard HTML attribute (i.e. foo:'bar') and the others are known HTML attributes.
14+
15+
```
16+
var reactNodeOption = React.createElement('option',
17+
{
18+
foo: 'bar',
19+
// Note the use of the JS className property to change the
20+
// class attribute below
21+
className: 'one',
22+
'data-test': 'test',
23+
'aria-role' :'optionitem',
24+
// Note use of JS camel-cased CSS property backgroundColor below
25+
style: {backgroundColor: 'red'}
26+
},
27+
'one'
28+
);
29+
```
30+
31+
Once the above code is rendered to an HTML page the HTML created will look like:
32+
33+
```
34+
<li data-test="test" class="one" aria-role="optionitem" style="background-color: red;" data-reactid=".0">one</li>
35+
```
36+
37+
What you need to realize is only the following attributes get passed to the real DOM from the Virtual DOM:
38+
39+
* [Standard HTML attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes)
40+
* [Custom data attributes data-*](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes)
41+
* [Accessibility attributes aria-*](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA)
42+
43+
The `foo` attribute/prop does not show up in the real DOM. This non-standard HTML attribute is available as an instance property of the created `option` React node instance. (e.g. `reactNodeOption.props.foo`).
44+
45+
<p data-height="265" data-theme-id="dark" data-slug-hash="LmJGdY" data-default-tab="js,result" data-user="Bunlong" data-embed-version="2" data-pen-title="LmJGdY" class="codepen">See the Pen <a href="https://codepen.io/Bunlong/pen/LmJGdY/">LmJGdY</a> by Bunlong (<a href="https://codepen.io/Bunlong">@Bunlong</a>) on <a href="https://codepen.io">CodePen</a>.</p>
46+
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
47+
48+
React attributes/props not only translate to real HTML attributes props, they become configuration values that are passed to React components. This aspect of props will be covered in the React component props chapter. For now simply realize that passing a prop into a React node is different from defining a prop on a component to be used as configuration input within a component.

0 commit comments

Comments
 (0)
0