GithubHelp home page GithubHelp logo

lastdreamer / react-router-prefetch Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 1.0 1020 KB

Load data for components before router transition

Home Page: https://lastdreamer.github.io/react-router-prefetch/index.html

License: MIT License

JavaScript 100.00%

react-router-prefetch's Introduction

react-router-prefetch (Deprecated)

Build Status dependencies Status Coverage Status Quality Gate

Load data for components before router transition.

Installation

npm i --save react-router-prefetch

Usage

For prefetching enable you need only 2 steps:

  1. Add static method prefetch to your component that returns a Promise
// component.jsx
import React, ( Component ) from 'react';

let asyncData = {};

class MyComponent extends Component {
  static prefetch(props) {
    return new Promise((resolve) => {
      fetch(`/data/${props.id}`)
        .then(data => {
          asyncData = data;
          resolve();
        });
    });
  }
  
  render() {
    const { foo, bar } = asyncData;
    ...
  }
}
  1. Wrap Router childs in a component Prefetch from this package
export default MyComponent;

// routes.jsx
import { BrowserRouter as Router } from 'react-router';
import Prefetch from 'react-router-prefetch';
import Routes from '...';

const App = (history) => (
  <Router history={history}>
    <Prefetch
      onError={message => window.alert(message)}
    >
      <Routes />
    </Prefetch>
  </Router>
)

export default App;

Handle prefetch in redux saga

  1. Same as previous example, but prefetch method should be created by createSagaPrefetch
// component.jsx
import React, ( Component ) from 'react';
import { createSagaPrefetch } from 'react-router-prefetch';
import { connect } from 'react-redux';

class MyComponent extends Component {
  static prefetch = props => createSagaPrefetch({
    props,
    'ACTION_TYPE',
    // payload
    {
      key: props.id,
    },
  })
  
  render() {
    ...
  }
}

export default connect()(MyComponent);
  1. Add handler into your saga
// sagas.js
import { call, put } from 'redux-saga/effects';

import api from './api';
import types from './types';

function* fetchData({ payload, resolve, reject }) {
  try {
    const data = yield call(api, payload);

    yield put({
      type: types.DATA_SUCCESS,
      payload: data,
    });

    resolve();
  } catch (e) {
    yield put({
      type: types.DATA_FAILURE,
      payload: e,
    });

    reject(e);
  }
}

createSagaPrefetch Properties

# Name Description
1 props Properties of Component - it used for get and call dispatch from it
2 type action.type that would be passed to dispatch
3 payload action.payload that would be passed to dispatch

Prefetch Properties

Name Type Required Default Description
initialHide bool true Hide children on initial transition
errorMessage string 'Error while page loading' Message for Promise rejecting callback
prefetchMethod string 'prefetch' Name of method that Prefetch will recursively search in children
preloader node 'Loading...' String or Component displays while fetching
onError func + Promise reject callback
onFetchStart func Callback before prefetch
onFetchEnd func Callback after prefetch or reject

Credits

Feel free to open issues

react-router-prefetch's People

Contributors

lastdreamer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gbiryukov

react-router-prefetch's Issues

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.