-
Notifications
You must be signed in to change notification settings - Fork 404
/
07-property-example.html
40 lines (31 loc) · 1.17 KB
/
07-property-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!doctype html>
<title>07 Property Example - React From Zero</title>
<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/prop-types.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<div id="app"></div>
<script type="text/babel">
// Here's a more practical example of a component, it formats a date
// and returns a <span> containing that formatted string.
function DateSpan(props) {
var date = props.date,
day = date.getDate(),
month = date.getMonth() + 1,
year = date.getFullYear();
return (
<span>
{day}.{month}.{year}
</span>
);
}
// Also a more sophisticated type check for the date property
// The property is required, because there are no defaults set
DateSpan.propTypes = {
date: PropTypes.instanceOf(Date).isRequired
};
// We have to supply a date object, the component does the formatting
var reactElement = <DateSpan date={new Date()} />;
var renderTarget = document.getElementById("app");
ReactDOM.render(reactElement, renderTarget);
</script>