<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ztorstri</title>
    <description>The latest articles on DEV Community by ztorstri (@ztorstri).</description>
    <link>https://dev.to/ztorstri</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F209740%2F9e91ae83-cc09-4f1d-9196-8f7f52d7150b.png</url>
      <title>DEV Community: ztorstri</title>
      <link>https://dev.to/ztorstri</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ztorstri"/>
    <language>en</language>
    <item>
      <title>Reuseable component logic</title>
      <dc:creator>ztorstri</dc:creator>
      <pubDate>Fri, 09 Aug 2019 18:53:16 +0000</pubDate>
      <link>https://dev.to/ztorstri/reuseable-component-logic-2208</link>
      <guid>https://dev.to/ztorstri/reuseable-component-logic-2208</guid>
      <description>&lt;p&gt;I am implementing a custom table that will have pagination, filtering, sorting, and other common features.  I do not want to use an existing solution, both because this is a good exercise to get familiar with React and because I want the table tailored to my needs.&lt;/p&gt;

&lt;p&gt;The issue I'm running into is with the filtering.  What I want is to put the "does object pass filter" logic in the Filter; I've used this pattern successfully in other languages and it's very clean.&lt;/p&gt;

&lt;p&gt;However, with React all of the logic has to go in the parent because the parent can't call methods on the child.  So I'm stuck. &lt;/p&gt;

&lt;p&gt;Here's what I &lt;em&gt;want&lt;/em&gt; to do, roughly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class FilterContainer extends Component {
  constructor(props) {
    super(props);

    this.toggle = this.toggle.bind(this);

    this.state = { isOpen: false };
  }

  toggle() {
    this.setState({ isOpen: !this.state.isOpen });
  }

  render() {
    return (
      &amp;lt;Fragment&amp;gt;
        &amp;lt;FaFilter id="filter-icon" /&amp;gt;
        &amp;lt;Popover placement="right" isOpen={this.state.isOpen} target="filter-icon" toggle={this.toggle}&amp;gt;
          &amp;lt;PopoverHeader&amp;gt;Filter table&amp;lt;/PopoverHeader&amp;gt;
          &amp;lt;PopoverBody&amp;gt;
            {this.props.children}
          &amp;lt;/PopoverBody&amp;gt;
        &amp;lt;/Popover&amp;gt;
      &amp;lt;/Fragment&amp;gt;
    );
  }
};

class Filter extends Component {
  constructor(props) {
    super(props);

    this.setValue = this.setValue.bind(this);
  }

  setValue(event) {
    this.props.setValue(this.props.name, event.target.value);
  }

  // I want this as a method on the filter because I will have different types of
  // filters, and I don't want to duplicate this everywhere I use a filter
  passesFilter(obj){
    if (obj.hasownproperty(this.props.name)){
      if (obj[this.props.name].contains(this.props.value)){
        return true;
      }
    }
  }

  render() {
    return (
      &amp;lt;Fragment&amp;gt;
        &amp;lt;Label&amp;gt;
          {this.props.name}

          &amp;lt;Input
            id={this.props.name + "-value"}
            type="text"
            value={this.props.value}
            onChange={this.setValue} /&amp;gt;
        &amp;lt;/Label&amp;gt;
      &amp;lt;/Fragment&amp;gt;
    );
  }
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But now imagine instead of Filter, I had a StringFilter which could handle case sensitivity and regex, a BoolFilter which is just true/false, maybe a DateFilter, etc.  Each one of those has the concept of "passes filter", and duplicating them in the DataTable as well as anywhere else I want to use them sucks.&lt;/p&gt;

&lt;p&gt;Hopefully this makes sense, if not I can provide more detail.&lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
