0.6.3 • Published 6 years ago

react-router-prefetch v0.6.3

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

react-router-prefetch

Build Status dependencies Status Coverage Status Quality Gate

Load data for components before router transition.

Live Demo Here

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';

class MyComponent extends Component {
  static prefetch = props => createSagaPrefetch({
    props,
    'ACTION_TYPE',
    // payload
    {
      key: props.id,
    },
  })
  
  render() {
    ...
  }
}
  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

#NameDescription
1propsProperties of Component - it used for get and call dispatch from it
2typeaction.type that would be passed to dispatch
3payloadaction.payload that would be passed to dispatch

Prefetch Properties

NameTypeRequiredDefaultDescription
initialHidebooltrueHide children on initial transition
errorMessagestring'Error while page loading'Message for Promise rejecting callback
prefetchMethodstring'prefetch'Name of method that Prefetch will recursively search in children
preloadernode'Loading...'String or Component displays while fetching
onErrorfunc+Promise reject callback
onFetchStartfuncCallback before prefetch
onFetchEndfuncCallback after prefetch or reject
0.6.3

6 years ago

0.6.2

6 years ago

0.6.0

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago