
Security News
Open Source CAI Framework Handles Pen Testing Tasks up to 3,600× Faster Than Humans
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Create an Event handler function that sets a given state property. Works with preact and react.
🤔 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.
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.
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()
onstatePath
: a key/path to update in state - can be dot-notated for deep keysvaluePath
: optional key/path into the event object at which to retrieve the new state valueIt'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.
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')}
/>
);
}
}
First off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.
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.
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.
git clone https://github.com/<your-username>/linkstate
cd linkstate
git checkout -b my-new-feature
npm install
npm run build
to verify your change doesn't increase output size.npm test
to make sure your change doesn't break anything.git commit -am 'Add some feature'
git push origin my-new-feature
FAQs
Bind events to state. Works with Preact and React.
The npm package linkstate receives a total of 3,465 weekly downloads. As such, linkstate popularity was classified as popular.
We found that linkstate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 9 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.
Security News
CVEForecast.org uses machine learning to project a record-breaking surge in vulnerability disclosures in 2025.