Skip to content

Commit 4b14c00

Browse files
committed
function component State Container and props validation
1 parent d8cfbb4 commit 4b14c00

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/app/containers/MainContainer.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class MainContainer extends Component {
9595
port.postMessage({ action: 'emptySnap' });
9696
}
9797

98-
// change the snapshot index, this will change the state shown in the state container but won't change the DOM
98+
// change the snapshot index
99+
// this will change the state shown in the state container but won't change the DOM
99100
handleChangeSnapshot(snapshotIndex) {
100101
// snapshotIndex
101102
// --> 1. affects the action that is highlighted

src/app/containers/StateContainer.jsx

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { MemoryRouter as Router, Route, NavLink } from 'react-router-dom';
3+
import PropTypes from 'prop-types';
34
import Tree from '../components/Tree';
45
import Chart from '../components/Chart';
56

6-
class StateContainer extends Component {
7-
constructor(props) {
8-
super(props);
9-
}
7+
const StateContainer = ({ snapshot }) => (
8+
<Router>
9+
<div className="state-container">
10+
<div className="navbar">
11+
<NavLink className="router-link" activeClassName="is-active" to="/tree">
12+
Tree
13+
</NavLink>
14+
<NavLink className="router-link" activeClassName="is-active" to="/chart">
15+
Chart
16+
</NavLink>
17+
</div>
18+
<Route path="/tree" render={() => <Tree snapshot={snapshot} />} />
19+
<Route path="/chart" render={() => <Chart snapshot={snapshot} />} />
20+
</div>
21+
</Router>
22+
);
1023

11-
render() {
12-
const { snapshot } = this.props;
13-
return (
14-
<Router>
15-
<div className="state-container">
16-
<div className="navbar">
17-
<NavLink className="router-link" activeClassName="is-active" to="/tree">
18-
Tree
19-
</NavLink>
20-
<NavLink className="router-link" activeClassName="is-active" to="/chart">
21-
Chart
22-
</NavLink>
23-
</div>
24-
<Route path="/tree" render={() => <Tree snapshot={snapshot} />} />
25-
<Route path="/chart" render={() => <Chart snapshot={snapshot} />} />
26-
</div>
27-
</Router>
28-
);
29-
}
30-
}
31-
{
32-
}
24+
StateContainer.propTypes = {
25+
snapshot: PropTypes.shape({
26+
state: PropTypes.oneOfType([
27+
PropTypes.string,
28+
PropTypes.object,
29+
]),
30+
children: PropTypes.arrayOf(
31+
PropTypes.object,
32+
),
33+
}).isRequired,
34+
};
3335

3436
export default StateContainer;

0 commit comments

Comments
 (0)