-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTutorial.jsx
186 lines (141 loc) · 4.28 KB
/
Tutorial.jsx
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React from 'react';
// REACT
// Composable Components
// The V in MVC
// Everything else is external: Routing, State management, Forms, Validation, Data fetching, ...
// Virtual DOM
// Readonly: props (=NG @Input()) and state
// Components do not need to have a visible part, they can also be used to encapsulate behavior or data
const App = () => (
<div>
<Header isMobile={window.innerWidth < 400} />
<Menu />
<Content style={{ fontSize: 15, color: 'red' }}>
<Row>
<Col span={12}>
<Title>Page Title</Title>
</Col>
</Row>
<Row>
<Col span={6}>
<OurServices />
</Col>
<Col span={6}>
<WhoWeAre />
</Col>
</Row>
<Row>
<Col span={12}>
<BlogLastEntries />
</Col>
</Row>
</Content>
<Footer />
</div>
);
const Header = ({ isMobile }) => (
<div>
<Brand />
<HeaderLinks />
{isMobile ? null : <SearchBar />}
{isMobile && <LanguageSelector />}
</div>
);
import './styles.css';
const Content = ({ children, ...rest }) => (
// const rest = {styles: '...'};
<div className="container" {...rest}>
{children}
</div>
);
class HeaderLinks extends React.Component {
constructor() {
super();
this.state = { open: false };
}
render() {
if (window.innerWidth < 400) {
// Mobile Devices
if (this.state.open) {
const mobileMenuOptions = ['docs', 'blog'];
return mobileMenuOptions.map(menu => (
<Link key={menu} to={meny}>
{menu.toUpperCase()}
</Link>
));
}
return <button onClick={() => this.setState({ open: !this.state.open })}>Hamburger</button>
}
// Desktops
return [
<Link key="docs" to="/docs">Docs</Link>,
<Link key="tuts" to="/tutorial">Tutorial</Link>,
<Link key="blog" to="/blog">Blog</Link>,
];
}
_renderLinks() {
// Don't do this!
// Create a new component instead!
return [<Link />, <Link />];
}
}
import PropTypes from 'prop-types';
HeaderLinks.propTypes = {
isMobile: PropTypes.bool.isRequired,
// string, number, func, element,
// object, instanceOf(T), any,
// oneOf(['a', 'b', 'c'])
// shape({errors: PropTypes.arrayOf(Error)})
}
HeaderLinks.defaultProps = {isMobile: true};
class OurServices extends React.Component {
constructor() {
super();
this.state = { services: null };
}
componentDidMount() { // ngOnInit
fetch('/api/our-services').then(result => {
this.setState({ services: result.json() });
}).catch(() => {
this.setState({ services: 'error' });
});
}
componentWillUnmount() { // ngDestroy
// Cleanup
// ex: clearInterval, ...
// ex: Cleaning up DOM nodes from non-React stuff like ChartJS, ...
}
render() { // template: ``
if (!this.state.services) {
return <i className="fas fa-spinner fa-spin" />;
}
if (this.state.services === 'error') {
return <div>Error fetching services</div>;
}
const services = this.state.services
.filter(x => x.active)
.sort((a, b) => a.sortOrder - b.sortOrder)
return (
<div className="our-services">
{services.map(service => <OurService key={service.id} service={service} />)}
</div>
);
}
// All Lifecycle methods:
// MOUNTING:
// constructor()
// static getDerivedStateFromProps(props, state) // Used to be: componentWillReceiveProps()
// render()
// componentDidMount()
// UPDATING:
// static getDerivedStateFromProps(props, state)
// shouldComponentUpdate(nextProps, nextState)
// render()
// getSnapshotBeforeUpdate(prevProps, prevState) // Check
// componentDidUpdate()
}
import ReactDOM from 'react-dom';
ReactDOM.render(
<App />,
document.getElementById('root')
);