All Projects → developit → Linkstate

developit / Linkstate

Licence: mit
Bind events to state. Works with Preact and React.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Linkstate

discord-giveaways-bot
🎁Giveways Bot using the discord-giveaways package
Stars: ✭ 118 (-59.31%)
Mutual labels:  handler
Svgwave
🌊 SVG Wave is a tiny, free and beautiful SVG gradient waves generator for your UI or website desgin. It offers dead simple UI to customize, and style your waves based on your theme specifications.
Stars: ✭ 255 (-12.07%)
Mutual labels:  preact
React Storefront
Build and deploy e-commerce progressive web apps (PWAs) in record time.
Stars: ✭ 275 (-5.17%)
Mutual labels:  preact
11tyby
Simple 11ty setup using TypeScript, SASS, Preact with partial hydration, and other useful things. Aims to provide the DX of Gatsby, but using 11ty!
Stars: ✭ 38 (-86.9%)
Mutual labels:  preact
graceful
Gracefully exit server (Koa), database (Mongo/Mongoose), Redis clients, and job scheduler (Redis/Bull)
Stars: ✭ 37 (-87.24%)
Mutual labels:  handler
Grafoo
A GraphQL Client and Toolkit
Stars: ✭ 264 (-8.97%)
Mutual labels:  preact
gort
Simple HTTP handler to receive remote calls to run scripts bundled in Docker containers
Stars: ✭ 23 (-92.07%)
Mutual labels:  handler
React Modern Library Boilerplate
Boilerplate for publishing modern React modules with Rollup
Stars: ✭ 285 (-1.72%)
Mutual labels:  preact
2048-game
2048 Game implementation based on Preact+Redux
Stars: ✭ 17 (-94.14%)
Mutual labels:  preact
Next Super Performance
The case of partial hydration (with Next and Preact)
Stars: ✭ 272 (-6.21%)
Mutual labels:  preact
govote-app
Simple Preact app to help people find locations to get PVCs and Vote in the upcoming Nigeria General Elections. It's built on the govote-api API.
Stars: ✭ 34 (-88.28%)
Mutual labels:  preact
chat-with-deno-and-preact
Chat app with Deno + Preact
Stars: ✭ 27 (-90.69%)
Mutual labels:  preact
Unistore
🌶 350b / 650b state container with component actions for Preact & React
Stars: ✭ 2,850 (+882.76%)
Mutual labels:  preact
jest-preset-preact
Jest preset for testing Preact apps
Stars: ✭ 14 (-95.17%)
Mutual labels:  preact
Wouter
🥢 A minimalist-friendly ~1.5KB routing for React and Preact. Nothing else but HOOKS.
Stars: ✭ 3,654 (+1160%)
Mutual labels:  preact
assets
Inpsyde Assets is a Composer package (not a plugin) that allows to deal with scripts and styles in a WordPress site.
Stars: ✭ 30 (-89.66%)
Mutual labels:  handler
Scena
🎬 Scena is a component that represents the timeline of Scene.js. You can control time, properties, and items.
Stars: ✭ 259 (-10.69%)
Mutual labels:  preact
Eo Locale
🌏Internationalize js apps 👔Elegant lightweight library based on Internationalization API
Stars: ✭ 290 (+0%)
Mutual labels:  preact
Preact Redux
➿ Preact integration for Redux (no shim needed!)
Stars: ✭ 284 (-2.07%)
Mutual labels:  preact
Preact Www
📖 Preact documentation website.
Stars: ✭ 272 (-6.21%)
Mutual labels:  preact

linkState

Create an Event handler function that sets a given state property. Works with preact and react.

  • Tiny: ~300 bytes of ES3 gzipped
  • Familiar: it's just a function that does what you would have done manually
  • Standalone: one function, no dependencies, works everywhere

🤔 Why?

linkState() is memoized: it only creates a handler once for each (key, eventPath) combination.

This is important for performance, because it prevents handler thrashing and avoids allocations during render.


Table of Contents


Installation

npm install --save linkstate

The UMD build is also available on unpkg:

<script src="//unpkg.com/linkstate/dist/linkstate.umd.js"></script>

This exposes the linkState() function as a global.


How It Works

It's important to understand what linkState does in order to use it comfortably.

linkState(component, statePath, [valuePath])

  • component: the Component instance to call setState() on
  • statePath: a key/path to update in state - can be dot-notated for deep keys
  • valuePath: optional key/path into the event object at which to retrieve the new state value

It's easiest to understand these arguments by looking at a simplified implementation of linkState itself:

function linkState(component, statePath, valuePath) {
  return event => {
    let update = {};
    update[statePath] = event[valuePath];
    component.setState(update);
  };
}

In reality, accounting for dot-notated paths makes this trickier, but the result is the same.

Here's two equivalent event handlers, one created manually and one created with linkState:

handleInput = e => {
  this.setState({ foo: e.target.value })
}

handleInput = linkState(this, 'foo')

Notice how we didn't specify the event path - if omitted, linkState() will use the checked or value property of the event target, based on its type.

Usage

Standard usage is a function that returns an event handler to update state:

import linkState from 'linkstate';

class Foo extends Component {
  state = {
    text: ''
  };
  render(props, state) {
    return (
      <input
        value={state.text}
        onInput={linkState(this, 'text')}
      />
    );
  }
}

You can also use it as a polyfill. This emulates the behavior of Preact 7.x, which provided linkState() as a method on its Component class. Since you're then calling linkState() as a method of the component instance, you won't have to pass in component as an argument:

import 'linkstate/polyfill';

// Component.prototype.linkState is now installed!

class Foo extends Component {
  state = {
    text: ''
  };
  render(props, state) {
    return (
      <input
        value={state.text}
        onInput={this.linkState('text')}
      />
    );
  }
}

Contribute

First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If it hasn't, just open a new clear and descriptive issue.

Submitting pull requests

Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.

💁 Remember: size is the #1 priority.

Every byte counts! PR's can't be merged if they increase the output size much.

  • Fork it!
  • Clone your fork: git clone https://github.com/<your-username>/linkstate
  • Navigate to the newly cloned directory: cd linkstate
  • Create a new branch for the new feature: git checkout -b my-new-feature
  • Install the tools necessary for development: npm install
  • Make your changes.
  • npm run build to verify your change doesn't increase output size.
  • npm test to make sure your change doesn't break anything.
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request with full remarks documenting your changes.

License

MIT License © Jason Miller

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