Skip to content

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

@richard-engineering

Description

@richard-engineering

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, () => {})} />
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions