All Projects → lhz516 → react-apollo-mutation-state

lhz516 / react-apollo-mutation-state

Licence: MIT license
A React HOC wrapper for Apollo GraphQL mutation, provides loading and error in props

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-apollo-mutation-state

Stateviews
Create & Show progress, data or error views, the easy way!
Stars: ✭ 367 (+2193.75%)
Mutual labels:  state, loading
PageStatusTransformer
A low invasive state management on Android
Stars: ✭ 12 (-25%)
Mutual labels:  state, loading
Pagestatemanager
manage the loading,emtpy,error state of page, use in xml or just in code
Stars: ✭ 173 (+981.25%)
Mutual labels:  state, loading
React Loads
React Loads is a backend agnostic library to help with external data fetching & caching in your UI components.
Stars: ✭ 268 (+1575%)
Mutual labels:  state, loading
Multistatepage
Android APP缺省页的正确打开方式 高度解耦、低侵入、易拓展 多状态视图状态切换器
Stars: ✭ 433 (+2606.25%)
Mutual labels:  state, loading
Stateview
✨ StateView is an invisible, zero-sized View that can be used to lazily inflate loadingView/emptyView/retryView at runtime.
Stars: ✭ 573 (+3481.25%)
Mutual labels:  state, loading
Statefulviewcontroller
Placeholder views based on content, loading, error or empty states
Stars: ✭ 2,139 (+13268.75%)
Mutual labels:  state, loading
boutique
Immutable data storage
Stars: ✭ 44 (+175%)
Mutual labels:  state
shopify-node-react-app
Shopify paid app in Node.js & React.js that connects to a store and lets merchants select products to automatically discount them in the Shopify admin interface.
Stars: ✭ 29 (+81.25%)
Mutual labels:  apollo
alda
A boilerplate for React isomorphic aplication with Material Design
Stars: ✭ 16 (+0%)
Mutual labels:  apollo
apollo-cache-instorage
Apollo Cache implementation that facilitates locally storing resources
Stars: ✭ 98 (+512.5%)
Mutual labels:  apollo
erxes-widgets-api
Deprecated: Repo is now integrated with erxes-api
Stars: ✭ 22 (+37.5%)
Mutual labels:  apollo
loading-indicator
🚦 Simple and customizable command line loading indicator
Stars: ✭ 18 (+12.5%)
Mutual labels:  loading
RxApolloClient
RxSwift extensions for Apollo Client
Stars: ✭ 46 (+187.5%)
Mutual labels:  apollo
public salaries
Public sector employee salaries
Stars: ✭ 16 (+0%)
Mutual labels:  state
redwood
The App Framework for Startups
Stars: ✭ 15,079 (+94143.75%)
Mutual labels:  apollo
vue-reactive-store
A VueX alternative : declarative + reactive + centralized way to structure your data store. Inspired by VueX and Vue.js . Compatible with vue-devtools.
Stars: ✭ 27 (+68.75%)
Mutual labels:  state
kerrigan
基于Tornado实现的一套配置中心,可基于分项目、环境管理配置,语法高亮、对比历史版本、快速回滚等,并提供Restful风格的API
Stars: ✭ 57 (+256.25%)
Mutual labels:  apollo
ngx-loading-mask
Angular 5+ simple loading-mask ui component.
Stars: ✭ 22 (+37.5%)
Mutual labels:  loading
isMounted
React hook to check if the component is still mounted
Stars: ✭ 93 (+481.25%)
Mutual labels:  state

React Apollo Mutation State

A React HOC for Apollo GraphQL mutation, provides loading & error as props.

Usage

Install from NPM

$ npm i react-apollo-mutation-state --save

Config the HOC

import mutationState from 'react-apollo-mutation-state';

const withMutationState = mutationState({
  // name - {String} Optional. Default: 'mutation'
  // Variable name of the object for passing to props.
  name: 'mutation'
});
// For default config
const withMutationState = mutationState();

The higher order component withMutationState passes an object to props, default called mutation.

API's for the injected mutation object

API Type Description
mutation.set Function {Object} Set loading & error state
mutation.loading Boolean Read only. Current loading state
mutation.error Object | Null Read only. Error object if any
mutation.set({
  loading: true, // {Boolean} Required, Default: true
  error: null // {Object|Null} Optional, Default: null
});

Example

import mutationState from 'react-apollo-mutation-state';
import { graphql } from 'react-apollo';

const MyComponent = ({submit, mutation}) => (
  <form onSubmit={submit}>
    <input type="text" name="message" />
    {
      mutation.loading ?
        <button disabled>Loading...</button> :
        <button type="submit">Send</button>
    }
    <p>{mutation.error ? mutation.error.message : null}</p>
  </form>
);

const withData = graphql(PARSED_SUBMIT_MUTATION, {
  props: ({mutate, ownProps}) => ({
    const { mutation } = ownProps;
    submit: e => {
      e.preventDefault();
      mutation.set({ loading: true });
      mutate({
        variables: {
          message: e.target.message.value
        },
      }).then(() => {
        mutation.set({ loading: false, error: null });
      }).catch(error => {
        mutation.set({ loading: false, error });
      });
    },
  }),
});

const withMutationState = mutationState();

export default withMutationState(withData(MyComponent));

FAQ

Why use this HOC?

To set loading/error state in a GraphQL mutation container and get loading/error state as props in a UI component so that the UI component can be stateless.

Can I use Redux to achieve the same thing?

Yes. However in many cases, one loading (submitting/saving) state is only for a particular button or component. Saving one loading state in Redux store for a single button is kinda too complicated and takes time to modify. So let HOC to make it easy.

What's the future of this project?

Currently react-apollo-mutation-state only handles loading & error state of mutation, but it definitely can be more. There might be more interactions about mutation using this HOC in the future.

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