Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Convenience onX method binding pattern for Link.Component classes #27

Open
richard-engineering opened this issue Jul 13, 2017 · 1 comment

Comments

@richard-engineering
Copy link

richard-engineering commented Jul 13, 2017

Some React components have onHide/onChange/other functions which store the functions to the DOM (beyond our control, library components). Thus if we pass it something like:

class SomeComponent extends React.Component {
  render() {
    const { someLink } = this.props;
    return <Modal onHide={someLink.action((value, event) => !value)} />
  }
}

the function bound to the onHide can get out of sync with the state (it saved a reference to the old Link object to the DOM on first creation instead of re-rendering it).

The workaround for this is to do something like

class SomeComponent extends React.Component {
  boundFunction() {
    const { someLink } = this.props;
    someLink.set(false); // Some Value
  }

  render() {
    return <Modal onHide={this.boundFunction} />
  }
}

It would be nice to have a convenience function on LinkedComponent which can "generate this boundFunction for you". Maybe the usage interface would be something like:

class SomeComponent extends LinkedComponent {
  //Implicitly from LinkedComponent
  bindLink(somePropString, callback) {
    const propLink = this.props[somePropString];
    //Or some other way to make it more generalized?
    link.update(callback);
  }

  render() {
    const { someLink } = this.props;
    return <Modal onHide={this.bindLink(somePropString, () => {})} />
  }
}
@richard-engineering richard-engineering changed the title Convenience onX method binding pattern for Link.Component classes Enhancement: Convenience onX method binding pattern for Link.Component classes Jul 13, 2017
@gaperton
Copy link

May I propose the alternative solution? I think it's good enough and LinkedComponent support would add no value compared to this:

class SomeComponent extends LinkedComponent {
   updateLink = () => this.props.myLink.set( {} );

  render() {
    const { someLink } = this.props;
    return <Modal onHide={ this.updateLink} />
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants