Skip to content

Commit bdd05d7

Browse files
committed
Refactor Stories to use mapDispatchToProps
1 parent 4ca630e commit bdd05d7

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## grpc-web-hacker-news
2-
An example app implementing a Hacker News reader. This example aims to demonstrate usage of [grpc-web](https://github.com/improbable-eng/grpc-web) with React. It additionally shows how to integrate with Redux.
2+
An example app implementing a Hacker News reader. This example aims to demonstrate usage of [grpc-web](https://github.com/improbable-eng/grpc-web)(v0.5.0) with React. It additionally shows how to integrate with Redux.
33

44
### Running
55
To start both the Go backend server and the frontend React application, run the following:
@@ -67,7 +67,7 @@ export default function (state: StoryState = initialState, action: RootAction):
6767
};
6868
}
6969
return state;
70-
70+
7171
default:
7272
return state;
7373
}

app/src/Stories.tsx

+17-11
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,16 @@ type StoriesProps = {
1212
stories: Story.AsObject[],
1313
loading: boolean,
1414
error: Error | null,
15-
dispatch: Dispatch<RootAction>,
1615
selected: Story.AsObject | null,
16+
17+
fetchStories: () => void,
18+
selectStory: (id: number) => void,
1719
};
1820

1921
class Stories extends React.Component<StoriesProps, {}> {
2022

21-
constructor(props: StoriesProps) {
22-
super(props);
23-
this.state = {
24-
selected: null,
25-
};
26-
}
27-
2823
componentDidMount() {
29-
this.props.dispatch(listStories());
24+
this.props.fetchStories();
3025
}
3126

3227
render() {
@@ -39,7 +34,7 @@ class Stories extends React.Component<StoriesProps, {}> {
3934
<StoryList
4035
selected={this.props.selected}
4136
stories={this.props.stories}
42-
onStorySelect={(id: number) => this.props.dispatch(selectStory(id))}
37+
onStorySelect={this.props.selectStory}
4338
/>
4439
</Grid.Column>
4540

@@ -66,4 +61,15 @@ function mapStateToProps(state: RootState) {
6661
};
6762
}
6863

69-
export default connect(mapStateToProps)(Stories);
64+
function mapDispatchToProps(dispatch: Dispatch<RootAction>) {
65+
return {
66+
fetchStories: () => {
67+
dispatch(listStories());
68+
},
69+
selectStory: (storyId: number) => {
70+
dispatch(selectStory(storyId));
71+
},
72+
};
73+
}
74+
75+
export default connect(mapStateToProps, mapDispatchToProps)(Stories);

0 commit comments

Comments
 (0)