Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

HOC to add a thenable setState to your React components.

License

Notifications You must be signed in to change notification settings

zacanger/with-thenable-setstate

Repository files navigation

with-thenable-setstate

HOC to add a thenable setState to your React components.

Support with PayPal Patreon ko-fi


Installation

npm i with-thenable-setstate

Usage

import * as React from 'react'
import withThenableSetState from 'with-thenable-setstate'
// `require` also works:
// const withThenableSetState = require('with-thenable-setstate')

class Foo extends React.Component {
  state = { a: 0 }

  // async/await example
  async something () {
    try {
      await this.thenableSetState({ a: 1 })
      await doSomethingElseNow()
    } catch (e) {
      // handle e
    }
  }

  // promise syntax example
  somethingElse () {
    this.thenableSetState({ a: 2 })
      .then(doSomethingElseNow)
      .catch((e) => {
        // handle e
      })
  }
}

export default withThenableSetState(Foo)

License

LICENSE