Skip to content

Commit e69c195

Browse files
committed
Readme added with full explanation of the project.
1 parent c0ebee8 commit e69c195

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Redux Github Viewier Example
2+
> See Redux in action with this simple project
3+
4+
### Explanation
5+
It's kind of tricky to grasp, and hard to see it by just receiving a bunch of random code. I hope that with the usage instructions I provide, things to look out for, and specific code examples, you might gain a more solid understanding of redux and how it works.
6+
7+
One other thing to note: There's a lot of things about this project aren't ideal, namely the file structure, module imports, error handling, lots of the css, and some other things that you wouldn't typically do in a real project. But for the sake of demo-ing redux, I skipped over a lot of those details.
8+
9+
### Getting Started
10+
Pull the repo, run `npm install && npm start`, and open up the browser to http://localhost:3000 to see the app.
11+
12+
In the input box, enter any Github username, and it will make a call to the Github public API, and display all of the information that gets returned in the boxes on the left side of the screen.
13+
The user entered will also appear on the right side of the screen in a gray box. As you enter more users, you have the ability to click through the other users to re-view their profiles.
14+
15+
### How Redux Comes Into Play
16+
With vanilla react, you could technically store all the profiles in the local state, and then cycle through previous ones that way, but as soon as you change views to another component, that state is lost and then reinitialized when the component mounts again.
17+
With redux, you're able to update the profiles in the *store*, and they persist there across views allowing access to that data at any point in the application.
18+
19+
### Seeing Redux In Action
20+
If you open up the dev tools while using the application, you will see that when you retrieve different users, there is a 'SAVE' action that is getting fired. If you expand this in the console, then you will see that it displays the `prevState`, the `action`, and the `nextState`.
21+
Viewing the console output from the logger can be confusing at times, so the actions are much clearer to see when you watch them through the redux dev tools, as found in the the following link:
22+
[Redux DevTools - Chrome Web Store.](https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd)
23+
24+
### What is Happening
25+
* Before the component is mounted, in the `AppContainer.js`, the `save` function, are passed to the component through the `mapDispatchToProps` function (:11, :16).
26+
* The `profiles` array from the state is passed into the component through the `mapStateToProps` function (:6, :15).
27+
* The `connect` method is a redux function that gives the ability for the app to be connected to the store.
28+
* From the `App` (./components/App.jsx) component, those two attributes are now available in `this.props`, as seen on lines :25 and :33.
29+
* When a profile is retrieved, that data is passed into `this.props.save` on line :25, which references the action on line :20 in ./redux/reducer.js.
30+
* `save` returns a function with `dispatch` as a parameter, which then enters into the reducer above on line :9.
31+
* The case statement matches on the `SAVE` action, and because the state in the redux store is immutable, we create a new profiles array from the current state, push the new profile onto the array, and then return a new state complete with the new profile.
32+
* Now, back in the App.jsx, because we called `mapStateToProps` in the AppContainer, that now gives `App` access to `profiles` as it updates from this.props.
33+
* On :26 of App.jsx, we call `setState()` after the action is fired, and that makes it so the DOM re-renders with the new profiles array available in the component, as seen on :33.
34+
* This process is repeated every time a new profile is searched.
35+
36+
### Other Aspects Explained
37+
* `makeStore`
38+
* This is where the redux store gets created. I'm not super familiar with all the details of how it works, but the `createStore` method is necessary, and that takes as parameters any reducers that your application has, the app's initial state, and any middleware that you might want to configure.
39+
* Also noted, is that on line:24, I assign `window.store` just for the simplicity of being able to access it from the console if needed. Its helpful to be able to see it's attributes and call the methods right from the console if necessary.
40+
* `index.js`
41+
* The initial state is, well, initialized and passed into `makeStore` where the store is created. From here, the Root component is passed newly created the `store`, into the `render` function, where it is then rendered to the DOM in the div with the ID of `root`.
42+
* `Root.jsx`
43+
* This is where we configure react router. You notice that before the component is initialized (in its purely functional form), the store is passed through the props, and into the `Provider` making that available to the app as a whole.
44+
45+
If you have questions, feel free to reach out to me.
46+
If you have suggestions to how this could be more accurate or better improved, feel free to make a pull request or submit an issue.
47+
48+
#### Credits
49+
This project was scaffolded with `create-react-app`.
50+
51+
52+
*Once again, there are many aspects of this repo that aren't the best practice, but its' purpose is to simply display how redux works.*

src/Root.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import AppContainer from './AppContainer';
55

66
export default function Root(props) {
77
const { store } = props;
8-
console.log({ store });
98
return (
109
<Provider store={store}>
1110
<Router history={hashHistory}>

src/redux/reducer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const SAVE = 'github-viewer/profiles/SAVE';
99
export default function reducer(state = defaultState, { type, ...payload } = {}) {
1010
switch (type) {
1111
case SAVE:
12-
console.log('STATE', state);
1312
const profiles = state.profiles;
1413
profiles.push(payload.profile);
1514
return { ...state, isFetching: false, profiles };
@@ -19,8 +18,9 @@ export default function reducer(state = defaultState, { type, ...payload } = {}
1918
}
2019

2120
export function save(profile) {
22-
console.log('Saving...', profile);
23-
return { type: SAVE, profile };
21+
return (dispatch) => {
22+
dispatch({ type: SAVE, profile });
23+
}
2424
}
2525

2626
export const getProfiles = state => state.profiles;

0 commit comments

Comments
 (0)