developit / linkstate Goto Github PK
View Code? Open in Web Editor NEWBind events to state. Works with Preact and React.
License: MIT License
Bind events to state. Works with Preact and React.
License: MIT License
Is it possible to link a change event to multiple objects in state?
In my use case, there are three state objects that have 3 inputs in my app but a special case requires me to keep them in sync against one input object.
P.S. Thanks for building this.
Relevant line to change: https://github.com/developit/linkstate/blob/master/src/index.js#L19
Would be nice if this used "valueAsNumber" if the input type is range
or number
.
When using the polyfill (having installed from NPM) webpack throws this warning:
WARNING in ./~/linkstate/polyfill.js
Cannot find SourceMap 'polyfill.js.map': Error: Cannot resolve 'file' or 'directory' ./polyfill.js.map in C:\dev\project\node_modules\linkstate
For the time being I've just removed the reference to the source map from polyfill.js
to stop the warnings scrolling past. I'm not sure how to fix it properly (otherwise I'd have a PR for you!)
Docs say it's memoized and I was curious about how bad memory usage can get over time because of it and just wanted to see what approach did you choose with it. Checked the source code and haven't found any trace of memoization anywhere.
Quick test shows that linkstate(state, 'foo') !== linkstate(state, 'foo')
. Maybe I'm missing something.
What did you mean by
🤔 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.
🤔
Doesn't require any babel plugins
Throws errors, requires "es2015" and "stage-0" babel config.
git clone https://github.com/parcel-bundler/examples
cd examples/preact
yarn
yarn add linkstate
# add some code using linkstate
yarn start
� /home/dalvik/WebstormProjects/preact-parcel/node_modules/linkstate/dist/linkstate.es.js: Couldn't find preset "es2015" relative to directory "/home/dalvik/WebstormProje🚨
🚨 /home/dalvik/WebstormProjects/preact-parcel/node_modules/linkstate/dist/linkstate.es.js: Couldn't find preset "stage-0" relative to directory "/home/dalvik/WebstormProjcts/preact-parcel/node_modules/linkstate"
at /home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
at OptionManager.resolvePresets (/home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (/home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (/home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (/home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (/home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (/home/dalvik/WebstormProjects/preact-parcel/node_modules/babel-core/lib/transformation/file/index.js:135:24)
at JSAsset.getParserOptions (/home/dalvik/WebstormProjects/preact-parcel/node_modules/parcel-bundler/src/assets/JSAsset.js:60:20)
at JSAsset.parse (/home/dalvik/WebstormProjects/preact-parcel/node_modules/parcel-bundler/src/assets/JSAsset.js:68:32)
Why is event.target.checked
rather than event.target.value
?
https://github.com/developit/linkstate/blob/master/src/index.js#L17
example:
this val
is always true
.
import linkState from 'linkstate'
class App extends React.Component {
constructor(props){
super(props);
this.state = {
val: "a"
};
}
render(){
const { val } = this.state;
return (
<form>
<input type="radio" name="foo" value="a" checked={val === "a"} onChange={linkState(this, 'val')} /> a
<input type="radio" name="foo" value="b" checked={val === "b"} onChange={linkState(this, 'val')} /> b
<input type="radio" name="foo" value="c" checked={val === "c"} onChange={linkState(this, 'val')} /> c
</form>
);
}
}
Repro:
import { h, Component } from 'preact';
import linkState from 'linkstate';
class Double extends Component {
componentDidUpdate(prevProps, prevState) {
console.log(prevProps.filter, this.props.filter); // BUG: same
}
render() {
return (
<p>{this.props.filter.value * 2}</p>
);
}
}
export default class App extends Component {
state = {
filter: {
value: 3
}
};
render() {
return (
<div>
<p>Change the number and watch the console. The filter values should be different but are identical.</p>
<input type="number" onInput={linkState(this, 'filter.value')} />
<Double filter={this.state.filter} />
</div>
);
}
}
On the Linked State section of the website, you say:
For custom event handlers, passing scalar values to the handler generated by linkState() will simply use the scalar value. Most of the time, this behavior is desirable.
But when I write something like this:
(e) => linkState(this, 'creditCard.cardNumber')(ParseCardNumber(e.target.value))
for an onInput handler, I get the following error:
linkstate.es.js:21 Uncaught TypeError: Cannot read property 'nodeName' of undefined
at eval (linkstate.es.js:21)
at Object.onInput [as input] (home.js:139)
at HTMLInputElement.eventProxy (preact.js:96)
It looks like the offending code is the first line of the returned handler:
var t = e && e.target || this,
This will cause e
(the passed in value) to pass on the next value e.target
which is undefined as is this
, so t
will end up being undefined.
Wouldn't the correct way to write this code for the intended behavior be the following?
'var t = (e && e.target) || this,`
This is my first issue on this project, but I am currently writing a POC for my company(a Series A funded startup) and am optimistic that we will be moving our Customer-facing apps to Preact from Angular 1.x over the course of the coming year and would like to contribute back to the framework.
Let me know if my suggestion is on the right track, and I'll dash off a PR.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.