Giter Club home page Giter Club logo

react-action-decorators's Introduction

React Action Decorators

Remove menial event handling

Install

npm install react-action-decorators
# or
yarn add react-action-decorators

Example

import React from 'react';
import { withTemplateHelpers } from 'react-action-decorators';

@withTemplateHelpers
export default class MyComponent extends Component {
  render() {
    const [ text, handleTextChange ] = this.useMut('text');

    return (
      <div>
        <input value={text} onChange={handleTextChange} />
      </div>
    );
  }
}

Demo: https://codesandbox.io/s/2067py0prn

Why?

For stateful components that need to handle user input, defining event handlers is verbose.

Before:

export default class MyComponent extends Component {
  firstNameChanged = (e) => {
    this.setState([ firstName: e.target.value ]);
  }

  lastNameChanged = (e) => {
    this.setState([ lastName: e.target.value ]);
  }

  toggler = (e) => {
    this.setState({ isEnabled: e.target.checked });
  }

  render() {
    const { firstName, lastName, isEnabled } = this.state;

    return (
      <div>
        <input value={firstName} onChange={this.firstNameChanged} />
        <input value={lastName} onChange={this.lastNameChanged} />
        <input type='checkbox' checked={isEnabled} onChange={toggler} />
      </div>
    );
  }
}

After:

@withTemplateHelpers
export default class MyComponent extends Component {
  render() {
    const [ firstName, firstNameChanged ] = this.useMut('text');
    const [ lastName, lastNameChanged ] = this.useMut('text');
    const [ isEnabled, toggler ] = this.useToggle('isEnabled');

    return (
      <div>
        <input value={firstName} onChange={firstNameChanged} />
        <input value={lastName} onChange={lastNameChanged} />
        <input type='checkbox' checked={isEnabled} onChange={toggler} />
      </div>
    );
  }
}

Using less code and not having to handle events yourself results in the code being less error prone. It also declaratively conveys intent by coupling a state value with the state updating function (very similar to react hooks).

Available Helpers

  • mut

    render() {
      const { text } = this.state;
      const handleTextChanged = this.mut('text');
    
      return (
        <div>
          <input value={text} onChange={handleTextChange} />
        </div>
      );
    }
  • toggle

    render() {
      const { isEnabled } = this.state;
      const toggler = this.toggle('isEnabled');
    
      return (
        <div>
          <input type='checkbox' checked={isEnabled} onChange={toggler} />
        </div>
      );
    }
  • pipe

    render() {
      const [ text, updateText ] = this.useMut('text');
      const { pipe } = this;
    
      return (
        <input
          type="input"
          value={text}
          onChange={pipe([
            updateText,
            this.validatePipedValue,
            (value: string) => console.log(value)
          ])}
        />
      );
    }
  • useMut

    render() {
      const [ text, handleTextChange ] = this.useMut('text');
    
      return (
        <div>
          <input value={text} onChange={handleTextChange} />
        </div>
      );
    }
  • useToggle

    render() {
      const [ isEnabled, toggler ] = this.useToggle('isEnabled');
    
      return (
        <div>
          <input type='checkbox' checked={isEnabled} onChange={toggler} />
        </div>
      );
    }

react-action-decorators's People

Contributors

nullvoxpopuli avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

giancorzo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.