react-preloader-ajax

    1.0.3 • Public • Published

    react-preloader-ajax

    npm install --save react-preloader-ajax
    

    or

    yarn add react-preloader-ajax
    

    Usage

    Preparation

    Set reducers.

      import ajaxReducers from 'react-preloader-ajax/reducers';
      //ajaxReducer is combined reducer with name: 'locals'
     
      export default mergeReducers({
        ajaxReducers
        //... other reducers
      });
     
      /*reducers result
      {
        'locals': ...,
        ...
      }*/

    In root app file set global preloader (react component)

      import * as ajaxPlugins from 'react-preloader-ajax';
     
      import Preloader from './components/preloader';
      import HeadUpdate from './components/head-update';
     
      ajaxPlugins.setGlobalPreloader(Preloader);
     
      //Preloader component documentation see below.
     
      //optional, you can assign additional updatable components which updates when new ajax request are made
     
      ajaxPlugins.addUpdateElement(HeadUpdate);

    In server side pass keystone to middleware and use it with express

      var keystone = require('keystone');
     
      var ajaxMiddleware = require('react-preloader-ajax/middleware');
     
      ajaxMiddleware.setKeystone(keystone);
      
      //...
     
      keystone.pre('routes', middleware.expressJsonCompress);
      //MUST BE AFTER express-json-compress middleware
      keystone.pre('routes', middleware.ajaxMiddleware);

    Pass data in server (Keystone.JS routes)

      //Route file
      exports = module.exports = function(req, res){
          
            //Pass data to locals file (merge)
            res.locals = Object.assign(res.locals, {
              data: 'some data',
                data_two: 'some data two'
            });
            
            return res.done(); //complete request
            
        }

    Simple usage in code

      import ajax from 'react-preloader-ajax';
     
      //ajax(Component, static_props); //typeof React component
     
      //Decorate component with ajax wrapper
      ajax(HomePage, {single: false});
     
      //Simple use in any props changeable component. E.g. Route
     
      <Route
        path="/"
        component={ajax(HomePage, {single: false})}
      />

    Get data requested data in component

      
        //Child component render function
        
      render(){
          //all data is flattened in props
            //this.props.data - 'some data'
            //this.props.data_two - 'some data two'
          return <div></div>
        }

    Ajax accepted props from parent component

    match - object with keys: ['pathname', 'direct']; If pathname is passed to ajax function returned component, it uses as request pathname. Direct parameter is for repeated requests management. See below

    Ajax static props

    single - Uses for request repeat at same pathname. If true, request is made single time per unique pathname (if request is not direct) pathname - Uses for override browser pathname (in match object)

    Props passed to preloader

    component - Child React component data - all data and props passed to ajax function progress - ajax request progress loading - boolean flag for ajax status loaded - boolean flag for ajax status

    Props passed to additional updatable component

    path - Pathname where ajax request was made.

    Author

    Edvinas pranka

    https://www.ceonnel.lt

    License

    The MIT License (MIT)

    Copyright (c) 2017 Edvinas Pranka

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

    Install

    npm i react-preloader-ajax

    DownloadsWeekly Downloads

    2

    Version

    1.0.3

    License

    MIT

    Last publish

    Collaborators

    • epranka