All Projects → 4nte → react-relay-rebind

4nte / react-relay-rebind

Licence: MIT License
Component-scope state management for Relay modern & React.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-relay-rebind

cliff-effects
Cliff effects guidance prototype (archived)
Stars: ✭ 30 (+100%)
Mutual labels:  archived, outdated
kibana4-vagrant
This is a vagrant virtual machine with an Kibana 4 (and ElasticSearch) instance. It belongs to a Kibana 4 tutorial on the following URL:
Stars: ✭ 39 (+160%)
Mutual labels:  archived, outdated
GraphQL-Client-Exploration
Simple exploration of GraphQL Clients
Stars: ✭ 18 (+20%)
Mutual labels:  relay-modern
create-react-app-relay-modern
Create React App + Relay Modern
Stars: ✭ 50 (+233.33%)
Mutual labels:  relay-modern
pyladies-courseware
Homework/task submit and review web app · based on React and Python aiohttp
Stars: ✭ 14 (-6.67%)
Mutual labels:  relay-modern
docker-react-native-android
Docker image used to build React Native projects for Android
Stars: ✭ 28 (+86.67%)
Mutual labels:  relay-modern
klaravel
DEPRECATED Laravel control panel for developers, comes with integrated Scaffold generator, backups, logs, and embedde user guide.
Stars: ✭ 24 (+60%)
Mutual labels:  relay-modern
react-relay-appsync
AppSync for Relay
Stars: ✭ 19 (+26.67%)
Mutual labels:  relay-modern
ton-client-rs
TON Labs SDK Client Library for Rust
Stars: ✭ 15 (+0%)
Mutual labels:  archived
Azure-AppServices-Diagnostics
Azure App Service Diagnostics provides developers ability to write various diagnostics features which helps customers to diagnose and troubleshoot their applications hosted on app services.
Stars: ✭ 42 (+180%)
Mutual labels:  archived
wdlRunR
Elastic, reproducible, and reusable genomic data science tools from R backed by cloud resources
Stars: ✭ 34 (+126.67%)
Mutual labels:  archived
passion
An object-oriented LÖVE game engine
Stars: ✭ 35 (+133.33%)
Mutual labels:  archived
CAVE
[deprecated] Configuration Assessment, Visualization and Evaluation
Stars: ✭ 43 (+186.67%)
Mutual labels:  archived
QR
DEPRECATED The bookmarklet and extensions generate QRCode of the current URL for viewing on mobile devices (Google Chrome/Mozilla Firefox/Opera/Safari)
Stars: ✭ 20 (+33.33%)
Mutual labels:  archived
tooltip
[DEPRECATED] The tooltip that has all the right moves
Stars: ✭ 133 (+786.67%)
Mutual labels:  archived
OSM-Completionist
⛔️ DEPRECATED iOS companion app for OpenStreetMap that allows contributors to complete missing information
Stars: ✭ 17 (+13.33%)
Mutual labels:  archived
DeepDIVA
⛔️ DEPRECATED <Python Framework for Reproducible Deep Learning Experiments>
Stars: ✭ 32 (+113.33%)
Mutual labels:  archived
isomorphic-relay-app
Example isomorphic React-Relay-(Modern / Classic)-Router app and helper lib that connects to Artsy's GraphQL service
Stars: ✭ 13 (-13.33%)
Mutual labels:  relay-modern
VRTK.Tutorials.OculusIntegration
Prefabs and code for use with the Oculus Integration Unity Package
Stars: ✭ 26 (+73.33%)
Mutual labels:  archived
cirrus-ci-web
Web front end for Cirrus CI
Stars: ✭ 48 (+220%)
Mutual labels:  relay-modern

React Relay Rebind Travis npm-version Chat

React relay rebind is a component-scope state management for Relay modern & React.

React-Relay-Rebind is a local state managment for React components which use Relay. Focus of React Relay Rebind is to handle data resolved with Relay mutations and provide it to a component. Component will recieve a state prop for each rebinded mutation. The API declaratively passes down state to components thus simplifying the data flow. The common usecase is non-persistent/local data (e.g UI state) which does not belong and does not deserve to be mixed with persistent data.

⚠️ Warning: Do not use in production! RRR is highly experimental, please stand by until 1.0.0 is released.

Roadmap to production

  • Automagically provide dispatch to commitMutation
  • Simplify state configuration
  • Subscribe to specific piece of state
  • Optional state alias

Installation

Yarn

$ yarn add react-relay-rebind

NPM

$ npm install --save react-relay-rebind

or build from source

$ git clone react-relay-rebind
$ cd react-relay-rebind && yarn install
$ yarn run build

Usage

Provide dispatch to the commitMutation

import { commitMutation } from 'react-relay-rebind';

const mutation = graphql`
  mutation LoginMutation($input: LoginInput!) {
    login(credentials: $input) {
      errors {
        username
        password
      }
    }
  }
`;

const loginMutation = (environment, input, dispatch) => { // dispatch is passed as the last argument
  commitMutation(
    environment,
    {
      mutation,
      variables: input,
    },
    dispatch // provide dispatch to the the commitMutation
  );
}

export default loginMutation;

Rebind mutations with the component

import { rebind } from 'react-relay-rebind';
import { loginMutation } from './loginMutation';

class MyComponent extends React.component {

  handleSubmit(){
    this.props.mutations.login(this.props.relay, this.state);
  }

  handleFieldChange(fieldName){
    return (e) => {
      // Login mutation stateProxy
      const { login } = this.props;

      this.setState({ [fieldName]: e.target.value() });

      // Set login mutation to initial state
      this.login.resetState();
    }
  }

  render(){
    const { errors } = this.props.login.state;
    const usernameClassname = errors.username ? 'has-error' : '';
    const passwordClassname = errors.password ? 'has-error' : '';

    return (
       <div>
        <input
          className={usernameClassname}
          name="username"
          type="text"
          onChange={this.handleFieldChange('username')}
          value={username} />
        <br/>
        <input
          className={passwordClassname}
          name="password"
          type="password"
          onChange={this.handleFieldChange('password')}
          value={password} />
        <br/>
        <input type="submit" onClick={this.handleSubmit} value="Login"/>
      </div>
  }
}

const mutations = {
  login: {
    mutation: LoginMutation,
    initialState: {
      errors: {
        username: null,
        password: null,
      },
    },
  },
};
export default rebind(mutations)(MyComponent);

API Documentation

Rebind

rebind(mutations: mutationsConfiguration)(component: Component): Component

Binds mutations with a component. Composed component will recieve a state proxy as a prop for each mutation specified in the mutations configuration.

Mutations configuration

    {
      mutationName : {
        mutation: function,
        initialState: <any>,
      }
    }

Mutations configuration object contains a property for each mutation binding. Property name be the same as the graphql's mutation name.

  • mutationName: A graphql mutation name.
  • mutation function: A function called by the mutation handler in a component. Mutation function will be provided with a dispatch function as the last argument. Mutation function could be a commitMutation or a function that calls commitMutation but then you must provide a dispatch to the commitMutation as the last argument.
  • initialState: default mutation state.

Commit mutation

commitMutation(environment, config, dispatch)

Commits a mutation and dispatches resolved data to a binded component. This function is almost identical to the Relays commitMutation except that it expectes a dispatch function as a third argument.

State proxy

StateProxy {
  state,
  setState(state),
  resetState()
}

A state proxy is used to read & update a mutation state.

  • state: mutation state
  • setState(): sets mutation state
  • resetState(): sets mutation state to the initialState

Mutation handlers

props.mutations.mutationName

A binded component will recieve mutations prop which contains a mutation handler for each binded mutation. Mutation handler is used to call the mutation defined in the mutation configuration

dispatch

dispatch(state)

A function that updates a mutation state and causes the component to recieve new mutation state

Contributing

Contributions are welcomed! It's suggested to create an issue beforehand to shed some light to others on what kind of change you are working on. Fork, improve & create a pull request.

Development

Use yarn run lint to run a lint

Use yarn run test:cover to run tests

Please build locally before submitting a PR.

Contributors

Backers

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