Coder Social home page Coder Social logo

linkstate's People

Contributors

developit avatar firaenix avatar nickytonline avatar ofirgeller avatar pl12133 avatar sapegin avatar whitetrefoil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

linkstate's Issues

Is it possible to link multiple states?

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.

NPM package lacks polyfill.js.map

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!)

So what's about memoization?

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.

🤔

Couldn't find preset "es2015" && "stage-0" relative to directory "~/project/node_modules/linkstate"

Expected behavior:

Doesn't require any babel plugins

Actual behavior:

Throws errors, requires "es2015" and "stage-0" babel config.

Steps to reproduce

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 to get target.checked when the input[type=radio]

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>
    );
  }
}

Make TypeScript Definitions More Strict

Although the current TypeScript types for this package are valid, they cause issues with TypeScript projects that have strict mode enabled for the compiler options. The e parameter of the return type signature should not be an implicit any. It should be of type Event or a generic event type.

image

Same prevProps/nextProps as this.props passed to componentDidUpdate / shouldComponentUpdate for nested props

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>
    );
  }
}

Passing Scalar value throws

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.

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.