Skip to content

Commit 5e39a27

Browse files
committed
Higher-Order Components
1 parent a326058 commit 5e39a27

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/App.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,12 @@ class App extends Component {
155155
/>
156156
}
157157
<div className="interactions">
158-
{ isLoading
159-
? <Loading />
160-
: <Button
161-
onClick={() => this.fetchSearchTopStories(searchKey, page + 1)}
162-
>
163-
More
164-
</Button>
165-
}
158+
<ButtonWithLoading
159+
isLoading={isLoading}
160+
onClick={() => this.fetchSearchTopStories(searchKey, page + 1)}
161+
>
162+
More
163+
</ButtonWithLoading>
166164
</div>
167165
</div>
168166
);
@@ -231,6 +229,13 @@ const Button = ({
231229
const Loading = () =>
232230
<div>Loading ...</div>
233231

232+
const withLoading = (Component) => ({ isLoading, ...rest }) =>
233+
isLoading
234+
? <Loading />
235+
: <Component { ...rest } />
236+
237+
const ButtonWithLoading = withLoading(Button);
238+
234239
export default App;
235240

236241
export {

src/__snapshots__/App.test.js.snap

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ exports[`App has a valid snapshot 1`] = `
2828
<div
2929
className="interactions"
3030
>
31-
<button
32-
className=""
33-
onClick={[Function]}
34-
type="button"
35-
>
36-
More
37-
</button>
31+
<div>
32+
Loading ...
33+
</div>
3834
</div>
3935
</div>
4036
`;

0 commit comments

Comments
 (0)