All Projects → makeomatic → Redux Connect

makeomatic / Redux Connect

Licence: mit
Provides decorator for resolving async props in react-router, extremely useful for handling server-side rendering in React

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Redux Connect

isomorphic-react-redux-saga-ssr
Isomorphic, React, Redux, Saga, Server Side rendering, Hot Module Reloading, Ducks, Code Splitting
Stars: ✭ 19 (-96.55%)
Mutual labels:  isomorphic, react-redux, server-side-rendering
Typescript Hapi React Hot Loader Example
Simple TypeScript React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-92.01%)
Mutual labels:  isomorphic, server-side-rendering, react-redux
Hapi React Hot Loader Example
Simple React Hot Loading example with Hapi Server-side rendering
Stars: ✭ 44 (-92.01%)
Mutual labels:  isomorphic, server-side-rendering, react-redux
react-ssr-starter
🔥 ⚛️ A React boilerplate for a universal web app with a highly scalable, offline-first foundation and our focus on performance and best practices.
Stars: ✭ 40 (-92.74%)
Mutual labels:  isomorphic, server-side-rendering
craft-react
Client and Server-side React rendering for CraftCMS
Stars: ✭ 40 (-92.74%)
Mutual labels:  react-redux, server-side-rendering
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (-9.8%)
Mutual labels:  isomorphic, server-side-rendering
universal-react-relay-starter-kit
A starter kit for React in combination with Relay including a GraphQL server, server side rendering, code splitting, i18n, SEO.
Stars: ✭ 14 (-97.46%)
Mutual labels:  isomorphic, server-side-rendering
webpack-isomorphic-compiler
A compiler that makes your life easier if you are building isomorphic webpack powered apps, that is, single page applications with server-side rendering
Stars: ✭ 16 (-97.1%)
Mutual labels:  isomorphic, 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 (-96.19%)
Mutual labels:  isomorphic, server-side-rendering
movies
Real world isomorphic application for movies search, based on Webpack 5 / Express / React 17 + Redux-Saga / Bootstrap 4.6 + CSS Modules / i18next / SSR
Stars: ✭ 20 (-96.37%)
Mutual labels:  isomorphic, server-side-rendering
Go Starter Kit
[abandoned] Golang isomorphic react/hot reloadable/redux/css-modules/SSR starter kit
Stars: ✭ 2,855 (+418.15%)
Mutual labels:  isomorphic, server-side-rendering
docker-bare-infra
Docker based, minimal infrastructure boilerplate, with MySQL, WordPress, a React application and Nginx
Stars: ✭ 11 (-98%)
Mutual labels:  isomorphic, server-side-rendering
webpack-isomorphic
A lightweight solution for the server-side rendering of Webpack-built applications.
Stars: ✭ 21 (-96.19%)
Mutual labels:  isomorphic, server-side-rendering
react-ssr-hydration
Example of React Server Side Rendering with Styled Components and Client Side Hydration
Stars: ✭ 15 (-97.28%)
Mutual labels:  isomorphic, server-side-rendering
boldr
React based CMF / blogging engine using Redux, Postgres, Node, and more...
Stars: ✭ 78 (-85.84%)
Mutual labels:  isomorphic, server-side-rendering
React Head
⛑ SSR-ready Document Head tag management for React 16+
Stars: ✭ 262 (-52.45%)
Mutual labels:  isomorphic, server-side-rendering
Angular Ssr
Angular 4+ server-side rendering solution compatible with @angular/material, jQuery, and other libraries that touch the DOM (as well as providing a rich feature set!)
Stars: ✭ 283 (-48.64%)
Mutual labels:  isomorphic, server-side-rendering
Js Stack From Scratch
🛠️⚡ Step-by-step tutorial to build a modern JavaScript stack.
Stars: ✭ 18,814 (+3314.52%)
Mutual labels:  immutablejs, server-side-rendering
React Redux Styled Hot Universal
react boilerplate used best practices and focus on performance
Stars: ✭ 147 (-73.32%)
Mutual labels:  immutablejs, isomorphic
ves
Vue SSR(Server Side Render) Web Framework for Egg
Stars: ✭ 23 (-95.83%)
Mutual labels:  isomorphic, server-side-rendering

ReduxConnect for React Router

npm version Build Status

How do you usually request data and store it to redux state? You create actions that do async jobs to load data, create reducer to save this data to redux state, then connect data to your component or container.

Usually it's very similar routine tasks.

Also, usually we want data to be preloaded. Especially if you're building universal app, or you just want pages to be solid, don't jump when data was loaded.

This package consist of 2 parts: one part allows you to delay containers rendering until some async actions are happening. Another stores your data to redux state and connect your loaded data to your container.

Notice

This is a fork and refactor of redux-async-connect

Installation & Usage

Using npm:

$ npm install redux-connect -S

import { BrowserRouter } from "react-router-dom";
import { renderRoutes } from "react-router-config";
import {
  ReduxAsyncConnect,
  asyncConnect,
  reducer as reduxAsyncConnect
} from "redux-connect";
import React from "react";
import { hydrate } from "react-dom";
import { createStore, combineReducers } from "redux";
import { Provider } from "react-redux";

const App = props => {
  const { route, asyncConnectKeyExample } = props; // access data from asyncConnect as props
  return (
    <div>
      {asyncConnectKeyExample && asyncConnectKeyExample.name}
      {renderRoutes(route.routes)}
    </div>
  );
};

// Conenect App with asyncConnect
const ConnectedApp = asyncConnect([
  // API for ayncConnect decorator: https://github.com/makeomatic/redux-connect/blob/master/docs/API.MD#asyncconnect-decorator
  {
    key: "asyncConnectKeyExample",
    promise: ({ match: { params }, helpers }) =>
      Promise.resolve({
        id: 1,
        name: "value returned from promise for the key asyncConnectKeyExample"
      })
  }
])(App);

const ChildRoute = () => <div>{"child component"}</div>;

// config route
const routes = [
  {
    path: "/",
    component: ConnectedApp,
    routes: [
      {
        path: "/child",
        exact: true,
        component: ChildRoute
      }
    ]
  }
];

// Config store
const store = createStore(
  combineReducers({ reduxAsyncConnect }), // Connect redux async reducer
  window.__data
);
window.store = store;

// App Mount point
hydrate(
  <Provider store={store} key="provider">
    <BrowserRouter>
      {/** Render `Router` with ReduxAsyncConnect middleware */}
      <ReduxAsyncConnect routes={routes} />
    </BrowserRouter>
  </Provider>,
  document.getElementById("root")
);

Server

import { renderToString } from 'react-dom/server'
import StaticRouter from 'react-router/StaticRouter'
import { ReduxAsyncConnect, loadOnServer, reducer as reduxAsyncConnect } from 'redux-connect'
import { parse as parseUrl } from 'url'
import { Provider } from 'react-redux'
import { createStore, combineReducers } from 'redux'
import serialize from 'serialize-javascript'

app.get('*', (req, res) => {
  const store = createStore(combineReducers({ reduxAsyncConnect }))
  const url = req.originalUrl || req.url
  const location = parseUrl(url)

  // 1. load data
  loadOnServer({ store, location, routes, helpers })
    .then(() => {
      const context = {}

      // 2. use `ReduxAsyncConnect` to render component tree
      const appHTML = renderToString(
        <Provider store={store} key="provider">
          <StaticRouter location={location} context={context}>
            <ReduxAsyncConnect routes={routes} helpers={helpers} />
          </StaticRouter>
        </Provider>
      )

      // handle redirects
      if (context.url) {
        req.header('Location', context.url)
        return res.send(302)
      }

      // 3. render the Redux initial data into the server markup
      const html = createPage(appHTML, store)
      res.send(html)
    })
})

function createPage(html, store) {
  return `
    <!doctype html>
    <html>
      <body>
        <div id="app">${html}</div>

        <!-- its a Redux initial data -->
        <script type="text/javascript">
          window.__data=${serialize(store.getState())};
        </script>
      </body>
    </html>
  `
}

API

Usage with ImmutableJS

This lib can be used with ImmutableJS or any other immutability lib by providing methods that convert the state between mutable and immutable data. Along with those methods, there is also a special immutable reducer that needs to be used instead of the normal reducer.

import { setToImmutableStateFunc, setToMutableStateFunc, immutableReducer as reduxAsyncConnect } from 'redux-connect';

// Set the mutability/immutability functions
setToImmutableStateFunc((mutableState) => Immutable.fromJS(mutableState));
setToMutableStateFunc((immutableState) => immutableState.toJS());

// Thats all, now just use redux-connect as normal
export const rootReducer = combineReducers({
  reduxAsyncConnect,
  ...
})

Comparing with other libraries

There are some solutions of problem described above:

  • AsyncProps It solves the same problem, but it doesn't work with redux state. Also it's significantly more complex inside, because it contains lots of logic to connect data to props. It uses callbacks against promises...
  • react-fetcher It's very simple library too. But it provides you only interface for decorating your components and methods to fetch data for them. It doesn't integrated with React Router or Redux. So, you need to write you custom logic to delay routing transition for example.
  • react-resolver Works similar, but isn't integrated with redux.

Redux Connect uses awesome Redux to keep all fetched data in state. This integration gives you agility:

  • you can react on fetching actions like data loading or load success in your own reducers
  • you can create own middleware to handle Redux Async Connect actions
  • you can connect to loaded data anywhere else, just using simple redux @connect
  • finally, you can debug and see your data using Redux Dev Tools

Also it's integrated with React Router to prevent routing transition until data is loaded.

Contributors

Collaboration

You're welcome to PR, and we appreciate any questions or issues, please open an issue!

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].