GithubHelp home page GithubHelp logo

ptzagk / react-async-prop-provider Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kamaltmo/react-async-prop-provider

0.0 2.0 0.0 454 KB

A React Component for intelligently changing a child components props depending on the state of a promise.

Home Page: https://kamaltmo.github.io/react-async-prop-provider/

License: MIT License

JavaScript 95.15% HTML 4.85%

react-async-prop-provider's Introduction

npm version Build Status Coverage Status

react-async-prop-provider

A React Component for intelligently changing a child components props depending on the state of a promise.

But, why ?

There are many component libraries that out there that provide developers with beautiful stateless components such as buttons. This allows you to easily make these components react to the state of an async function they fire without needing to much boiler plate or using Redux.

Props

  • Action:
    • Type: Function
    • Required: Yes
    • Usage: An Async function or a function return a promise. This is will be used to judge the correct props to provide.
  • pendingProps:
    • Type: Object
    • Required: No
    • Usage: The additional props that you would like to pass while the action is being resolved.
  • fulfilledProps:
    • Type: Object
    • Required: No
    • Usage: The additional props that you would like to pass when the action has been fulfilled.
  • rejectedProps:
    • Type: Object
    • Required: No
    • Usage: The additional props that you would like to pass if the action fails.
  • Children:
    • Type: Function
    • Required: Yes
    • Params:
      • actionHandler (Function) - fires the provided action when called.
      • asyncProps (Object) - The props for the current action state.
    • Usage: This functions result is used as the return for AsyncPropProvider. This pattern is know as render props learn more here

Example

Install

npm i react-async-prop-provider

Usage

import AsyncPropProvider from 'react-async-prop-provider

A Stateless Button component from the Semantic-UI-React component library wrapped in AsyncPropProvider. This allows the buttons to change its presentation depending on the state of async function it fires.

class Example extends Component {
  // Async function
  action() {
    return new Promise(resolve => {
      setTimeout(resolve, 1000);
    });
  }

  render() {
    return (
      <AsyncPropProvider
        action={this.action}
        pendingProps={{ disabled: true, loading: true }}
        fulfilledProps={{ color: "green" }}
        rejectedProps={{ color: "red" }}
      >
        {(actionHandler, asyncProps) => (
          <Button color="blue" onClick={actionHandler} {...asyncProps}>
            A Stateless Button
          </Button>
        )}
      </AsyncPropProvider>
    );
  }
}

react-async-prop-provider's People

Contributors

kamaltmo avatar

Watchers

 avatar  avatar

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.