Skip to content

Commit 02b68d2

Browse files
committed
functional stateless components refactoring
1 parent b1ca0fb commit 02b68d2

File tree

5 files changed

+52
-73
lines changed

5 files changed

+52
-73
lines changed

src/components/App.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import './App.css';
33

44
import Stories from './Stories';
55
import SearchStories from './SearchStories';
66

7-
class App extends Component {
8-
render() {
9-
return (
10-
<div className="app">
11-
<div className="interactions">
12-
<SearchStories />
13-
</div>
14-
<Stories />
15-
</div>
16-
);
17-
}
18-
}
7+
const App = () =>
8+
<div className="app">
9+
<div className="interactions">
10+
<SearchStories />
11+
</div>
12+
<Stories />
13+
</div>
1914

2015
export default App;

src/components/Buttons.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ const Button = ({
3030
export default Button;
3131

3232
export {
33-
Button,
3433
ButtonInline,
3534
};

src/components/SearchStories.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import { connect } from 'react-redux';
33
import { doFetchStories } from '../actions/story';
4-
import { Button } from './Buttons';
4+
import Button from './Buttons';
55

66
const applyQueryState = query => () => ({
77
query

src/components/Stories.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { connect } from 'react-redux';
33
import { getReadableStories } from '../selectors/story';
44
import './Stories.css';
@@ -27,25 +27,18 @@ const COLUMNS = {
2727
},
2828
};
2929

30-
class Stories extends Component {
31-
render() {
32-
const { stories } = this.props;
30+
const Stories = ({ stories }) =>
31+
<div className="stories">
32+
<StoriesHeader columns={COLUMNS} />
3333

34-
return (
35-
<div className="stories">
36-
<StoriesHeader columns={COLUMNS} />
37-
38-
{(stories || []).map(story =>
39-
<Story
40-
key={story.objectID}
41-
story={story}
42-
columns={COLUMNS}
43-
/>
44-
)}
45-
</div>
46-
);
47-
}
48-
}
34+
{(stories || []).map(story =>
35+
<Story
36+
key={story.objectID}
37+
story={story}
38+
columns={COLUMNS}
39+
/>
40+
)}
41+
</div>
4942

5043
const StoriesHeader = ({ columns }) =>
5144
<div className="stories-header">

src/components/Story.js

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,41 @@
1-
import React, { Component } from 'react';
1+
import React from 'react';
22
import { connect } from 'react-redux';
33
import { doArchiveStory } from '../actions/archive';
44
import './Story.css';
55

66
import { ButtonInline } from './Buttons';
77

8-
class Story extends Component {
9-
render() {
10-
const {
11-
story,
12-
columns,
13-
onArchive,
14-
} = this.props;
8+
const Story = ({ story, columns, onArchive }) => {
9+
const {
10+
title,
11+
url,
12+
author,
13+
num_comments,
14+
points,
15+
objectID,
16+
} = story;
1517

16-
const {
17-
title,
18-
url,
19-
author,
20-
num_comments,
21-
points,
22-
objectID,
23-
} = story;
24-
25-
return (
26-
<div className="story">
27-
<span style={{ width: columns.title.width }}>
28-
<a href={url}>{title}</a>
29-
</span>
30-
<span style={{ width: columns.author.width }}>
31-
{author}
32-
</span>
33-
<span style={{ width: columns.comments.width }}>
34-
{num_comments}
35-
</span>
36-
<span style={{ width: columns.points.width }}>
37-
{points}
38-
</span>
39-
<span style={{ width: columns.archive.width }}>
40-
<ButtonInline onClick={() => onArchive(objectID)}>
41-
Archive
42-
</ButtonInline>
43-
</span>
44-
</div>
45-
);
46-
}
18+
return (
19+
<div className="story">
20+
<span style={{ width: columns.title.width }}>
21+
<a href={url}>{title}</a>
22+
</span>
23+
<span style={{ width: columns.author.width }}>
24+
{author}
25+
</span>
26+
<span style={{ width: columns.comments.width }}>
27+
{num_comments}
28+
</span>
29+
<span style={{ width: columns.points.width }}>
30+
{points}
31+
</span>
32+
<span style={{ width: columns.archive.width }}>
33+
<ButtonInline onClick={() => onArchive(objectID)}>
34+
Archive
35+
</ButtonInline>
36+
</span>
37+
</div>
38+
);
4739
}
4840

4941
const mapDispatchToProps = dispatch => ({

0 commit comments

Comments
 (0)