{"id":13422430,"url":"https://github.com/makeomatic/redux-connect","last_synced_at":"2025-05-15T20:02:21.269Z","repository":{"id":8374977,"uuid":"58172347","full_name":"makeomatic/redux-connect","owner":"makeomatic","description":"Provides decorator for resolving async props in react-router, extremely useful for handling server-side rendering in React","archived":false,"fork":false,"pushed_at":"2023-01-03T17:36:49.000Z","size":1958,"stargazers_count":547,"open_issues_count":97,"forks_count":64,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-05-12T06:05:19.055Z","etag":null,"topics":["immutablejs","isomorphic","node","nodejs","react","react-redux","redux-connect","server-side-rendering","universal-react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/makeomatic.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-06T01:46:10.000Z","updated_at":"2025-03-31T09:40:05.000Z","dependencies_parsed_at":"2023-01-13T14:46:17.775Z","dependency_job_id":null,"html_url":"https://github.com/makeomatic/redux-connect","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makeomatic%2Fredux-connect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makeomatic%2Fredux-connect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makeomatic%2Fredux-connect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/makeomatic%2Fredux-connect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/makeomatic","download_url":"https://codeload.github.com/makeomatic/redux-connect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414477,"owners_count":22067270,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["immutablejs","isomorphic","node","nodejs","react","react-redux","redux-connect","server-side-rendering","universal-react"],"created_at":"2024-07-30T23:00:44.800Z","updated_at":"2025-05-15T20:02:19.691Z","avatar_url":"https://github.com/makeomatic.png","language":"JavaScript","readme":"ReduxConnect for React Router\n============\n[![npm version](https://img.shields.io/npm/v/redux-connect.svg?style=flat-square)](https://www.npmjs.com/package/redux-connect)\n[![Build Status](https://travis-ci.org/makeomatic/redux-connect.svg?branch=master)](https://travis-ci.org/makeomatic/redux-connect)\n\n\nHow do you usually request data and store it to redux state?\nYou create actions that do async jobs to load data, create reducer to save this data to redux state,\nthen connect data to your component or container.\n\nUsually it's very similar routine tasks.\n\nAlso, usually we want data to be preloaded. Especially if you're building universal app,\nor you just want pages to be solid, don't jump when data was loaded.\n\nThis package consist of 2 parts: one part allows you to delay containers rendering until some async actions are happening.\nAnother stores your data to redux state and connect your loaded data to your container.\n\n## Notice\n\nThis is a fork and refactor of [redux-async-connect](https://github.com/Rezonans/redux-async-connect)\n\n## Installation \u0026 Usage\n\nUsing [npm](https://www.npmjs.com/):\n\n`$ npm install redux-connect -S`\n\n```js\nimport { BrowserRouter } from \"react-router-dom\";\nimport { renderRoutes } from \"react-router-config\";\nimport {\n  ReduxAsyncConnect,\n  asyncConnect,\n  reducer as reduxAsyncConnect\n} from \"redux-connect\";\nimport React from \"react\";\nimport { hydrate } from \"react-dom\";\nimport { createStore, combineReducers } from \"redux\";\nimport { Provider } from \"react-redux\";\n\nconst App = props =\u003e {\n  const { route, asyncConnectKeyExample } = props; // access data from asyncConnect as props\n  return (\n    \u003cdiv\u003e\n      {asyncConnectKeyExample \u0026\u0026 asyncConnectKeyExample.name}\n      {renderRoutes(route.routes)}\n    \u003c/div\u003e\n  );\n};\n\n// Conenect App with asyncConnect\nconst ConnectedApp = asyncConnect([\n  // API for ayncConnect decorator: https://github.com/makeomatic/redux-connect/blob/master/docs/API.MD#asyncconnect-decorator\n  {\n    key: \"asyncConnectKeyExample\",\n    promise: ({ match: { params }, helpers }) =\u003e\n      Promise.resolve({\n        id: 1,\n        name: \"value returned from promise for the key asyncConnectKeyExample\"\n      })\n  }\n])(App);\n\nconst ChildRoute = () =\u003e \u003cdiv\u003e{\"child component\"}\u003c/div\u003e;\n\n// config route\nconst routes = [\n  {\n    path: \"/\",\n    component: ConnectedApp,\n    routes: [\n      {\n        path: \"/child\",\n        exact: true,\n        component: ChildRoute\n      }\n    ]\n  }\n];\n\n// Config store\nconst store = createStore(\n  combineReducers({ reduxAsyncConnect }), // Connect redux async reducer\n  window.__data\n);\nwindow.store = store;\n\n// App Mount point\nhydrate(\n  \u003cProvider store={store} key=\"provider\"\u003e\n    \u003cBrowserRouter\u003e\n      {/** Render `Router` with ReduxAsyncConnect middleware */}\n      \u003cReduxAsyncConnect routes={routes} /\u003e\n    \u003c/BrowserRouter\u003e\n  \u003c/Provider\u003e,\n  document.getElementById(\"root\")\n);\n\n```\n\n### Server\n\n```js\nimport { renderToString } from 'react-dom/server'\nimport StaticRouter from 'react-router/StaticRouter'\nimport { ReduxAsyncConnect, loadOnServer, reducer as reduxAsyncConnect } from 'redux-connect'\nimport { parse as parseUrl } from 'url'\nimport { Provider } from 'react-redux'\nimport { createStore, combineReducers } from 'redux'\nimport serialize from 'serialize-javascript'\n\napp.get('*', (req, res) =\u003e {\n  const store = createStore(combineReducers({ reduxAsyncConnect }))\n  const url = req.originalUrl || req.url\n  const location = parseUrl(url)\n\n  // 1. load data\n  loadOnServer({ store, location, routes, helpers })\n    .then(() =\u003e {\n      const context = {}\n\n      // 2. use `ReduxAsyncConnect` to render component tree\n      const appHTML = renderToString(\n        \u003cProvider store={store} key=\"provider\"\u003e\n          \u003cStaticRouter location={location} context={context}\u003e\n            \u003cReduxAsyncConnect routes={routes} helpers={helpers} /\u003e\n          \u003c/StaticRouter\u003e\n        \u003c/Provider\u003e\n      )\n\n      // handle redirects\n      if (context.url) {\n        req.header('Location', context.url)\n        return res.send(302)\n      }\n\n      // 3. render the Redux initial data into the server markup\n      const html = createPage(appHTML, store)\n      res.send(html)\n    })\n})\n\nfunction createPage(html, store) {\n  return `\n    \u003c!doctype html\u003e\n    \u003chtml\u003e\n      \u003cbody\u003e\n        \u003cdiv id=\"app\"\u003e${html}\u003c/div\u003e\n\n        \u003c!-- its a Redux initial data --\u003e\n        \u003cscript type=\"text/javascript\"\u003e\n          window.__data=${serialize(store.getState())};\n        \u003c/script\u003e\n      \u003c/body\u003e\n    \u003c/html\u003e\n  `\n}\n```\n\n## [API](/docs/API.MD)\n\n## Usage with `ImmutableJS`\n\nThis 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.\n\n```js\nimport { setToImmutableStateFunc, setToMutableStateFunc, immutableReducer as reduxAsyncConnect } from 'redux-connect';\n\n// Set the mutability/immutability functions\nsetToImmutableStateFunc((mutableState) =\u003e Immutable.fromJS(mutableState));\nsetToMutableStateFunc((immutableState) =\u003e immutableState.toJS());\n\n// Thats all, now just use redux-connect as normal\nexport const rootReducer = combineReducers({\n  reduxAsyncConnect,\n  ...\n})\n```\n\n## Comparing with other libraries\n\nThere are some solutions of problem described above:\n\n- [**AsyncProps**](https://github.com/ryanflorence/async-props)\n  It solves the same problem, but it doesn't work with redux state. Also it's significantly more complex inside,\n  because it contains lots of logic to connect data to props.\n  It uses callbacks against promises...\n- [**react-fetcher**](https://github.com/markdalgleish/react-fetcher)\n  It's very simple library too. But it provides you only interface for decorating your components and methods\n  to fetch data for them. It doesn't integrated with React Router or Redux. So, you need to write you custom logic\n  to delay routing transition for example.\n- [**react-resolver**](https://github.com/ericclemmons/react-resolver)\n  Works similar, but isn't integrated with redux.\n\n**Redux Connect** uses awesome [Redux](https://github.com/reactjs/redux) to keep all fetched data in state.\nThis integration gives you agility:\n\n- you can react on fetching actions like data loading or load success in your own reducers\n- you can create own middleware to handle Redux Async Connect actions\n- you can connect to loaded data anywhere else, just using simple redux @connect\n- finally, you can debug and see your data using Redux Dev Tools\n\nAlso it's integrated with [React Router](https://github.com/rackt/react-router) to prevent routing transition\nuntil data is loaded.\n\n## Contributors\n- [Vitaly Aminev](https://makeomatic.ca)\n- [Gerhard Sletten](https://github.com/gerhardsletten)\n- [Todd Bluhm](https://github.com/toddbluhm)\n- [Eliseu Monar](https://github.com/eliseumds)\n- [Rui Araújo](https://github.com/ruiaraujo)\n- [Rodion Salnik](https://github.com/sars)\n- [Rezonans team](https://github.com/Rezonans)\n\n## Collaboration\nYou're welcome to PR, and we appreciate any questions or issues, please [open an issue](https://github.com/makeomatic/redux-connect/issues)!\n","funding_links":[],"categories":["Code Design","Uncategorized","Utilities","Redux","目录"],"sub_categories":["Props from server","Uncategorized","Side Effects","React Components","\u003ca id=\"state\"\u003e状态管理\u003c/a\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmakeomatic%2Fredux-connect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmakeomatic%2Fredux-connect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmakeomatic%2Fredux-connect/lists"}