concept view in category isomorphic web applications

appears as: view, views, views, The view, view
Isomorphic Web Applications: Universal Development with React

This is an excerpt from Manning's book Isomorphic Web Applications: Universal Development with React.

Figure 1.11. The view (React) uses Redux to update the app state when the user takes an action. Redux then lets the view know when it should update based on the new app state.

React isn’t a framework like Angular or Ember. It only provides the code you use to write your view components. It can fit easily into a Model-View-Controller (MVC) style architecture as the view. But there’s a recommended way to build complex React apps, which is covered throughout the book.

  • Express— Enables simple server-side routing for rendering the view.
  • Listing 2.9. Action creators for recipes and featured data—src/action-creators.es6
    export const GET_RECIPES = 'GET_RECIPES'; #1 export const GET_FEATURED_RECIPE = 'GET_FEATURED_RECIPE'; #1 export function fetchRecipes() { #2 return dispatch => { return fetch('http://localhost:3000/recipes', { #3 method: 'GET' }).then((response) => { return response.json().then((data) => { #4 return dispatch({ #5 type: GET_RECIPES, #6 data: data.recipes #7 }); }); }) } } export function fetchFeaturedRecipe() { #8 return dispatch => { return fetch('http://localhost:3000/featured', { #3 method: 'GET' }).then((response) => { return response.json().then((data) => { #4 return dispatch({ #9 type: GET_FEATURED_RECIPE, #6 data: data.recipe #10 }); }); }) } } export function getHomePageData() { #11 return (dispatch, getState) => { return Promise.all([ dispatch(fetchFeaturedRecipe()), dispatch(fetchRecipes()) ]) } } #1 Best practice is to create constants for all your actions so action creators (functions listed here) and reducers can use them. Then you won’t have discrepancies between strings. #2 fetchRecipes, the first action creator, handles logic for making a request to the server for recipes data. #3 Implements fetch API for making GET request to appropriate endpoint. #4 On a successful response, get JSON from the response—using a promise to get JSON response is standard with the fetch API. #5 Dispatch action. #6 Type is the only required property of every action, set it using string constants declared at the top of the module. #7 Attach JSON data to action payload on a property called data. #8 The second action creator handles the logic for requesting the featured recipe from the server. #9 Dispatch action. #10 Attach JSON data to action payload on a property called data. #11 This action creator composes the other two action creators—making it easier for view and server to request related data. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":71},{\"line\":0,\"ch\":73}],[{\"line\":1,\"ch\":71},{\"line\":1,\"ch\":73}],[{\"line\":26,\"ch\":71},{\"line\":26,\"ch\":73}],[{\"line\":33,\"ch\":71},{\"line\":33,\"ch\":73}],[{\"line\":42,\"ch\":0},{\"line\":42,\"ch\":2}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":2}],[{\"line\":52,\"ch\":0},{\"line\":52,\"ch\":2}],[{\"line\":0,\"ch\":71},{\"line\":0,\"ch\":73}],[{\"line\":1,\"ch\":71},{\"line\":1,\"ch\":73}],[{\"line\":26,\"ch\":71},{\"line\":26,\"ch\":73}],[{\"line\":33,\"ch\":71},{\"line\":33,\"ch\":73}],[{\"line\":42,\"ch\":0},{\"line\":42,\"ch\":2}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":2}],[{\"line\":52,\"ch\":0},{\"line\":52,\"ch\":2}],[{\"line\":3,\"ch\":71},{\"line\":3,\"ch\":73}],[{\"line\":43,\"ch\":0},{\"line\":43,\"ch\":2}],[{\"line\":5,\"ch\":71},{\"line\":5,\"ch\":73}],[{\"line\":20,\"ch\":71},{\"line\":20,\"ch\":73}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":8,\"ch\":71},{\"line\":8,\"ch\":73}],[{\"line\":23,\"ch\":71},{\"line\":23,\"ch\":73}],[{\"line\":45,\"ch\":0},{\"line\":45,\"ch\":2}],[{\"line\":9,\"ch\":71},{\"line\":9,\"ch\":73}],[{\"line\":46,\"ch\":0},{\"line\":46,\"ch\":2}],[{\"line\":10,\"ch\":71},{\"line\":10,\"ch\":73}],[{\"line\":25,\"ch\":71},{\"line\":25,\"ch\":73}],[{\"line\":47,\"ch\":0},{\"line\":47,\"ch\":2}],[{\"line\":11,\"ch\":71},{\"line\":11,\"ch\":73}],[{\"line\":48,\"ch\":0},{\"line\":48,\"ch\":2}],[{\"line\":18,\"ch\":71},{\"line\":18,\"ch\":73}],[{\"line\":49,\"ch\":0},{\"line\":49,\"ch\":2}],[{\"line\":5,\"ch\":71},{\"line\":5,\"ch\":73}],[{\"line\":20,\"ch\":71},{\"line\":20,\"ch\":73}],[{\"line\":44,\"ch\":0},{\"line\":44,\"ch\":2}],[{\"line\":8,\"ch\":71},{\"line\":8,\"ch\":73}],[{\"line\":23,\"ch\":71},{\"line\":23,\"ch\":73}],[{\"line\":45,\"ch\":0},{\"line\":45,\"ch\":2}],[{\"line\":24,\"ch\":71},{\"line\":24,\"ch\":73}],[{\"line\":50,\"ch\":0},{\"line\":50,\"ch\":2}],[{\"line\":10,\"ch\":71},{\"line\":10,\"ch\":73}],[{\"line\":25,\"ch\":71},{\"line\":25,\"ch\":73}],[{\"line\":47,\"ch\":0},{\"line\":47,\"ch\":2}],[{\"line\":26,\"ch\":71},{\"line\":26,\"ch\":74}],[{\"line\":51,\"ch\":0},{\"line\":51,\"ch\":3}],[{\"line\":33,\"ch\":71},{\"line\":33,\"ch\":74}],[{\"line\":52,\"ch\":0},{\"line\":52,\"ch\":3}]]"} !@%STYLE%@!