Git Product home page Git Product logo

redux-swapi's Introduction

Redux Swapi


Topics

  • Middleware and applyMiddleware
  • redux-thunk
  • redux-logger

Description

  • Notice the file structure. There are certainly many ways by which one could layout an application.
  • This pattern is commonly used:
src
  actions
    - index.js
  components -
    - index.js
    - Character.js
    - CharacterList.js
  reducers
    - index.js
  styles
    - App.css
  views
    - index.js
    - CharacterListView.js
  - App.js
  - index.js

You'll notice that we are using container and presentational components to separate concerns in our app structure. The container component, found in the views directory, is a connected component that will receive data from the redux store. Then it sends that data to it's children components, which are presentational components found in the components directory.

Initialize Project

  • The purpose of this mini-project is to expose you to the world of asyncronous Redux.

  • Notice the package.json. We have included a few new packages here for you.

    • redux-thunk redux-logger and axios.
  • axios isn't something new to you, you've used it before to make Asynchronous JavaScript and XML requests.

  • redux-thunk and redux-logger are the technologies you'll be introduced to here.

middleware

  • Simply put, middleware is software that sits between our action creators and the reducer stack. Every action will first go through all middleware sequentially before it is sent to the reducers.
  • Consider something we can use to augment the powers of Redux. redux has a package you can use called applyMiddleware that will allow you to install utilities to help you achieve specific tasks.
  • We can pull in the applyMiddleware function directly from redux.
import { applyMiddleware, createStore } from 'redux';
  • We're going to use applyMiddleware to inject middlware into the store, specifically we'll add the redux-thunk and redux-logger middleware packages.

redux-thunk

  • redux-thunk was built by Dan Abramov, co-author of Redux, to handle Asynchronous requests in Redux.
  • What is it? - redux-thunk is a middleware that we can plug into our createStore() method when setting up our Redux application.
  • Why do we need it? Well, Dan himself argues that if you have to ask that question you probably don't need it. However, the average single-page-application deals with HTTP requests and often times, we don't have the data back from the server we need in time for use in a synchronous flow. redux-thunk allows us to turn our action creators into async functions by granting them the ability to return 'functions' instead of plain objects.
  • How do we use it? It's pretty simple really.
import { applyMiddleware, createStore } from 'redux';
import thunk from 'redux-thunk';
import { someCoolReducer } from './reducers';

const store = createStore(someCoolReducer, applyMiddleware(thunk));

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);
  • instead of passing our initial state directly to our createStore() function, we can pass it applyMiddleware as a second argument.
  • Then, anything we add to applyMiddleware we'll have access to once we pass it to our store and set it as a property on the <Provider /> component.
  • Set up is really that simple. The biggest changes lie in the re-design of our action-creators.
  • Instead of returning a simple object every time, any http request sent out can be done within an action creator and that function can now return another function.
  • This would look a lot like this.
function myCleverAction = () => dispatch => {
  const request = axios.get('https://someRadUrlAPI.com/api/coolness);
  request.then(({data}) => {
    dispatch({type: GET_COOL_THING, payload: data.things});
  })
  .catch(err => {
    dispatch({type: ERROR_GETTING_THINGS, error: err});
  });
};
  • This is an http request, and at this point in time, it is a promise.
  • Our promise resolves here with data in this .then block
  • Now we just call the dispatch method which has ben exposed to us through our thunk middleware.
  • This looks like a lot of boiler plate, but it's actually a controlled, and eloquent solution to big problems often caused by cross-site-scripting and making http requests.

redux-logger

  • redux-logger is a logging middleware that allows us to simply put some of the powers of the redux dev tools directly into our browser.
  • If you don't want to mess with configuration of Chrome packages etc. this is a really good way to tap into your store.subscribe() function.
  • Set up is simple, pass it into the applyMiddleware() method along with your redux-thunk middleware and you're good to go!
import logger from 'redux-logger';

applyMiddleware(thunk, logger);
  • As soon as your app starts dispatching actions, you'll see a very delightful log of these actions in the console :) Feel free to disable this at anytime if logs get to busy or if you just simply prefer to use the dev tools.

Project

  • Your project here is to build a react-redux application that will request some data from a 3rd party api.
  • go ahead and run an npm install or yarn to get what you need installed here.
  • Start in src/index.js. We'll need to pull in the appropriate packages.
  • Next after you're all wired up in your index lets move over to work on your reducers.
  • Next move into our actions/index.js file to build out the action that will be sending off the axios request to the SWAPI api, the URL is https://swapi.co/api/people.
    • Hint - console.log will be your best friend here. As soon as you get the right data back, you'll want to make sure your reducer is ready to receive it... so there may be some back and forth here.
  • Finally wire everything up inside of your component tree. You'll do most of the work in CharacterListView.js. Be sure to call your action from within componentDidMount to trigger the request.

redux-swapi's People

Contributors

bdurb avatar dustinmyers avatar jasonsbarr avatar justsml avatar luishrd avatar ryan-hamblin avatar seanchen1991 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

redux-swapi's Issues

Typo In Readme

The url for swapi , as written in the readme, returns an error.

It works correctly when an end slash is added, https://swapi.co/api/people/ .

Incorrect action type labels in comment/directions

Also in starWarsReducer, forgot this one before:

The comment in charsReducer labels the actions differently than what were specified actions/index.js and imported in the first line here.

import { FETCHING, FETCHED, ERRORS } from '../actions';

const initialState = {
// define a few properties here.
// Array chars, String fetching, String fetched, null error.
chars: [],
fetching: false,
fetched: false,
error: null,
};
export const charsReducer = (state = initialState, action) => {
// Fill me in with the important reducers
// action types should be FETCHING_CHARS, CHARS_RECEIVED, ERROR_FETCHING_CHARS
// your switch statement should handle all of these cases.
switch (action.type) {
case FETCHING:
return { ...state, fetching: true };
case FETCHED:
return {
...state,
fetched: true,
fetching: false,
chars: action.payload.results
}
case ERRORS:
return { ...state, fetching: false, error: action.payload };
default:
return state;
}
};

Data type description text errors

The direction comments indicate to use strings for fetching and fetched, but they should be booleans:

const initialState = {
// define a few properties here.
// Array chars, String fetching, String fetched, null error.
chars: [],
fetching: false,
fetched: false,
error: null,
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.