Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-apollo-mutation-state
Advanced tools
A React HOC for Apollo GraphQL mutation, provides loading & error as props.
A React HOC for Apollo GraphQL mutation, provides loading & error as props.
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
.
mutation
objectAPI | 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
});
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));
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.
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.
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.
FAQs
A React HOC for Apollo GraphQL mutation, provides loading & error as props.
The npm package react-apollo-mutation-state receives a total of 6 weekly downloads. As such, react-apollo-mutation-state popularity was classified as not popular.
We found that react-apollo-mutation-state demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.