Skip to content

Commit fd979d2

Browse files
committed
Some syntax updates
1 parent ca90aec commit fd979d2

File tree

5 files changed

+33
-42
lines changed

5 files changed

+33
-42
lines changed

subjects/07-Higher-Order-Components/exercise.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// Exercise:
33
//
44
// - Make the mouse-tracking logic reusable by filling in the `withMouse`
5-
// higher-order component and returning a new component that renders a
6-
// `ComposedComponent` element with the current mouse position as props
7-
// - Use the `withMouse` function you just wrote to create an `AppWithMouse`
8-
// component
9-
// - Render <AppWithMouse> instead of <App>
5+
// higher-order component and returning a new component that renders the
6+
// given component with a `mouse` prop
107
//
118
// Got extra time?
129
//
@@ -18,8 +15,10 @@ import React from "react";
1815
import ReactDOM from "react-dom";
1916
import PropTypes from "prop-types";
2017

21-
function withMouse(ComposedComponent) {
22-
// TODO
18+
function withMouse(Component) {
19+
// TODO: Return a *new* component class that renders
20+
// the given Component with a `mouse` prop
21+
return Component;
2322
}
2423

2524
class App extends React.Component {
@@ -42,4 +41,6 @@ class App extends React.Component {
4241
}
4342
}
4443

45-
ReactDOM.render(<App />, document.getElementById("app"));
44+
const AppWithMouse = withMouse(App);
45+
46+
ReactDOM.render(<AppWithMouse />, document.getElementById("app"));

subjects/07-Higher-Order-Components/lecture.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ const media = createMediaListener({
1010
});
1111

1212
class App extends React.Component {
13-
state = {
14-
media: media.getState()
15-
};
13+
state = { media: media.getState() };
1614

1715
componentDidMount() {
1816
media.listen(media => this.setState({ media }));
@@ -42,13 +40,11 @@ ReactDOM.render(<App />, document.getElementById("app"));
4240
// component (HOC) is a function that takes a `Component` as an argument, and
4341
// returns a new component renders that `Component` with some extra props.
4442

45-
// const mediaComponent = (Component, queries) => {
43+
// function withMedia(Component, queries) {
4644
// const media = createMediaListener(queries);
4745

4846
// return class extends React.Component {
49-
// state = {
50-
// media: media.getState()
51-
// };
47+
// state = { media: media.getState() };
5248

5349
// componentDidMount() {
5450
// media.listen(media => this.setState({ media }));
@@ -62,7 +58,7 @@ ReactDOM.render(<App />, document.getElementById("app"));
6258
// return <Component {...this.props} media={this.state.media} />;
6359
// }
6460
// };
65-
// };
61+
// }
6662

6763
// class App extends React.Component {
6864
// static propTypes = {
@@ -85,7 +81,7 @@ ReactDOM.render(<App />, document.getElementById("app"));
8581
// }
8682
// }
8783

88-
// const AppWithMedia = mediaComponent(App, {
84+
// const AppWithMedia = withMedia(App, {
8985
// big: "(min-width : 1000px)",
9086
// tiny: "(max-width: 400px)"
9187
// });

subjects/07-Higher-Order-Components/solution-extra.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
////////////////////////////////////////////////////////////////////////////////
22
// Exercise:
33
//
4-
// Make `withMouse` a "higher-order component" that sends the mouse position
5-
// to the component as props (hint: use `event.clientX` and `event.clientY`).
4+
// - Make the mouse-tracking logic reusable by filling in the `withMouse`
5+
// higher-order component and returning a new component that renders the
6+
// given component with a `mouse` prop
67
//
78
// Got extra time?
89
//
9-
// Make a `withCat` HOC that shows a cat chasing the mouse around the screen!
10+
// - Make a `withCat` HOC that shows a cat chasing the mouse around the screen!
1011
////////////////////////////////////////////////////////////////////////////////
1112
import "./styles.css";
1213

@@ -19,10 +20,7 @@ function withMouse(Component) {
1920
state = { x: 0, y: 0 };
2021

2122
handleMouseMove = event => {
22-
this.setState({
23-
x: event.clientX,
24-
y: event.clientY
25-
});
23+
this.setState({ x: event.clientX, y: event.clientY });
2624
};
2725

2826
render() {

subjects/07-Higher-Order-Components/solution.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@
22
// Exercise:
33
//
44
// - Make the mouse-tracking logic reusable by filling in the `withMouse`
5-
// higher-order component and returning a new component that renders a
6-
// `ComposedComponent` element with the current mouse position as props
7-
// - Use the `withMouse` function you just wrote to create an `AppWithMouse`
8-
// component
9-
// - Render <AppWithMouse> instead of <App>
5+
// higher-order component and returning a new component that renders the
6+
// given component with a `mouse` prop
107
//
118
// Got extra time?
129
//
@@ -23,10 +20,7 @@ function withMouse(Component) {
2320
state = { x: 0, y: 0 };
2421

2522
handleMouseMove = event => {
26-
this.setState({
27-
x: event.clientX,
28-
y: event.clientY
29-
});
23+
this.setState({ x: event.clientX, y: event.clientY });
3024
};
3125

3226
render() {

subjects/07-Higher-Order-Components/utils/createMediaListener.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ listener.getState()
99
listenter.dispose()
1010
*/
1111

12-
export default media => {
12+
function createMediaListener(queries) {
1313
let transientListener = null;
1414

15-
const mediaKeys = Object.keys(media);
15+
const keys = Object.keys(queries);
1616

17-
const queryLists = mediaKeys.reduce((queryLists, key) => {
18-
queryLists[key] = window.matchMedia(media[key]);
17+
const queryLists = keys.reduce((queryLists, key) => {
18+
queryLists[key] = window.matchMedia(queries[key]);
1919
return queryLists;
2020
}, {});
2121

22-
const mediaState = mediaKeys.reduce((state, key) => {
22+
const mediaState = keys.reduce((state, key) => {
2323
state[key] = queryLists[key].matches;
2424
return state;
2525
}, {});
@@ -28,7 +28,7 @@ export default media => {
2828
if (transientListener != null) transientListener(mediaState);
2929
};
3030

31-
const listeners = mediaKeys.reduce((listeners, key) => {
31+
const listeners = keys.reduce((listeners, key) => {
3232
listeners[key] = event => {
3333
mediaState[key] = event.matches;
3434
notify();
@@ -39,19 +39,21 @@ export default media => {
3939

4040
const listen = listener => {
4141
transientListener = listener;
42-
mediaKeys.forEach(key => {
42+
keys.forEach(key => {
4343
queryLists[key].addListener(listeners[key]);
4444
});
4545
};
4646

4747
const dispose = () => {
4848
transientListener = null;
49-
mediaKeys.forEach(key => {
49+
keys.forEach(key => {
5050
queryLists[key].removeListener(listeners[key]);
5151
});
5252
};
5353

5454
const getState = () => mediaState;
5555

5656
return { listen, dispose, getState };
57-
};
57+
}
58+
59+
export default createMediaListener;

0 commit comments

Comments
 (0)