-
Notifications
You must be signed in to change notification settings - Fork 32
/
mvc_react.html
56 lines (48 loc) · 1.98 KB
/
mvc_react.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>React MVC Example</title>
</head>
<body>
<div id="root"></div>
<script src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
/*
In this example, I've created a simple Model (an array of photo objects), a View component Photo that receives photo data as props, and a Controller component PhotoList that maps over the photos and renders a Photo component for each item. The application is then rendered using ReactDOM.render.
*/
// Model
const photos = [
{ caption: 'Sample Photo 1', src: 'https://via.placeholder.com/300x200?text=Sample+Photo+1', metadata: 'Some metadata for photo 1' },
{ caption: 'Sample Photo 2', src: 'https://via.placeholder.com/300x200?text=Sample+Photo+2', metadata: 'Some metadata for photo 2' },
];
// View
function Photo(props) {
const { photo } = props;
return (
<li className="photo">
<h2>{photo.caption}</h2>
<img className="source" src={photo.src} />
<div className="metadata">{photo.metadata}</div>
</li>
);
}
// Controller
function PhotoList(props) {
const { photos } = props;
return (
<ul>
{photos.map((photo, index) => (
<Photo key={index} photo={photo} />
))}
</ul>
);
}
// Render the application
ReactDOM.render(<PhotoList photos={photos} />, document.getElementById('root'));
</script>
</body>
</html>