{"id":16159810,"url":"https://github.com/robertherber/react-as-promised","last_synced_at":"2025-03-18T21:31:57.397Z","repository":{"id":143904685,"uuid":"99124604","full_name":"robertherber/react-as-promised","owner":"robertherber","description":"Library for easily presenting React Components in a control flow with Promise handling","archived":false,"fork":false,"pushed_at":"2017-09-17T22:16:56.000Z","size":2054,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T18:54:46.011Z","etag":null,"topics":["control-flow","placeholder","promise-handling","react"],"latest_commit_sha":null,"homepage":"https://robertherber.github.io/react-as-promised","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/robertherber.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-02T14:18:50.000Z","updated_at":"2023-02-02T15:59:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"5e81c327-47da-4390-86ea-d70983746a05","html_url":"https://github.com/robertherber/react-as-promised","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertherber%2Freact-as-promised","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertherber%2Freact-as-promised/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertherber%2Freact-as-promised/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robertherber%2Freact-as-promised/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robertherber","download_url":"https://codeload.github.com/robertherber/react-as-promised/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243777279,"owners_count":20346357,"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":["control-flow","placeholder","promise-handling","react"],"created_at":"2024-10-10T01:59:51.940Z","updated_at":"2025-03-18T21:31:57.393Z","avatar_url":"https://github.com/robertherber.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[ ![Codeship Status for robertherber/react-as-promised](https://app.codeship.com/projects/ad274460-7c62-0135-699b-424c23212dd2/status?branch=master)](https://app.codeship.com/projects/245704)\n\u003ca href=\"https://www.npmjs.com/package/react-as-promised\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/react-as-promised.svg?maxAge=2592000\" alt=\"npm downloads\" style=\"max-width:100%;\"\u003e\u003c/a\u003e\n\u003ca href=\"https://www.npmjs.com/package/react-as-promised\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/react-as-promised.svg?maxAge=2592000\" alt=\"npm version\" style=\"max-width:100%;\"\u003e\u003c/a\u003e\n\n# react-as-promised\n\n[Demo in Browser](https://robertherber.github.io/react-as-promised)\n\n## Use case\nSometimes you want to present UI as part of a control flow (for example inside a redux action creator). This can be overly complicated if you trigger one action from multiple places in your application. react-as-promised makes it easier by providing a way to present UI components with a promise handle.\n\n```\nimport { Manager } from 'react-as-promised';\nimport ConfirmAgeDialog from './components/ConfirmAgeDialog';\n\nfunction proceed(userNeedsToConfirmAge){\n  if(userNeedsToConfirmAge){\n    const componentPromise = Manager.present(ConfirmAgeDialog);\n\n    return componentPromise\n      .then((userInput) =\u003e {\n        console.log('ok!', userInput);\n      })\n      .catch((error) =\u003e {\n        console.log('oh no!', error)\n      });\n  }\n}\n```\n\n## Placeholder\n\nFor react-as-promised to know where in the React render tree to put your component a placeholder needs to be placed somewhere, a good place would probably be inside your redux provider (so you can use smart components) but outside your navigation:\n\n```\nimport React from 'react';\nimport { Placeholder } from 'react-as-promised';\n//...\n\nclass App extends React.Component\n{\n  render(){\n    return \u003cProvider store={store}\u003e\n      \u003cPlaceholder /\u003e // \u003c-- something like this\n      \u003cRouter history={history}\u003e\n        \u003cRoute path=\"/\" component={App}\u003e\n          \u003cRoute path=\"foo\" component={Foo}/\u003e\n          \u003cRoute path=\"bar\" component={Bar}/\u003e\n        \u003c/Route\u003e\n      \u003c/Router\u003e\n    \u003c/Provider\u003e\n  }\n}\n```\n\n## Wiring up the props\nBy default react-as-promised will supply your presented component with the following props, adhering to the underlying [Bluebird](http://bluebirdjs.com/docs/api-reference.html) promise standard:\n* resolve - to be called when successful\n* reject - to be called when unsuccessful\n* onCancel - used so you can clean up the view before it disappears, useful to handle animations etc (requires that you choose to [enable cancellation with Bluebird](http://bluebirdjs.com/docs/api/cancellation.html))\n\nOf course you might want to use your own prop mappings instead - to allow reusing component logic. To use your own prop names supply them as arguments to present:\n\n```\nManager.present(ConfirmAgeDialog, {}, 'onConfirm', 'onDismiss');\nManager.present(ConfirmAgeDialog, {}, ['onConfirm', 'onProceed'], ['onDismiss']); // react-as-promised can treat multiple props as resolvers/rejecters\n```\n\n## Registering a component\nFor reusability react-as-promised allows you to register a component for later use, which enables you to specify the prop name mapping at configuration time:\n\n```\n//..at setup..\n\nManager.registerComponent('ConfirmAgeDialog', ConfirmAgeDialog, 'onConfirm', 'onDismiss');\n\n//..later..\n\nManager.presentRegistered('ConfirmAgeDialog');\n```\n\n## Specify props\nWe made it easy for you to forward props to the component you're presenting:\n```\n\nconst props = { isRequired: true, theme: 'the-green-one' };\n\nManager.present(DialogWithExternalControl, props)\n```\n\n## Controlling the component from the outside\nIt's common for components to be controlled from the outside. For this we added the `updateProps` method on the returned componentPromise:\n```\nconst hideDialog = () =\u003e {\n  componentPromise.updateProps({ open: false });\n};\n\nconst props = {\n  open: true,\n  onDismiss: hideDialog,\n  onProceed: hideDialog,\n};\n\nconst componentPromise = Manager.present(DialogWithExternalControl, props);\nreturn componentPromise;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertherber%2Freact-as-promised","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobertherber%2Freact-as-promised","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertherber%2Freact-as-promised/lists"}