请选择 进入手机版 | 继续访问电脑版

技术控

    今日:1| 主题:61668
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Build A Media Library With React, Redux, and Redux-saga – Part 2

[复制链接]
谢谢你忽略 发表于 2016-12-1 05:03:53
383 6
In thefirst part of this tutorial, we had a running app. We covered basic React setup, project workflow; defined basic components and configured our application's routes.
  In part 2 of this tutorial, which is unarguably the most interesting part of building React/Redux application, we will setup application state management with redux, connect our React components to the store, and then deploy to Heroku. We will walk through this part in eight steps:
  
       
  • Define Endpoints of interest.   
  • Create a container component.   
  • Define action creators.   
  • Setup state management system.   
  • Define async task handlers.   
  • Create presentational components.   
  • Connect our React component to Redux store.   
  • Deploy to Heroku.  
  Step 1 of 8: Define Endpoints of interest

  Our interest is the media search endpoints of Flickr API and Shutterstock API.
  Api/api.js

  1. const FLICKR_API_KEY = 'a46a979f39c49975dbdd23b378e6d3d5';
  2. const SHUTTER_CLIENT_ID = '3434a56d8702085b9226';
  3. const SHUTTER_CLIENT_SECRET = '7698001661a2b347c2017dfd50aebb2519eda578';
  4. // Basic Authentication for accessing Shutterstock API
  5. const basicAuth = () => 'Basic '.concat(window.btoa(`${SHUTTER_CLIENT_ID}:${SHUTTER_CLIENT_SECRET}`));
  6. const authParameters = {
  7.   headers: {
  8.     Authorization: basicAuth()
  9.   }
  10. };
  11. /**
  12. * Description [Access Shutterstock search endpoint for short videos]
  13. * @params { String } searchQuery
  14. * @return { Array }
  15. */
  16. export const shutterStockVideos = (searchQuery) => {
  17.   const SHUTTERSTOCK_API_ENDPOINT = `https://api.shutterstock.com/v2/videos/search?
  18.   query=${searchQuery}&page=1&per_page=10`;
  19.   return fetch(SHUTTERSTOCK_API_ENDPOINT, authParameters)
  20.   .then(response => {
  21.     return response.json();
  22.   })
  23.   .then(json => {
  24.       return json.data.map(({ id, assets, description }) => ({
  25.         id,
  26.         mediaUrl: assets.preview_mp4.url,
  27.         description
  28.       }));
  29.   });
  30. };
  31. /**
  32. * Description [Access Flickr search endpoint for photos]
  33. * @params { String } searchQuery
  34. * @return { Array }
  35. */
  36. export const flickrImages = (searchQuery) => {
  37.   const FLICKR_API_ENDPOINT = `https://api.flickr.com/services/rest/?method=flickr.photos.search&text=${searchQuery}&api_key=${FLICKR_API_KEY}&format=json&nojsoncallback=1&per_page=10`;
  38.   return fetch(FLICKR_API_ENDPOINT)
  39.     .then(response => {
  40.       return response.json()
  41.     })
  42.     .then(json => {
  43.       return json.photos.photo.map(({ farm, server, id, secret, title }) => ({
  44.         id,
  45.         title,
  46.         mediaUrl: `https://farm${farm}.staticflickr.com/${server}/${id}_${secret}.jpg`
  47.       }));
  48.     });
  49. };
复制代码
  First, head to Flickr and Shutterstock to get your credentials or use mine.
   We’re using fetch method from fetch API for our AJAX request. It returns a promise that resolves to the response of such request. We simply format the response of our call using ES6 destructuring assignment before returning to the store.
   We can use jQuery for this task but it’s such a large library with many features, so using it just for AJAX doesn’t make sense.
  Step 2 of 8: Create a container component

  In order to test our application as we walk through the steps, let's define a MediaGalleryPage component which we will update later for a real time sync with our store.
  container/MediaGalleryPage.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;
复制代码
We can now add library route and map it to MediaGalleryPage Container.
   Let's update out routes.js for this feature.
  1. import React from 'react';
  2. import { Route, IndexRoute } from 'react-router';
  3. import App from './containers/App';
  4. import HomePage from './components/HomePage';
  5. import MediaGalleryPage from './containers/MediaGalleryPage';
  6. // Map components to different routes.
  7. // The parent component wraps other components and thus serves as
  8. // the entrance to other React components.
  9. // IndexRoute maps HomePage component to the default route
  10. export default (
  11.   <Route path="/" component={App}>
  12.     <IndexRoute component={HomePage} />
  13.     <Route path="library" component={MediaGalleryPage} />
  14.   </Route>
  15. );
复制代码
Let's check it out on the browser console.
   

Build A Media Library With React, Redux, and Redux-saga – Part 2

Build A Media Library With React, Redux, and Redux-saga – Part 2-1-技术控-management,building,interest,running,through

  We are now certain that we can access our endpoints of interest to fetch images and short videos. We can render the results to the view but we want to separate our React components from our state management system. Some major advantages of this approach are maintainability, readability, predictability, and testability.
  We will be wrapping our heads around some vital concepts in a couple of steps.
  Step 3 of 8: Define action creators

   Action creatorsare functions that return plain Javascript object of action type and an optional payload. So action creators create actions that are dispatched to the store. They are just pure functions.
  Let’s first define our action types in a file and export them for ease of use in other files. They’re constants and it’s a good practice to define them in a separate file(s).
  constants/actionTypes.js

  1. // It's preferable to keep your action types together.
  2. export const SELECTED_IMAGE = 'SELECTED_IMAGE';
  3. export const FLICKR_IMAGES_SUCCESS = 'FLICKR_IMAGES_SUCCESS';
  4. export const SELECTED_VIDEO = 'SELECTED_VIDEO';
  5. export const SHUTTER_VIDEOS_SUCCESS = 'SHUTTER_VIDEOS_SUCCESS';
  6. export const SEARCH_MEDIA_REQUEST = 'SEARCH_MEDIA_REQUEST';
  7. export const SEARCH_MEDIA_SUCCESS = 'SEARCH_MEDIA_SUCCESS';
  8. export const SEARCH_MEDIA_ERROR = 'SEARCH_MEDIA_ERROR';
复制代码
Now, we can use the action types to define our action creators for different actions we need.
  actions/mediaActions.js

  1. import * as types from '../constants/actionTypes';
  2. // Returns an action type, SELECTED_IMAGE and the image selected
  3. export const selectImageAction = (image) => ({
  4.   type: types.SELECTED_IMAGE,
  5.   image
  6. });
  7. // Returns an action type, SELECTED_VIDEO and the video selected
  8. export const selectVideoAction = (video) => ({
  9.   type: types.SELECTED_VIDEO,
  10.   video
  11. });
  12. // Returns an action type, SEARCH_MEDIA_REQUEST and the search criteria
  13. export const searchMediaAction = (payload) => ({
  14.   type: types.SEARCH_MEDIA_REQUEST,
  15.   payload
  16. });
复制代码
  The optional arguments in the action creators: payload , image , and video are passed at the site of call/dispatch. Say, a user selects a video clip on our app, selectVideoAction is dispatched which returns SELECTED_VIDEO action type and the selected video as payload. Similarly, when searchMediaAction is dispatched, SEARCH_MEDIA_REQUEST action type and payload are returned.
  Step 4 of 8: Setup state management system

  We have defined the action creators we need and it's time to connect them together. We will setup our reducers and configure our store in this step.
   There are some wonderful concepts here as shown in the diagram inPart 1.
  Let's delve into some definitions and implementations.
   The Store holds the whole state tree of our application but more importantly, it does nothing to it. When an action is dispatched from a React component, it delegates the reducer but passing the current state tree alongside the action object. It only updates its state after the reducer returns a new state.
   Reducers, for short are pure functions that accept the state tree and an action object from the store and returns a new state. No state mutation. No API calls. No side effects. It simply calculates the new state and returns it to the store.
  Let’s wire up our reducers by first setting our initial state. We want to initialize images and videos as an empty array in our own case.
  reducers/initialState.js

  1. export default {
  2.   images: [],
  3.   videos: []
  4. };
复制代码
Our reducers take the current state tree and an action object and then evaluate and return the outcome.
  Let’s check it out.
  reducers/imageReducer.js

  1. import initialState from './initialState';
  2. import * as types from '../constants/actionTypes';
  3. // Handles image related actions
  4. export default function (state = initialState.images, action) {
  5.   switch (action.type) {
  6.     case types.FLICKR_IMAGES_SUCCESS:
  7.       return [...state, action.images];
  8.     case types.SELECTED_IMAGE:
  9.       return { ...state, selectedImage: action.image };
  10.     default:
  11.       return state;
  12.   }
  13. }
复制代码
reducers/videoReducer.js

  1. import initialState from './initialState';
  2. import * as types from '../constants/actionTypes';
  3. // Handles video related actions
  4. // The idea is to return an updated copy of the state depending on the action type.
  5. export default function (state = initialState.videos, action) {
  6.   switch (action.type) {
  7.     case types.SHUTTER_VIDEOS_SUCCESS:
  8.       return [...state, action.videos];
  9.     case types.SELECTED_VIDEO:
  10.       return { ...state, selectedVideo: action.video };
  11.     default:
  12.       return state;
  13.   }
  14. }
复制代码
The two reducers look alike and that’s how simple reducers can be. We use a switch statement to evaluate an action type and then return a new state.
   create-react-appcomes preinstalled with babel-plugin-transform-object-rest-spread that lets you use the spread (…) operator to copy enumerable properties from one object to another in a succinct way.
   For context, { …state, videos: action.videos } evaluates to Object.assign({}, state, action.videos).
  Since reducers don’t mutate state, you would always find yourself using spread operator, to make and update the new copy of the current state tree.
   So, When the reducer receives SELECTED_VIDEO action type, it returns a new copy of the state tree by spreading it( …state ) and updating the selectedVideo property.
  The next step is to register our reducers to a root reducer before passing to the store.
  reducers/index.js

  1. import { combineReducers } from 'redux';
  2. import images from './imageReducer';
  3. import videos from './videoReducer';
  4. // Combines all reducers to a single reducer function
  5. const rootReducer = combineReducers({
  6.   images,
  7.   videos
  8. });
  9. export default rootReducer;
复制代码
  We import combineReducers from Redux. CombineReducers is a helper function that combines our images and videos reducers into a single reducer function that we can now pass to the creatorStore function.
   You might be wondering why we’re not passing in key/value pairs to combineReducers function. Yes, you’re right. ES6 allows us to pass in just the property if the key and value are the same.
  Now, we can complete our state management system by creating the store for our app.
  store/configureStore.js

  1. import { createStore, applyMiddleware } from 'redux';
  2. import createSagaMiddleware from 'redux-saga';
  3. import rootReducer from '../reducers';
  4. import rootSaga from '../sagas'; // TODO: Next step
  5. //  Returns the store instance
  6. // It can  also take initialState argument when provided
  7. const configureStore = () => {
  8.   const sagaMiddleware = createSagaMiddleware();
  9.   return {
  10.     ...createStore(rootReducer,
  11.       applyMiddleware(sagaMiddleware)),
  12.     runSaga: sagaMiddleware.run(rootSaga)
  13.   };
  14. };
  15. export default configureStore;
复制代码

       
  • Initialize your SagaMiddleWare. We’d discuss about sagas in the next step.   
  • Pass rootReducer and sagaMiddleware to the createStore function to create our redux store.   
  • Finally, we run our sagas. You can either spread them or wire them up to a rootSaga.  
  What are sagas and why use a middleware?
  Step 5 of 8: Define async task handlers

  Handling AJAX is a very important aspect of building web applications and React/Redux application is not an exception. We will look at the libraries to leverage for such task and how they neatly fit into the whole idea of having a state management system.
  You would remember reducers are pure functions and don’t handle side effects or async tasks; this is where redux-saga comes in handy.
   redux-sagais a library that aims to make side effects (i.e. asynchronous things like data fetching and impure things like accessing the browser cache) in React/Redux applications easier and better — documentation .
  So, to use redux-saga, we also need to define our own sagas that will handle the necessary async tasks.
  What the heck are sagas?
  Sagas are simply generator functions that abstract the complexities of an asynchronous workflow. It’s a terse way of handling async processes. It’s easy to write, test and reason. Still confused, you might want to revisit the first part of this tutorial if you missed it.
  Let’s first define our watcher saga and work down smoothly.
  sagas/watcher.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;0
复制代码
We want a mechanism that ensures any action dispatched to the store which requires making API call is intercepted by the middleware and result of request yielded to the reducer.
   To achieve this, Redux-saga API exposes some methods. We need only four of those for our app: call , put , fork and takeLatest .
  
       
  • takeLatest is a high-level method that merges take and fork effect creators together. It basically takes an action type and runs the function passed to it in a non-blocking manner with the result of the action creator. As the name suggests, takeLatest returns the result of the last call.   
  • watchSearchMedia watches for SEARCH_MEDIA_REQUEST action type and call searchMediaSaga function(saga) with the action’s payload from the action creator.  
   Now, we can define searchMediaSaga ; it serves as a middleman to call our API. Getting interesting right?
  sagas/mediaSaga.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;1
复制代码
  searchMediaSagais not entirely different from normal functions except the way it handles async tasks.
   callis a redux-saga effect that instructs the middleware to run a specified function with an optional payload.
  Let’s do a quick review of some happenings up there.
  
       
  • searchMediaSaga is called by the watcher saga defined earlier on each time SEARCH_MEDIA_REQUEST is dispatched to store.   
  • It serves as an intermediary between the API and the reducers.  
   

Build A Media Library With React, Redux, and Redux-saga – Part 2

Build A Media Library With React, Redux, and Redux-saga – Part 2-2-技术控-management,building,interest,running,through

  
       
  • So, when the saga( searchMediaSaga ) is called, it makes a call to the API with the payload. Then, the result of the promise(resolved or rejected) and an action object is yielded to the reducer using put effect creator. put instructs Redux-saga middleware on what action to dispatch.   
  • Notice, we’re yielding an array of effects. This is because we want them to run concurrently. The default behaviour would be to pause after each yield statement which is not the behaviour we intend.   
  • Finally, if any of the operations fail, we yield a failure action object to the reducer.  
   Let’s wrap up this section by registering our saga to the rootSaga .
  sagas/index.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;2
复制代码
  forkis an effect creator that provisions the middleware to run a non-blocking call on watchSearchMedia saga.
  Here, we can bundle our watcher sagas as an array and yield them at once if we have more than one.
   Hope by now, you are getting comfortable with the workflow. So far, we’re able to export startForman as our rootSaga .
  How does our React component know what is happening in the state management system?
  Step 6 of 8: Connect our React component to redux store

  I’m super excited that you’re still engaging and we’re about testing our app.
   First, let’s update our index.jsapp’s entry file.
  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;3
复制代码
Let’s review what’s going on.
  
       
  • Initialize our store.   
  • Provider component from react-redux makes the store available to the components hierarchy. So, we have to pass the store as props to it. That way, the components below the hierarchy can access the store’s state with connect method call.  
   Now, let's update our MediaGalleryPage component to access the store.
  container/MediaGalleryPage.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;4
复制代码
  MediaGalleryPagecomponent serves two major purposes:
  a) Sync React Components with the Redux store.
   b) Pass props to our presentational components: PhotoPage and VideoPage . We will create this later in the tutorial to render our content to the page.
  Let’s summarize what's going on.
   React-router exposes two important methods(components) we will use to bind our redux store to our component - connect and Provider.
   connecttakes three optional functions. If any is not defined, it takes the default implementation. It’s a function that returns a function that takes our React component- MediaGalleryPage as an argument.
   mapStateToPropsallows us keep in sync with store's updates and to format our state values before passing as props to the React component. We use ES6 destructuring assignment to extract images and videos from the store’s state.
  Now, everthing is good and we can test our application.
  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;5
复制代码
  

Build A Media Library With React, Redux, and Redux-saga – Part 2

Build A Media Library With React, Redux, and Redux-saga – Part 2-3-技术控-management,building,interest,running,through

  You can grab a cup of coffee and be proud of yourself.
  Wouldn't it be nice if we can render our result on the webpage as supposed to viewing them on the browser console?
  Step 7 of 8: Create presentational components

  What are presentational components?
   They are basically components that are concerned with presentation - how things look. Early in this tutorial, we created a container component - MediaGalleryPage which will be concerned with passing data to these presentational components. This is a design decision which has helped large applications scale efficiently. However, it's at your discretion to choose what works for your application.
  Our task is now easier. Let's create the two React components to handle images and the videos.
  components/PhotoPage.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;6
复制代码
components/VideoPage.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;7
复制代码
The two React components are basically the same except that one handles images and the other is responsible for rendering our videos.
   Let's now update our MediaGalleryPage to pass props to this components.
  container/MediaGalleryPage.js

  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;8
复制代码
  This is pretty much how our final MediaGalleryPage component looks like. We can now render our images and videos to the webpage.
   Let's recap on the latest update on MediaGalleryPage component.
   rendermethod of our component is very interesting. Here we’re passing down props from the store and the component’s custom functions( handleSearch , handleSelectVideo , handleSelectImage ) to the presentational components — PhotoPage and VideoPage .
   This way, our presentational components are not aware of the store. They simply take their behaviour from MediaGalleryPage component and render accordingly.
   Each of the custom functions dispatches an action to the store when called. We use ref to save a callback that would be executed each time a user wants to search the library.
   componentDidMountLifecycle method is meant to allow dynamic behaviour, side effects, AJAX, etc. We want to render search result for rain once a user navigates to library route.
   One more thing. We bound all the custom functions in the component’s constructor function with .bind() method. It’s simply Javascript. bind() allows you to create a function out of regular functions. The first argument to it is the context ( this, in our own case ) to which you want to bind your function. Any other argument will be passed to such function that’s bounded.
  We’re done building our app. Surprised?
  Let’s test it out…
  1. import React, { Component } from 'react';
  2. import { flickrImages, shutterStockVideos } from '../Api/api';
  3. // MediaGalleryPage Component
  4. class MediaGalleryPage extends Component {
  5. // We want to get images and videos from the API right after our component renders.
  6. componentDidMount() {
  7.     flickrImages('rain').then(images => console.log(images, 'Images'));
  8.     shutterStockVideos('rain').then(videos => console.log(videos,'Videos'));
  9.   }
  10.   render() {
  11.   // TODO: Render videos and images here
  12.   return (<div></div>)
  13.   }
  14. }
  15. export default MediaGalleryPage;5
复制代码
  

Build A Media Library With React, Redux, and Redux-saga – Part 2

Build A Media Library With React, Redux, and Redux-saga – Part 2-4-技术控-management,building,interest,running,through

  Step 8 of 8: Deploy to Heroku

  Now that our app works locally, let’s deploy to a remote server for our friends to see and give us feedback. Heroku’s free plan will suffice.
   We want to use create-react-app’s build script to bundle our app for production.
  1. import React from 'react';
  2. import { Route, IndexRoute } from 'react-router';
  3. import App from './containers/App';
  4. import HomePage from './components/HomePage';
  5. import MediaGalleryPage from './containers/MediaGalleryPage';
  6. // Map components to different routes.
  7. // The parent component wraps other components and thus serves as
  8. // the entrance to other React components.
  9. // IndexRoute maps HomePage component to the default route
  10. export default (
  11.   <Route path="/" component={App}>
  12.     <IndexRoute component={HomePage} />
  13.     <Route path="library" component={MediaGalleryPage} />
  14.   </Route>
  15. );0
复制代码
  A build/ folder is now in our project directory. That’s the minified static files we will deploy.
   Next step is to add another script command to our package.json to help us serve our build files with a static file server. We will use pushstate-server (a static file server) to serve our files.
  1. import React from 'react';
  2. import { Route, IndexRoute } from 'react-router';
  3. import App from './containers/App';
  4. import HomePage from './components/HomePage';
  5. import MediaGalleryPage from './containers/MediaGalleryPage';
  6. // Map components to different routes.
  7. // The parent component wraps other components and thus serves as
  8. // the entrance to other React components.
  9. // IndexRoute maps HomePage component to the default route
  10. export default (
  11.   <Route path="/" component={App}>
  12.     <IndexRoute component={HomePage} />
  13.     <Route path="library" component={MediaGalleryPage} />
  14.   </Route>
  15. );1
复制代码
  Let's create Procfile in our project's root directory and add: web: npm run deploy
   Procfileinstructs Heroku on how to run your application.
   Now, let’s create our app on Heroku. You need to register first on their platform. Then, download and install Heroku toolbelt if it’s not installed on your system. This will allow us to deploy our app from our terminal.
  1. import React from 'react';
  2. import { Route, IndexRoute } from 'react-router';
  3. import App from './containers/App';
  4. import HomePage from './components/HomePage';
  5. import MediaGalleryPage from './containers/MediaGalleryPage';
  6. // Map components to different routes.
  7. // The parent component wraps other components and thus serves as
  8. // the entrance to other React components.
  9. // IndexRoute maps HomePage component to the default route
  10. export default (
  11.   <Route path="/" component={App}>
  12.     <IndexRoute component={HomePage} />
  13.     <Route path="library" component={MediaGalleryPage} />
  14.   </Route>
  15. );2
复制代码
We did it. Congrats. You’ve built and deployed a React/Redux application elegantly. It can be that simple.
  Now some recommended tasks.
  
       
  • Add tests for your application.   
  • Add error handling functionality.   
  • Add a loading spinner for API calls for good user experience.   
  • Add sharing functionality to allow users share on their social media walls   
  • Allow users to like an image.  
  Conclusion

  It’s apparent that some things were left out, some intentionally. However, you can improve the app for your learning. The key takeaway is to separate your application state from your React components. Use redux-saga to handle any AJAX requests and don’t put AJAX in your React components. Good luck!!!
雨珍 发表于 2016-12-2 20:52:27
我对楼主的敬仰如滔滔江水,绵延不绝!
回复 支持 反对

使用道具 举报

段姿羽 发表于 2016-12-2 23:08:46
LZ是天才,坚定完毕
回复 支持 反对

使用道具 举报

离娟闯 发表于 2016-12-2 23:29:12
不错 支持下
回复 支持 反对

使用道具 举报

香女 发表于 2016-12-5 03:16:19
意料之中是戏, 意外之中是计
回复 支持 反对

使用道具 举报

她昰戲孒 发表于 2016-12-10 21:21:58
好东西,学习学习!
回复 支持 反对

使用道具 举报

黎强 发表于 2016-12-25 19:22:38
我只是路过,不发表意见
回复 支持 反对

使用道具 举报

我要投稿

推荐阅读


回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表