{"id":20714420,"url":"https://github.com/vydia/react-loading-switch","last_synced_at":"2025-04-23T08:51:29.544Z","repository":{"id":41203555,"uuid":"130629199","full_name":"Vydia/react-loading-switch","owner":"Vydia","description":"React component API for easily composing the render logic surrounding react-apollo data fetching, loading, and error handling.","archived":false,"fork":false,"pushed_at":"2018-05-06T14:53:52.000Z","size":84,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-26T10:44:55.765Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Vydia.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":"2018-04-23T02:27:06.000Z","updated_at":"2019-05-07T01:08:46.000Z","dependencies_parsed_at":"2022-08-29T14:31:07.193Z","dependency_job_id":null,"html_url":"https://github.com/Vydia/react-loading-switch","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vydia%2Freact-loading-switch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vydia%2Freact-loading-switch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vydia%2Freact-loading-switch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Vydia%2Freact-loading-switch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Vydia","download_url":"https://codeload.github.com/Vydia/react-loading-switch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250403140,"owners_count":21424773,"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":[],"created_at":"2024-11-17T02:31:48.924Z","updated_at":"2025-04-23T08:51:29.487Z","avatar_url":"https://github.com/Vydia.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"react-loading-switch 🐶\n==\n\nReact component API for easily composing the render logic surrounding react-apollo data fetching, loading, and error handling.\n\nCompatible with React, React Native, React Web, React anything!\n\nGetting Started\n--\n\n```shell\nnpm i --save react-loading-switch\n```\n\nWhy?\n--\n\n### Data-related conditional rendering code mucking up our render functions\n\nIn our experience, re-writing identical or similar logic in every component can lead to problems ❌\n\n - Multiple programming styles result in different-looking code.\n - Difficult to digest at a glance.\n - Easy to make a mistake if hard-coding everywhere.\n - These problems grow as the codebase grows.\n - Wasted brain cycles thinking about it, writing it, reviewing it.\n\n#### Say goodbye to `if (loading)` and `if (error)` 👋\n\nWith react-loading-switch, we won't need this:\n\n```js\nconst Puppy = ({ loading, error, puppy }) =\u003e {\n  if (error) {\n    return \u003cRenderError error={error} /\u003e\n  }\n\n  if (!puppy) {\n    if (loading) {\n      return \u003cRenderLoading /\u003e\n    }\n\n    return \u003cRenderError error={new Error('Missing puppy data!')} /\u003e\n  }\n\n  return (\n    \u003cView\u003e{ `Finally the puppy is here! ${puppy.id}` }\u003c/View\u003e\n  )\n}\n```\n\nWe won't need this:\n\n```js\nconst Puppy = ({ loading, error, puppy }) =\u003e {\n  if (loading) return \u003cRenderLoading /\u003e\n  if (error) return \u003cRenderError error={error} /\u003e\n\n  return \u003cView\u003e{ `Finally the puppy is here! ${puppy.id}` }\u003c/View\u003e\n}\n```\n\n### Instead, compose this logic with `react-loading-switch` ✅\n\n - Consistent JSX component API.\n - Easy to digest at a glance.\n - Extensible \u0026 Functional\n - Optionally centralize a shared configuration across many components.\n   - It's just a react component. Wrap it with some default props and export.\n\n#### Hello `\u003cLoadingSwitch /\u003e` 🍻\n\nThis example uses all available props, but in practice it gets cleaner:\n\n```js\nimport LoadingSwitch from 'react-loading-switch'\n\nconst Puppy = ({ loading, error, puppy }) =\u003e (\n  \u003cLoadingSwitch\n    error={error}\n    errorWhenMissing={() =\u003e new Error('Missing puppy data!')}\n    loading={loading}\n    renderError={(error) =\u003e \u003cDataError error={error} /\u003e}\n    renderLoading={() =\u003e \u003cLoading /\u003e}\n    require={puppy}\n  \u003e\n    { () =\u003e (\n      \u003cView\u003e{ `The puppy data is here! ${puppy.id}` }\u003c/View\u003e\n    ) }\n  \u003c/LoadingSwitch\u003e\n)\n```\n\n### DRY it up by wrapping with some default props 🤔\n\nShare identical behavior across similar components 👩‍👦‍👦\n\n```js\nimport LoadingSwitch from 'react-loading-switch'\n\nexport const PuppyLoadingSwitch = (props) =\u003e (\n  \u003cLoadingSwitch\n    errorWhenMissing={() =\u003e new Error('Could not find puppy!')}\n    renderLoading={() =\u003e \u003cp\u003eLoading puppies...\u003c/p\u003e}\n    renderError={(error) =\u003e \u003cp\u003eError: {error.message}\u003c/p\u003e}\n    {...props}\n  /\u003e\n)\n```\n\n#### Use `\u003cPuppyLoadingSwitch /\u003e` in every component that shares this logic\n\nNow we're talkin' 🎉\n\n```js\nimport PuppyLoadingSwitch from '../PuppyLoadingSwitch'\n\nconst Puppy = ({ loading, error, puppy }) =\u003e (\n  \u003cPuppyLoadingSwitch\n    error={error}\n    loading={loading}\n    require={puppy}\n  \u003e\n    { () =\u003e (\n      \u003cView\u003e{ `The puppy data is here! ${puppy.id}` }\u003c/View\u003e\n    ) }\n  \u003c/PuppyLoadingSwitch\u003e\n)\n```\n\nYou can use one LoadingSwitch component for your entire application, or you can\nuse different LoadingSwitches in different areas. It's up to you!\n\n### The function-child prop / child-render prop receives the value of `require`\n\nThis optional feature allows us to avoid long property lookup chains in JSX.\nCompare the below to the above. Notice the lack of `data.puppy.whatever`\n\n```js\nconst PuppyBirthday = ({ loading, error, data}) =\u003e (\n  \u003cPuppyLoadingSwitch\n    /* ... */\n    require={data \u0026\u0026 data.puppy}\n  \u003e\n    { ({ name, birthday }) =\u003e (\n      \u003cView\u003e{ `${name}'s birthday is ${birthday}!` }\u003c/View\u003e\n    ) }\n  \u003c/PuppyLoadingSwitch\u003e\n)\n```\n\n### With React-Apollo `\u003cQuery /\u003e` components\n\n```js\nimport PuppyLoadingSwitch from '../PuppyLoadingSwitch'\nimport { Query } from 'react-apollo'\n\nconst GET_PUPPY = gql`\n  query puppy($puppyId: ID!) {\n    puppy(id: $puppyId) {\n      id\n      name\n      birthday\n    }\n  }\n`;\n\nconst PuppyBirthday = ({ puppyId }) =\u003e (\n  \u003cQuery query={GET_PUPPY} variables={{ puppyId }}\u003e\n    {({ loading, error, data}) =\u003e (\n      \u003cPuppyLoadingSwitch\n        error={error}\n        loading={loading}\n        require={data \u0026\u0026 data.puppy}\n      \u003e\n        { ({ name, birthday }) =\u003e (\n          \u003cView\u003e{ `${name}'s birthday is ${birthday}!` }\u003c/View\u003e\n        ) }\n      \u003c/PuppyLoadingSwitch\u003e\n    )}\n  \u003c/Query\u003e\n)\n```\n\n### Versitile `require` prop uses JavaScript truthy/falsey checking.\n\nFalsey in JavaScript: `false || null || undefined || 0 || '' || NaN`\n\n```js\nconst Puppy = ({ loading, error, someData, moreData }) =\u003e (\n  \u003cPuppyLoadingSwitch\n    /* ... */\n    require={someData \u0026\u0026 moreData \u0026\u0026 moreData.foo}\n  \u003e\n    { () =\u003e (\n      \u003cView\u003e{ moreData.foo.name }\u003c/View\u003e\n    ) }\n  \u003c/PuppyLoadingSwitch\u003e\n)\n```\n\nAPI\n--\n\nSee the [test/](test/) directory in this repo for detailed snapshot tests that cover the whole API.\n\nReact-Apollo fetch policies\n--\n\nMost of the [React-Apollo example apps](https://github.com/apollographql/react-apollo/blob/d57f25237c69f78a7e52b586d2303844baf2d4e0/examples/ssr/imports/app.js#L25-L43) use [this pattern](https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-loading), where loading takes precedence:\n\n```js\nexport const Character = withCharacter(({ loading, hero, error }) =\u003e {\n  if (loading) return \u003cdiv\u003eLoading\u003c/div\u003e;\n  // ...\n```\n\n### `fetchPolicy: 'cache-and-network'` conflicts with the above\n\nExcerpt from the [`apollo-client` README](https://github.com/apollographql/apollo-client/blob/9ebbb61fb1061f56edf0bedb965c9954f75afead/docs/source/api/react-apollo.md#dataloading)\n\n\u003e However, just because `data.loading` is true it does not mean that you won’t have data. For instance, if you already have `data.todos`, but you want to get the latest todos from your API `data.loading` might be true, but you will still have the todos from your previous request.\n\ntl;dr we might still want to render the data we have, even if `loading === true`.\n\n### ReactLoadingSwitch considers `data` before `loading`\n\nAs long as there is no `error`, and `require` is truthy, it renders its `children`; even if `loading === true`. Now we can safely use the `cache-and-network` fetch-policy with no chance of seeing a loading state when we have data we could be rendering.\n\nFrom [`src/LoadingSwitch.js`](https://github.com/Vydia/react-loading-switch/blob/0e87d845f129cba525d44c4162d4b2305a2826fd/src/LoadingSwitch.js#L55-L67)\n\n```js\nif (error) {\n  return renderError(error)\n}\n\nif (!require) {\n  if (loading) {\n    return renderLoading()\n  }\n\n  if (errorWhenMissing) {\n    return renderError(typeof errorWhenMissing === 'function' ? errorWhenMissing() : errorWhenMissing)\n  }\n}\n\nreturn children(require)\n```\n\n#### However, it's easy to revert to the classic example behavior\n\nIn this example, `renderLoading` will be rendered if `loading` is truthy, even if we have some other data:\n\n```js\nrequire={!loading \u0026\u0026 puppy}\n```\n\nNow when `loading` is truthy `require` evaluates falsey.\n\n```js\nimport PuppyLoadingSwitch from '../PuppyLoadingSwitch'\n\nconst Puppy = ({ loading, error, puppy }) =\u003e (\n  \u003cPuppyLoadingSwitch\n    error={error}\n    loading={loading}\n    require={!loading \u0026\u0026 puppy}\n  \u003e\n    { () =\u003e (\n      \u003cView\u003e\n        { `We are not loading and the puppy data is here! ${puppy.id}` }\n      \u003c/View\u003e\n    ) }\n  \u003c/PuppyLoadingSwitch\u003e\n)\n```\n\nOr, if we only care about `loading` and `error`, we don't need to check for data presence:\n\n```js\nimport PuppyLoadingSwitch from '../PuppyLoadingSwitch'\n\nconst Puppy = ({ loading, error, puppy }) =\u003e (\n  \u003cPuppyLoadingSwitch\n    error={error}\n    loading={loading}\n    require={!loading}\n  \u003e\n    { () =\u003e (\n      \u003cView\u003e\n        { `We are not loading! ${puppy.id}` }\n      \u003c/View\u003e\n    ) }\n  \u003c/PuppyLoadingSwitch\u003e\n)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvydia%2Freact-loading-switch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvydia%2Freact-loading-switch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvydia%2Freact-loading-switch/lists"}