<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ivan Starkov on Medium]]></title>
        <description><![CDATA[Stories by Ivan Starkov on Medium]]></description>
        <link>https://medium.com/@icelabaratory?source=rss-c3986cb346ee------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*i4Lbi09hF79EY_tu8iZT5Q.jpeg</url>
            <title>Stories by Ivan Starkov on Medium</title>
            <link>https://medium.com/@icelabaratory?source=rss-c3986cb346ee------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:17:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@icelabaratory/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Typing Higher-order Components in Recompose With Flow]]></title>
            <link>https://medium.com/flow-type/flow-support-in-recompose-1b76f58f4cfc?source=rss-c3986cb346ee------2</link>
            <guid isPermaLink="false">https://medium.com/p/1b76f58f4cfc</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typescript]]></category>
            <dc:creator><![CDATA[Ivan Starkov]]></dc:creator>
            <pubDate>Sun, 03 Sep 2017 18:54:28 GMT</pubDate>
            <atom:updated>2017-09-06T22:51:07.378Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qXHT7N-iOCcwUI2m4-QU7w.jpeg" /></figure><p>One month ago <a href="https://github.com/acdlite/recompose">Recompose</a> landed an official Flow library definition. The definitions were a long time coming, considering the original <a href="https://github.com/acdlite/recompose/pull/241">PR</a> was created by <a href="https://twitter.com/GiulioCanti">@GiulioCanti</a> a year ago.</p><p>It took a while to land the definitions because of one problem. I got stuck typing one of simplest enhancers in the Recompose library: <a href="https://github.com/acdlite/recompose/blob/master/docs/API.md#withprops">withProps</a>.</p><p>Type inference worked, but not in a way I expected. Errors were not detected, error messages were cryptic, and the readability of intersection types didn’t make me happy. The only fix was to declare almost every input-output type for each enhancer but I’m too lazy to do that well. Playing with type definitions didn’t give me a better result, so I gave up.</p><p>The game changer was object type spread, which landed in <a href="https://github.com/facebook/flow/releases/tag/v0.42.0">Flow v0.42.0</a>. Object type spread provides functionality for “spreading” two object types together with the same semantics as JavaScript’s runtime value spread. The syntax is also almost exactly the same:</p><pre><strong>type</strong> A = { ...B };<br><strong>const</strong> a = { ...b };</pre><p>Currently the object type spread logic only exists for the type spread syntax, but Flow will be using the same logic to implement spread for values in an upcoming version.</p><p>With object spread for types, a small output type definition change to the withProps enhancer gave me the expected result:</p><pre>// HOC here is a higher-order component<br>// a function that takes a component and returns a new component.</pre><pre><strong>type</strong> HOC&lt;A, B&gt; = (a: React.ComponentType&lt;A&gt;) =&gt;<br>  React.ComponentType&lt;B&gt;</pre><pre>// change output type of withProps<br>// from `HOC&lt;A &amp; B, B&gt;` to `HOC&lt;{ ...$Exact&lt;B&gt;, ...A }, B&gt;`<br>// HOC here is a higher-order component<br>// a function that takes a component and returns a new component<br>// or in terms of flow: type HOC&lt;A, B&gt; = <br>// (a: React.ComponentType&lt;A&gt;) =&gt; React.ComponentType&lt;B&gt;</pre><pre><strong>type</strong> EnhancedCompProps = { b: number };</pre><pre><strong>const</strong> enhancer2: HOC&lt;*, EnhancedCompProps&gt; = compose(<br>  withProps(({ b }) =&gt; ({<br>    b: `${b}`,<br>  })),<br>  withProps(({ b }) =&gt; ({<br>    // <strong>$ExpectError</strong>: The operand of an arithmetic<br>    // operation must be a number.<br>    c: 1 * b,<br>  }))<br>)</pre><p>I was able to declare just the enhanced component props type definition; then all types were inferred properly, and the error was readable.</p><p>It was clear that if it were possible to make the same idea work for most Recompose enhancers, it would be easy to work with Flow and Recompose.</p><p>And thanks to Flow type inference and <a href="https://twitter.com/GiulioCanti">@GiulioCanti</a>, for most enhancers it already worked.</p><h3>Meet Recompose + Flow</h3><p>In most cases all you need to do is to declare a props type of enhanced component. Flow will infer all the other types you need.</p><pre>// @flow</pre><pre><strong>import</strong> * <strong>as</strong> React <strong>from</strong> &#39;react&#39;;<br><strong>import</strong> {<br>  compose,<br>  defaultProps,<br>  withProps,<br>  <strong>type</strong> HOC,<br>} <strong>from</strong> &#39;recompose&#39;;</pre><pre><strong>type</strong> EnhancedComponentProps = {<br>  text?: string,<br>};</pre><pre><strong>const</strong> baseComponent = ({ text }) =&gt; &lt;<strong>div</strong>&gt;{text}&lt;/<strong>div</strong>&gt;;</pre><pre><strong>const</strong> enhance: HOC&lt;*, EnhancedComponentProps&gt; = compose(<br>  defaultProps({<br>    text: &#39;world&#39;,<br>  }),<br>  withProps(({ text }) =&gt; ({<br>    text: `Hello ${text}`<br>  }))<br>);</pre><pre><strong>const</strong> EnhancedComponent = enhance(baseComponent);</pre><pre><strong>export default</strong> EnhancedComponent;</pre><p>You don’t need to provide types for arguments to enhancers. Flow will infer them automatically.</p><p>Remove defaultProps in the example above and you would immediately get a Flow error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*61AK7PXL1MGGn0OVNbuQbg.png" /></figure><p>The other wonderful feature is Flow’s ability to infer types automatically, which makes the Recompose experience even better. See it in action:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*1IMRs5GY5WXV5fCYiimkcw.gif" /></figure><p>For me this feature is much cooler than error detection, as it allows you to read and understand the code much faster.</p><p>The magic above works for most Recompose enhancers, but not all. Any type system has its limitations, so for some enhancers you will need to provide type information for every special case (no automatic type inference), or use the following recommendations:</p><ul><li>flattenProp, renameProp, renameProps—use withProps</li><li>withReducer, withState—use withStateHandlers</li><li>lifecycle—write your own enhancer instead; see <a href="https://github.com/acdlite/recompose/blob/master/types/flow-typed/recompose_v0.24.x/flow_v0.53.x-/test_mapPropsStream.js">this test</a> for an example</li><li>mapPropsStream—see the <a href="https://github.com/acdlite/recompose/blob/master/types/flow-typed/recompose_v0.24.x/flow_v0.53.x-/test_mapPropsStream.js">test</a> for an example</li></ul><p>For an example of how to type enhancers like this, see <a href="https://github.com/acdlite/recompose/blob/master/types/flow-typed/recompose_v0.24.x/flow_v0.53.x-/test_voodoo.js">this test</a>.</p><h3>Using Recompose and Flow with React class components</h3><p>Sometimes you need to use Recompose with React class components. You can use the following helper to extract the property type from an enhancer:</p><pre>// Extract type from any enhancer</pre><pre><strong>type</strong> HOCBase_&lt;A, B, C: HOC&lt;A, B&gt;&gt; = A;<br><strong>type</strong> HOCBase&lt;C&gt; = HOCBase_&lt;*, *, C&gt;;</pre><p>And use it within your component declaration:</p><pre><strong>type</strong> MyComponentProps = HOCBase&lt;typeof myEnhancer&gt;;</pre><pre><strong>class</strong> MyComponent <strong>extends</strong> React.Component&lt;MyComponentProps&gt; {<br>  render() { /* ... */ }<br>}</pre><pre><strong>const</strong> MyEnhancedComponent = myEnhancer(MyComponent);</pre><h3>Write your own enhancers</h3><p>To write your own simple enhancer refer to the <a href="https://flow.org/en/docs/react/hoc/">Flow documentation</a> and you will end up with something like:</p><pre>// @flow</pre><pre><strong>import</strong> * <strong>as</strong> React <strong>from</strong> &#39;react&#39;;<br><strong>import</strong> { compose, withProps, <strong>type</strong> HOC } <strong>from</strong> &#39;recompose&#39;;</pre><pre><strong>function</strong> mapProps&lt;BaseProps: {}, EnhancedProps&gt;(<br>  mapperFn: EnhancedProps =&gt; BaseProps,<br>): (React.ComponentType&lt;BaseProps&gt;) =&gt; React.ComponentType&lt;EnhancedProps&gt; {<br>  <strong>return</strong> Component =&gt; props =&gt; &lt;<strong>Component</strong> {...mapperFn(props)} /&gt;;<br>}</pre><pre><strong>type</strong> EnhancedProps = { hello: string };</pre><pre><strong>const</strong> enhancer: HOC&lt;*, EnhancedProps&gt; = compose(<br>  mapProps(({ hello }) =&gt; ({<br>    hello: `${hello} world`,<br>    len: hello.length,<br>  })),<br>  withProps(props =&gt; ({<br>    helloAndLen: `${props.hello} ${props.len}`,<br>  })),<br>);</pre><p>For class-based enhancers:</p><pre>// @flow</pre><pre><strong>import</strong> * <strong>as</strong> React <strong>from</strong> &#39;react&#39;;<br><strong>import</strong> { compose, withProps, <strong>type</strong> HOC } <strong>from</strong> &#39;recompose&#39;;</pre><pre><strong>function</strong> fetcher&lt;Response: {}, Base: {}&gt;(<br>  dest: string,<br>  nullRespType: ?Response,<br>): HOC&lt;{ ...$Exact&lt;Base&gt;, data?: Response }, Base&gt; {<br>  <strong>return</strong> BaseComponent =&gt;<br>    <strong>class</strong> Fetcher<br>      <strong>extends</strong> React.Component&lt;Base, { data?: Response }&gt; {</pre><pre>      state = { data: <strong>undefined</strong> };</pre><pre>      componentDidMount() {<br>        fetch(dest)<br>          .then(r =&gt; r.json())<br>          .then((data: Response) =&gt; this.setState({ data }));<br>      }</pre><pre>      render() {<br>        <strong>return</strong> (<br>          &lt;<strong>BaseComponent</strong><br>            {...this.props}<br>            {...this.state}<br>          /&gt;<br>        );<br>      }<br>}</pre><pre><strong>type</strong> EnhancedCompProps = { b: number };<br><strong>type</strong> FetchResponseType = { hello: string, world: number };</pre><pre><strong>const</strong> enhancer: HOC&lt;*, EnhancedCompProps&gt; = compose(<br><em>  // pass response type via typed null</em><br>  fetcher(&#39;http://endpoint.ep&#39;, (null: ?FetchResponseType)),</pre><pre><em>  // see here fully typed data</em><br>  withProps(({ data }) =&gt; ({<br>    data,<br>  }))<br>);</pre><p>Now Flow will infer the type of data properly, so you can safely use it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*qdh3zvCJwmd959-L4ok7YA.png" /></figure><h3>Links</h3><ul><li>A fully-typed <a href="https://github.com/acdlite/recompose/tree/master/types/flow-example">example project</a> of this <a href="https://grader-meets-16837.netlify.com/">menu app</a></li><li>Usage examples can be found in <a href="https://github.com/acdlite/recompose/tree/master/types/flow-typed/recompose_v0.24.x/flow_v0.53.x-">tests</a></li></ul><h3>P.S.</h3><p>Your move, <a href="https://twitter.com/calebmer">Caleb</a> ;-)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*INTH4OgBJ7jjGFy3WYmJiQ.png" /></figure><blockquote><strong>Editor’s Note: </strong>Done<strong> 😉</strong> <a href="https://github.com/facebook/flow/commit/ab9bf44c725efd2ed6d7e1e957c5566b6eb6f688">https://github.com/facebook/flow/commit/ab9bf44c725efd2ed6d7e1e957c5566b6eb6f688</a></blockquote><blockquote>Are you creating something cool with Recompose and Flow? We want to hear about it. Send a message to <a href="https://twitter.com/flowtype">flowtype</a> on Twitter so we can share what you are building with our community so that we all may celebrate your efforts to make writing JavaScript more delightful!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1b76f58f4cfc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/flow-type/flow-support-in-recompose-1b76f58f4cfc">Typing Higher-order Components in Recompose With Flow</a> was originally published in <a href="https://medium.com/flow-type">Flow</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[compose in recompose]]></title>
            <link>https://medium.com/@icelabaratory/why-recompose-uses-compose-19e6cb1430ac?source=rss-c3986cb346ee------2</link>
            <guid isPermaLink="false">https://medium.com/p/19e6cb1430ac</guid>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ivan Starkov]]></dc:creator>
            <pubDate>Sun, 23 Apr 2017 15:20:01 GMT</pubDate>
            <atom:updated>2017-04-23T17:21:08.266Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://github.com/acdlite/recompose">Recompose</a> is the library I use now in every project of mine.</p><p>When I first saw recompose, I immediately had one question — why does a library use the compose function for enhancers composition?</p><p>All my previous knowledge about compose told me that evaluation order of composed functions will be unnatural, not the same as code flow.</p><p>As an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddbec52159442b780107bf6258f421e0/href">https://medium.com/media/ddbec52159442b780107bf6258f421e0/href</a></iframe><p>gives the result 4 not 6 because evaluation order is 1 * 2 then 2+ 2</p><p>Before writing an issue that usage of flow will be better, I created a fast check, something like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b27869b93b5c4ca70c71d4737580f37e/href">https://medium.com/media/b27869b93b5c4ca70c71d4737580f37e/href</a></iframe><p>And got the result on the screen equal to 6 not 4, so the order was natural even compose was used. It was strange and was not expected by me.</p><p>At first I decided that library author uses his own handwritten version of compose which is some kind of lodash flow. I was wrong he used lodash/function/compose.</p><p>I wrote what I see on the paper in simplified form.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67768480c58f2d430f1686641da5e40e/href">https://medium.com/media/67768480c58f2d430f1686641da5e40e/href</a></iframe><p>So I got the “right” natural order of operations (x + 2) and then *2, just because Recompose uses composition of enhancers and not the composition of property transformers.</p><p>That example somehow changed my mind and because of such “magic” of non magic things I love being a developer ;-).</p><p>Big thanks to <a href="https://github.com/antongb">Anton Shain</a> for looking over this document draft.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19e6cb1430ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>