All Projects → dbbk → react-data-fetching-components

dbbk / react-data-fetching-components

Licence: other
♻️ Asynchronously load data for your React components with SSR

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-data-fetching-components

React Loadable
⏳ A higher order component for loading components with promises.
Stars: ✭ 16,238 (+124807.69%)
Mutual labels:  code-splitting, loading, server-side-rendering
Modern-Web-App
React PWA with SSR and Code splitting
Stars: ✭ 45 (+246.15%)
Mutual labels:  code-splitting, server-side-rendering
isomorphic-react-redux-saga-ssr
Isomorphic, React, Redux, Saga, Server Side rendering, Hot Module Reloading, Ducks, Code Splitting
Stars: ✭ 19 (+46.15%)
Mutual labels:  code-splitting, server-side-rendering
React Router Server
Server Side Rendering library for React Router v4.
Stars: ✭ 443 (+3307.69%)
Mutual labels:  code-splitting, server-side-rendering
untool
JavaScript tooling platform that focuses on universal React applications. Supports advanced features such as hot-reloading, static and dynamic server side rendering and code splitting.
Stars: ✭ 18 (+38.46%)
Mutual labels:  code-splitting, server-side-rendering
Hapi React Hot Loader Example
Simple React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (+238.46%)
Mutual labels:  code-splitting, server-side-rendering
Twreporter React
twreporter site with nodejs
Stars: ✭ 263 (+1923.08%)
Mutual labels:  code-splitting, server-side-rendering
kaonjs
Kaon.js is a react isomorphic app solution. It contains webpack build, hot reloading, code splitting and server side rendering.
Stars: ✭ 21 (+61.54%)
Mutual labels:  code-splitting, server-side-rendering
Typescript Hapi React Hot Loader Example
Simple TypeScript React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (+238.46%)
Mutual labels:  code-splitting, server-side-rendering
Loadable Components
The recommended Code Splitting library for React ✂️✨
Stars: ✭ 6,194 (+47546.15%)
Mutual labels:  code-splitting, server-side-rendering
React Imported Component
✂️📦Bundler-independent solution for SSR-friendly code-splitting
Stars: ✭ 525 (+3938.46%)
Mutual labels:  code-splitting, server-side-rendering
React Ssr Boilerplate
Boilerplate for React apps with routing, code splitting, & server side rendering
Stars: ✭ 183 (+1307.69%)
Mutual labels:  code-splitting, server-side-rendering
V2 Universal Js Hmr Ssr React Redux
⚡ (V2) Universal JS - Server Side Rendering, Code Splitting and Hot Module Reloading ⚡
Stars: ✭ 147 (+1030.77%)
Mutual labels:  code-splitting, server-side-rendering
guide-to-async-components
📖 Guide To JavaScript Async Components
Stars: ✭ 79 (+507.69%)
Mutual labels:  code-splitting, loading
react-loadable-ssr-addon
Server Side Render add-on for React Loadable. Load splitted chunks was never that easy.
Stars: ✭ 68 (+423.08%)
Mutual labels:  server-side-rendering
react-loading-placeholder
Loading placeholer, inspired by Facebook
Stars: ✭ 17 (+30.77%)
Mutual labels:  loading
shitload
The appropriate bullgit loading animation
Stars: ✭ 15 (+15.38%)
Mutual labels:  loading
XLDotLoading
iOS 新浪微博红包加载动画
Stars: ✭ 30 (+130.77%)
Mutual labels:  loading
dlib
Dynamic loading library for C/C++
Stars: ✭ 19 (+46.15%)
Mutual labels:  loading
universal-react-starter-kit
Universal React Starter Kit is an universal web application framework using koa, react, redux and webpack.
Stars: ✭ 13 (+0%)
Mutual labels:  code-splitting

This package allows you to add a getInitialData method to your React components, no matter how deeply they are nested. It also works seamlessly with Server-Side Rendering and rehydration.

Installation

yarn add react-data-fetching-components

Usage

Wrap any components you want to fetch data for with the withInitialData HOC, and add a static method getInitialData to the class. This method should return a Promise, or be set to an async function. Here's an example component;

import React, { Component } from 'react';
import { withInitialData } from 'react-data-fetching-components';

class Page extends Component {
  static async getInitialData(props) {
    const res = await fetch('...');
    const json = await res.json();

    return json;
  }

  render() {
    return <div>{this.props.data}</div>;
  }
}

export default withInitialData(Page);

Loading and Error States

You can also add loading and error methods on your component, alongside render, that will display when getInitialData is either pending or rejects. For example;

import React, { Component } from 'react';
import { withInitialData } from 'react-data-fetching-components';

class Page extends Component {
  static async getInitialData(props) {
    const res = await fetch('...');
    const json = await res.json();

    return json;
  }

  loading() {
    return <div>Loading data...</div>;
  }

  error() {
    return <div>Error loading data!</div>;
  }

  render() {
    return <div>{this.props.data}</div>;
  }
}

export default withInitialData(Page);

Server-Side Rendering

Setup

To get SSR working, you just need to edit two files. First, edit your server.js file by awaiting on getAllInitialData before rendering your top-level app component. Then, when you're ready to renderToString, wrap the app component with <ComponentDataStore data={data}> to pass in the data that has been pre-fetched. It should look something like this pseudocode;

import {
  ComponentDataStore,
  getAllInitialData
} from 'react-data-fetching-components';

server.get('/*', async (req, res) => {
  const app = <App />;

  const data = await getAllInitialData(app);

  const markup = renderToString(
    <ComponentDataStore data={data}>{app}</ComponentDataStore>
  );

  res.status(200).send(`<html>
    <body>
      ${markup}
    </body>
  </html>`);
});

Then, in your HTML response, you should add the following script tag before your JS assets;

<script>window._COMPONENT_DATA_ = ${JSON.stringify(data)};</script>

Now, edit your client.js file by adding the <ComponentDataStore> component and passing in the data from window._COMPONENT_DATA_ like so;

import { ComponentDataStore } from 'react-data-fetching-components';

const data = window._COMPONENT_DATA_;

hydrate(
  <ComponentDataStore data={data}>
    <App />
  </ComponentDataStore>,
  document.getElementById('root')
);

This allows the client-side app to seamlessly rehydrate the data loaded during your server request.

Advanced: Parallelising Network Requests

By default, every time you server-render a component that has getInitialData, rendering is paused until that method's Promise is completed. This means that this.props.data is always available by the time the render method is fired. However, if you have a lot of nested components making network requests, your page load times will start to get noticeably slower, as every request is happening sequentially. Fortunately, there's a solution! Here's an example component;

class ParallelComponent extends Component {
  static getInitialDataInParallel = true;

  static async getInitialData(props) {
    const res = await fetch('...');
    const json = await res.json();

    return json;
  }

  render() {
    return <div>{this.props.data ? this.props.data : null}</div>;
  }
}

By setting the getInitialDataInParallel property on your component class to true, during the server-render pass the getInitialData Promise will be pushed to an array and later fired in parallel with Promise.all. The main caveat for your component is that now, in your render method, you must include a conditional check for this.props.data, as it will not be defined during the initial render pass.

This is ideal for cases where you have a nested component that makes a data request, but does not depend upon the result of a data request higher up the component tree, e.g. it can rely purely on routing params.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].