Skip to content

Commit cecc3e3

Browse files
committed
removed prop drilling and used context
1 parent 79aa9bd commit cecc3e3

File tree

6 files changed

+53
-78
lines changed

6 files changed

+53
-78
lines changed

src/app/components/App.jsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
import React from 'react';
1+
import React, { useReducer } from 'react';
22
import MainContainer from '../containers/MainContainer';
3+
import { StoreContext } from '../store';
4+
import mainReducer from '../reducers/mainReducer';
35

4-
const App = () => <MainContainer />;
6+
const initialState = {
7+
port: null,
8+
sliderIndex: 0,
9+
viewIndex: -1,
10+
intervalId: null,
11+
playing: false,
12+
snapshots: [],
13+
mode: {
14+
locked: false,
15+
paused: false,
16+
persist: false,
17+
},
18+
};
19+
20+
function App() {
21+
return (
22+
<StoreContext.Provider value={useReducer(mainReducer, initialState)}>
23+
<MainContainer />
24+
</StoreContext.Provider>
25+
);
26+
}
527

628
export default App;

src/app/containers/ActionContainer.jsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
/* eslint-disable react/no-array-index-key */
12
import React from 'react';
2-
import PropTypes from 'prop-types';
33
import Action from '../components/Action';
44

55
import { emptySnapshots } from '../actions/actions';
6+
import { useStoreContext } from '../store';
67

7-
const ActionContainer = ({
8-
snapshots,
9-
sliderIndex,
10-
viewIndex,
11-
dispatch,
12-
}) => {
8+
function ActionContainer() {
9+
const [{ snapshots, sliderIndex, viewIndex }, dispatch] = useStoreContext();
1310
let actionsArr = [];
11+
// build actions array
1412
if (snapshots.length > 0) {
1513
actionsArr = snapshots.map((snapshot, index) => {
1614
const selected = index === viewIndex;
@@ -35,13 +33,6 @@ const ActionContainer = ({
3533
<div>{actionsArr}</div>
3634
</div>
3735
);
38-
};
39-
40-
ActionContainer.propTypes = {
41-
snapshots: PropTypes.arrayOf(PropTypes.object).isRequired,
42-
dispatch: PropTypes.func.isRequired,
43-
sliderIndex: PropTypes.number.isRequired,
44-
viewIndex: PropTypes.number.isRequired,
45-
};
36+
}
4637

4738
export default ActionContainer;

src/app/containers/ButtonsContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useContext } from 'react';
1+
import React from 'react';
22

33
import {
44
importSnapshots, toggleMode,
55
} from '../actions/actions';
6-
import StoreContext from '../store';
6+
import { useStoreContext } from '../store';
77

88
function exportHandler(snapshots) {
99
// create invisible download anchor link
@@ -36,7 +36,7 @@ function importHandler(dispatch) {
3636
}
3737

3838
function ButtonsContainer() {
39-
const [{ snapshots, mode: { paused, locked, persist } }, dispatch] = useContext(StoreContext);
39+
const [{ snapshots, mode: { paused, locked, persist } }, dispatch] = useStoreContext();
4040
return (
4141
<div className="buttons-container">
4242
<button className="pause-button" type="button" onClick={() => dispatch(toggleMode('paused'))}>

src/app/containers/MainContainer.jsx

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
import React, { useReducer, useEffect } from 'react';
1+
import React, { useEffect } from 'react';
22
import HeadContainer from './HeadContainer';
33
import ActionContainer from './ActionContainer';
44
import StateContainer from './StateContainer';
55
import TravelContainer from './TravelContainer';
66
import ButtonsContainer from './ButtonsContainer';
77

8-
import mainReducer from '../reducers/mainReducer';
98
import { addNewSnapshots, initialConnect, setPort } from '../actions/actions';
10-
import StoreContext from '../store';
9+
import { useStoreContext } from '../store';
1110

12-
const initialState = {
13-
port: null,
14-
sliderIndex: 0,
15-
viewIndex: -1,
16-
intervalId: null,
17-
playing: false,
18-
snapshots: [],
19-
mode: {
20-
locked: false,
21-
paused: false,
22-
persist: false,
23-
},
24-
};
2511

2612
function MainContainer() {
27-
const [mainState, dispatch] = useReducer(mainReducer, initialState);
13+
const [mainState, dispatch] = useStoreContext();
2814

2915
useEffect(() => {
3016
if (mainState.port) return;
@@ -63,38 +49,20 @@ function MainContainer() {
6349
snapshots,
6450
sliderIndex,
6551
viewIndex,
66-
playing,
67-
mode,
6852
} = mainState;
6953

7054
// if viewIndex is -1, then use the sliderIndex instead
7155
const snapshotView = (viewIndex === -1) ? snapshots[sliderIndex] : snapshots[viewIndex];
7256
return (
73-
<StoreContext.Provider value={[mainState, dispatch]}>
74-
<div className="main-container">
75-
<HeadContainer />
76-
<div className="body-container">
77-
<ActionContainer
78-
snapshots={snapshots}
79-
sliderIndex={sliderIndex}
80-
viewIndex={viewIndex}
81-
dispatch={dispatch}
82-
/>
83-
{(snapshots.length) ? <StateContainer snapshot={snapshotView} /> : null}
84-
<TravelContainer
85-
snapshotsLength={snapshots.length}
86-
sliderIndex={sliderIndex}
87-
playing={playing}
88-
dispatch={dispatch}
89-
/>
90-
<ButtonsContainer
91-
mode={mode}
92-
dispatch={dispatch}
93-
snapshots={mainState.snapshots}
94-
/>
95-
</div>
57+
<div className="main-container">
58+
<HeadContainer />
59+
<div className="body-container">
60+
<ActionContainer />
61+
{(snapshots.length) ? <StateContainer snapshot={snapshotView} /> : null}
62+
<TravelContainer snapshotsLength={snapshots.length} />
63+
<ButtonsContainer />
9664
</div>
97-
</StoreContext.Provider>
65+
</div>
9866
);
9967
}
10068

src/app/containers/TravelContainer.jsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
33
import MainSlider from '../components/MainSlider';
44
import Dropdown from '../components/Dropdown';
5-
65
import {
76
playForward, pause, startPlaying, moveForward, moveBackward,
87
} from '../actions/actions';
8+
import { useStoreContext } from '../store';
99

1010
const speeds = [
1111
{ value: 2000, label: '0.5x' },
1212
{ value: 1000, label: '1.0x' },
1313
{ value: 500, label: '2.0x' },
1414
];
1515

16+
// start slider movement
1617
function play(speed, playing, dispatch, snapshotsLength, sliderIndex) {
1718
if (playing) {
1819
dispatch(pause());
@@ -30,15 +31,9 @@ function play(speed, playing, dispatch, snapshotsLength, sliderIndex) {
3031
}
3132
}
3233

33-
function TravelContainer(props) {
34+
function TravelContainer({ snapshotsLength }) {
3435
const [selectedSpeed, setSpeed] = useState(speeds[1]);
35-
36-
const {
37-
snapshotsLength,
38-
sliderIndex,
39-
playing,
40-
dispatch,
41-
} = props;
36+
const [{ sliderIndex, playing }, dispatch] = useStoreContext();
4237

4338
return (
4439
<div className="travel-container">
@@ -63,13 +58,10 @@ function TravelContainer(props) {
6358
/>
6459
</div>
6560
);
66-
};
61+
}
6762

6863

6964
TravelContainer.propTypes = {
70-
dispatch: PropTypes.func.isRequired,
7165
snapshotsLength: PropTypes.number.isRequired,
72-
sliderIndex: PropTypes.number.isRequired,
73-
playing: PropTypes.bool.isRequired,
7466
};
7567
export default TravelContainer;

src/app/store.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22

3-
export default React.createContext();
3+
export const StoreContext = React.createContext();
4+
5+
export const useStoreContext = () => useContext(StoreContext);

0 commit comments

Comments
 (0)