Skip to content

Commit

Permalink
complete set up for making a successful request with redux-thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
ohansemmanuel committed Aug 7, 2018
1 parent 0cf239b commit 589c2e1
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 6 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"mo-js": "^0.288.2",
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-redux": "^5.0.7",
"react-scripts": "1.1.4",
"redux": "^4.0.0",
"redux-thunk": "^2.3.0",
"styled-components": "^3.4.1"
},
"scripts": {
Expand Down
17 changes: 17 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import axios from "axios";
import { SET_ARTICLE_DETAILS } from "./types";

export function fetchArticleDetails() {
return function(dispatch) {
return axios.get("https://api.myjson.com/bins/19dtxc").then(({ data }) => {
dispatch(setArticleDetails(data));
});
};
}

function setArticleDetails(data) {
return {
type: SET_ARTICLE_DETAILS,
payload: data
};
}
2 changes: 2 additions & 0 deletions src/actions/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const FETCH_ARTICLE_DETAILS = "FETCH_ARTICLE_DETAILS";
export const SET_ARTICLE_DETAILS = "SET_ARTICLE_DETAILS";
3 changes: 2 additions & 1 deletion src/components/Clap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Clap extends React.Component {
super(props);
this.state = {
count: 0,
countTotal: this._generateRandomNumber(500, 10000),
countTotal:
this.props.countTotal || this._generateRandomNumber(500, 10000),
isClicked: false
};
this._handleClick = this._handleClick.bind(this);
Expand Down
15 changes: 14 additions & 1 deletion src/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React, { Component } from "react";
import { connect } from "react-redux";
import styled from "styled-components";
import Article from "../components/Article";
import Clap from "../components/Clap";
import { fetchArticleDetails } from "../actions";

const StyledApp = styled.div`
min-height: 100vh;
Expand All @@ -21,6 +23,9 @@ const StyledApp = styled.div`

class App extends Component {
state = {};
componentDidMount() {
this.props.fetchArticleDetails();

This comment has been minimized.

Copy link
@rafalkrupinski

rafalkrupinski May 16, 2019

I don't understand this line, how did fetchArticleDetails end up in this.props?

This comment has been minimized.

Copy link
@emanuelbierman

emanuelbierman May 20, 2019

@rafalkrupinski The component receives it as props from mapDispatchToProps in line 49 of the same file. When you import the { connect } function in line 6, it is called with two arguments (though you can set either to null): the first is mapStateToProps, the second is mapDispatchToProps. source

This comment has been minimized.

Copy link
@varshan09

varshan09 Jun 27, 2020

@emanuelbierman You haven't added mapDispatchToProps in this commit, that's the reasons of confusion.

This comment has been minimized.

Copy link
@tobywhughes

tobywhughes Sep 28, 2020

To clarify for anybody new to redux strolling through, mapDispatchToProps is in this commit, but using the shorthand.

https://react-redux.js.org/api/connect#object-shorthand-form

}
render() {
return (
<StyledApp>
Expand All @@ -35,4 +40,12 @@ class App extends Component {
}
}

export default App;
const mapStateToProps = ({ data = {} }) => ({
data
});
export default connect(
mapStateToProps,
{
fetchArticleDetails
}
)(App);
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import "./index.css";
import App from "./containers/App";
import registerServiceWorker from "./registerServiceWorker";
import store from "./store";

ReactDOM.render(<App />, document.getElementById("root"));
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
registerServiceWorker();
10 changes: 10 additions & 0 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { SET_ARTICLE_DETAILS } from "../actions/types";

export default function(state = {}, action) {
switch (action.type) {
case SET_ARTICLE_DETAILS:
return { data: action.payload };
default:
return state;
}
}
6 changes: 6 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createStore, applyMiddleware } from "redux";
import thunk from "redux-thunk";
import rootReducer from "../reducers";

const store = createStore(rootReducer, applyMiddleware(thunk));
export default store;
43 changes: 40 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ aws4@^1.6.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"

axios@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.18.0.tgz#32d53e4851efdc0a11993b6cd000789d70c05102"
dependencies:
follow-redirects "^1.3.0"
is-buffer "^1.1.5"

axobject-query@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-0.1.0.tgz#62f59dbc59c9f9242759ca349960e7a2fe3c36c0"
Expand Down Expand Up @@ -2805,7 +2812,7 @@ flatten@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/flatten/-/flatten-1.0.2.tgz#dae46a9d78fbe25292258cc1e780a41d95c03782"

follow-redirects@^1.0.0:
follow-redirects@^1.0.0, follow-redirects@^1.3.0:
version "1.5.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.2.tgz#5a9d80e0165957e5ef0c1210678fc5c4acb9fb03"
dependencies:
Expand Down Expand Up @@ -3387,7 +3394,7 @@ interpret@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614"

invariant@^2.2.2:
invariant@^2.0.0, invariant@^2.2.2:
version "2.2.4"
resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.4.tgz#610f3c92c9359ce1db616e538008d23ff35158e6"
dependencies:
Expand Down Expand Up @@ -4230,6 +4237,10 @@ locate-path@^2.0.0:
p-locate "^2.0.0"
path-exists "^3.0.0"

lodash-es@^4.17.5:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.10.tgz#62cd7104cdf5dd87f235a837f0ede0e8e5117e05"

lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -4271,7 +4282,7 @@ lodash.uniq@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"

"lodash@>=3.5 <5", lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.3.0:
"lodash@>=3.5 <5", lodash@^4.15.0, lodash@^4.17.10, lodash@^4.17.2, lodash@^4.17.3, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down Expand Up @@ -5589,6 +5600,17 @@ react-is@^16.3.1:
version "16.4.2"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.4.2.tgz#84891b56c2b6d9efdee577cc83501dfc5ecead88"

react-redux@^5.0.7:
version "5.0.7"
resolved "https://registry.yarnpkg.com/react-redux/-/react-redux-5.0.7.tgz#0dc1076d9afb4670f993ffaef44b8f8c1155a4c8"
dependencies:
hoist-non-react-statics "^2.5.0"
invariant "^2.0.0"
lodash "^4.17.5"
lodash-es "^4.17.5"
loose-envify "^1.1.0"
prop-types "^15.6.0"

[email protected]:
version "1.1.4"
resolved "https://registry.yarnpkg.com/react-scripts/-/react-scripts-1.1.4.tgz#d5c230e707918d6dd2d06f303b10f5222d017c88"
Expand Down Expand Up @@ -5730,6 +5752,17 @@ reduce-function-call@^1.0.1:
dependencies:
balanced-match "^0.4.2"

redux-thunk@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622"

redux@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/redux/-/redux-4.0.0.tgz#aa698a92b729315d22b34a0553d7e6533555cc03"
dependencies:
loose-envify "^1.1.0"
symbol-observable "^1.2.0"

regenerate@^1.2.1:
version "1.4.0"
resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.4.0.tgz#4a856ec4b56e4077c557589cae85e7a4c8869a11"
Expand Down Expand Up @@ -6505,6 +6538,10 @@ sw-toolbox@^3.4.0:
path-to-regexp "^1.0.1"
serviceworker-cache-polyfill "^4.0.0"

symbol-observable@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.2.0.tgz#c22688aed4eab3cdc2dfeacbb561660560a00804"

symbol-tree@^3.2.1:
version "3.2.2"
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.2.tgz#ae27db38f660a7ae2e1c3b7d1bc290819b8519e6"
Expand Down

0 comments on commit 589c2e1

Please sign in to comment.