<?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 Suryakant on Medium]]></title>
        <description><![CDATA[Stories by Suryakant on Medium]]></description>
        <link>https://medium.com/@suryakant_67440?source=rss-461fb5d11f3b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*m9fas6nJR5QoUP0u.</url>
            <title>Stories by Suryakant on Medium</title>
            <link>https://medium.com/@suryakant_67440?source=rss-461fb5d11f3b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 17:13:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suryakant_67440/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[React-Redux Patterns]]></title>
            <link>https://medium.com/@suryakant_67440/react-redux-patterns-627ae9e9ea52?source=rss-461fb5d11f3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/627ae9e9ea52</guid>
            <category><![CDATA[react-patterns]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Suryakant]]></dc:creator>
            <pubDate>Sat, 16 Dec 2017 15:49:49 GMT</pubDate>
            <atom:updated>2017-12-16T15:49:49.271Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Part 2- Components</strong></p><p>In this part we will try and generate a pattern to write a typical component to be used in our project. Basic assumption for a generalised component is that it requires to fetch data from api, and the data will reside in store, passed onto the component as props.</p><p>We are looking at a signature like following</p><pre>&lt;OurComponent fetchData={fetchData} data={data} {...otherProps} /&gt;</pre><p>As the component loads it should fetch data and render.</p><pre>class OurComponent extends Component {<br>    componentWillMount () {<br>      this.setState({loading: true})<br>      this.props.fetchData()<br>    }<br>    <br>    componentWillReceiveProps (nextProps) {<br>      if (!nextProps.data &amp;&amp; this.props.data) {<br>        this.setState({loading: true})<br>        this.props.fetchData()<br>      }<br>      if (nextProps.data &amp;&amp; nextProps.data !== this.props.data) {<br>        this.setState({loading: false})<br>      }<br>    }</pre><pre>    instanceProperty = &#39;something&#39;<br>    instanceMethod () {/*do Something*/}</pre><pre>    render () {<br>      if(!data) return {/*Empty state jsx*/}<br>      if(this.state.loading) return({/*loading state jsx */})<br>      return ({/*actual jsx*/})<br>    }<br>  }<br>}</pre><p>We avoid using constructor in our component as componentWillMount gives us certain advantages.</p><ol><li><em>this and this.setState is readily available</em></li><li>can be easily converted to an reusable component using <a href="https://github.com/acdlite/recompose/blob/master/docs/API.md#lifecycle">lifecycle-HOC</a>.</li></ol><p>Also instanceProperty and methods are by default bound in above examples.</p><p>If we need to trigger a reload of this mounted component we just need to nullify the corresponding data in store. This will trigger <em>componentWillReceiveProps</em> to fetch the data again.</p><p>Also the loading and empty states are easy to manage. Loading state is not lifted up or managed in store, as it causes proliferation of loading state for various component which might be loading in parallel.</p><p>Some time single api can fetch necessary data for multiple component. To prevent multiple call, if all the component are connected through single container the <em>fetchData </em>action can be throttled. Or, else we can keep a flag in our reducer to check if action has already been performed.</p><p>Our above component can be converted to HOC for multiple use.</p><pre>/*HOC.js*/<br>function <em>ourHOC (BaseComponent, /*EmptyState, LoadingState*/){<br>  </em>class OurComponent extends Component {<br>   .<br>   .<br>   .  <br>   render () {<br>     if(this.state.loading) return(&lt;LoadingState /&gt;)<br>     if(!data) return &lt;EmptyState /&gt;<br>     return (&lt;BaseComponent /&gt;)<br>   }<br>  }<br>  return OurComponent<br>}<br>export default ourHOC</pre><pre>/*FirstComponent.js*/<br>const FirstComponent = props =&gt; return (&lt;h1&gt;{props.data.first}&lt;/h1&gt;)<br>export default HOC(FirstComponent)</pre><pre>/*SecondComponent.js*/<br>const FirstComponent = props =&gt; return (&lt;h1&gt;{props.data.first}&lt;/h1&gt;)<br>export default HOC(SecondComponent)</pre><pre>/*index.js*/<br>const ContainerComponent = props =&gt; {<br>   return (<br>     &lt;&gt;<br>      &lt;FirstComponent fetchData={fetchFirstData} data={FirstData} /&gt;<br>      &lt;SecondComponent fetchData={FetchSecondData} data={SecondData} /&gt;)<br>}</pre><p>With react 16.0.0 you can return string or array as <a href="https://reactjs.org/docs/fragments.html">fragment</a> in components.</p><p>Another feature to use is <a href="https://reactjs.org/docs/error-boundaries.html">componentDidCatch</a>. Its need and use is clearly explained in the hyperlink.</p><p>The <em>otherProps </em>passed to the component will usually be methods and properties to interact with other component.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=627ae9e9ea52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React-Redux Patterns]]></title>
            <link>https://medium.com/@suryakant_67440/react-redux-patterns-a7bfdd38859b?source=rss-461fb5d11f3b------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7bfdd38859b</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-patterns]]></category>
            <category><![CDATA[reusable-component]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Suryakant]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 07:09:05 GMT</pubDate>
            <atom:updated>2017-12-11T07:09:05.750Z</atom:updated>
            <content:encoded><![CDATA[<h3>Part 1- Reusable Components</h3><p>Based on experiences, I wanted to list down certain pattern for review, refinement and reuse in a react-redux app.</p><p>As ReactJs(March’13) and ReduxJs (June’15) is still a fresh technology, it is still going major changes and qualified engineers are trying to create libraries and subroutines to solve recurrent problem. I would just list down patterns faced in my past project. This part is dedicated to developing reusable component.</p><p><strong>Functional vs Stateful — </strong>Creating reusable component in react-redux application. Though I prefer using controlled component and know the downside to it, react-redux ecosystem emphasises on keeping components functional. Also no-one is really definite on when to use functional component or class component. For presentational purposes, functional component serve the purpose. But some time keeping a internal state helps reducing complexity, as we avoid storing state information in store which need not be shared be any other component. It would be nice if the decision is left to the developer to decide what to use based on the context.</p><p><strong>Connected Component — </strong>Sharing a connected component is another option to create a reusable component. I have not seen many examples of such kind but there has been constant discussion about it</p><ul><li><a href="https://github.com/reactjs/redux/issues/1098">Using Redux in reusable React component · Issue #1098 · reactjs/redux</a></li><li><a href="https://github.com/reactjs/react-redux/issues/278">RFC: Reuse complex components implemented in React plus Redux · Issue #278 · reactjs/react-redux</a></li><li><a href="https://medium.com/@MattiaManzati/how-to-reuse-redux-components-8acd5b4d376a">How to reuse Redux components?</a></li></ul><p>Based on an article, I can’t locate any more I have created following example. Here in the following example we have a connected component in ‘<em>room.js’, </em>with its own reducer, action and container<em>. </em>All the action-creators are stored in action object.</p><pre>/* room.js*/<br>import {connect} from &#39;react-redux&#39;<br>import {throttle} from &#39;lodash&#39;<br>import RoomUtilization from &#39;./components/RoomUtilization&#39;<br>import reducer, {action} from &#39;./modules/reducer&#39;</pre><pre>function mapStateToProps (state) {<br>  return state.roomReducer<br>}<br>export {action, reducer}<br>export default connect(mapStateToProps, action)(RoomUtilization)</pre><pre>/*meeting.js*/<br>import room, {reducer, action} from &#39;../room&#39;</pre><p>Only thing to keep in mind is the key with which we connect the reducer to store. Here the room reducer is connected to store as ‘roomReducer’, so mapStateToProps return state.roomReducer.</p><p><strong>HOC —</strong> Other recurring patterns to be mentioned is HOC. HOC is now a well established pattern to share code in functional programming. Its a recommended pattern by react developer over inheritance. Lot of HOC examples are available, but probably best place to look for will be <a href="https://github.com/acdlite/recompose">recompose</a> library.</p><p>Also, here is something mentioned in <a href="https://github.com/Semantic-Org/Semantic-UI-React/blob/master/src/lib/AutoControlledComponent.js">semantic-ui repo</a></p><blockquote>/** * Why choose inheritance over a HOC? Multiple advantages for this particular use case. * In short, we need identical functionality to setState(), unless there is a prop defined * for the state key. Also: * * 1. Single Renders * Calling trySetState() in constructor(), componentWillMount(), or componentWillReceiveProps() * does not cause two renders. Consumers and tests do not have to wait two renders to get state. * See <a href="http://www.react.run/4kJFdKoxb/27">www.react.run/4kJFdKoxb/27</a> for an example of this issue. * * 2. Simple Testing * Using a HOC means you must either test the undecorated component or test through the decorator. * Testing the undecorated component means you must mock the decorator functionality. * Testing through the HOC means you can not simply shallow render your component. * * 3. Statics * HOC wrap instances, so statics are no longer accessible. They can be hoisted, but this is more * looping over properties and storing references. We rely heavily on statics for testing and sub * components. * * 4. Instance Methods * Some instance methods may be exposed to users via refs. Again, these are lost with HOC unless * hoisted and exposed by the HOC. */</blockquote><p><strong>Hanging DOM — </strong>Another issue with developing reusable component, is of hanging DOM node.</p><p>Lemme explain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*6zmKFHp6eTfls-qnOcPV5w.png" /></figure><p>This is a recurring component in my project with header containing title and filter button. The main body contains a chart. And clicking on filter button opens up corresponding filter in the sidebar. This component is developed as an HOC which takes title and chart as argument component. Also based on the store the filter keeps it fields populated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*sj3Msfh1tQhTBv42dm6-CQ.png" /></figure><p>This when you press the filter button. The problem is filter-button resides outside the filter in header div, which forces us to store some state information outside the reusable component, here the Filter.</p><p>I tried using <em>ReactDOM.createPortal</em> for inserting the Filter into an existing DOM node, but missed the smooth transitioning of closing and opening of Filters. Also the code looked too complicated for simple understanding when used to <em>createPortal</em></p><pre>const <strong>Filter</strong> = props =&gt; {<br>  let rootNode = document.getElementById(props.type)<br>  if(props.visible){<br>    rootNode.className += &#39; visible&#39;<br>    return ReactDOM.createPortal(props.children, document.getElementById(props.type))<br>  }<br>  rootNode.classList.remove(&#39;visible&#39;);<br>  return null<br>}</pre><pre>export default class FilterComponent extends Component {<br>  ....<br>  toggleVisibility = (e) =&gt; {<br>    return this.setState({ visible: !this.state.visible })<br>  }<br>  render () {<br>   .....<br>    return (<br>      &lt;Menu.Item className={visible ? &#39;refine hide&#39; : &#39;refine&#39;}&gt;<br>        &lt;Button className=&#39;btn-refine&#39; onClick={this.toggleVisibility} &gt;&lt;i className=&#39;jif-iris-refine&#39; /&gt;&lt;/Button&gt;<br>        &lt;<strong>Filter</strong> {...this.props} visible={visible}&gt;<br>          {/* .....Filter jsx */}<br>        &lt;/<strong>Filter</strong>&gt;<br>      &lt;/Menu.Item&gt;<br>    )<br>  }<br>}</pre><p>Here I am only rendering the button by default. On click of filter button <em>this.state.visible</em> becomes true and filter is rendered in the <em>rootNode. </em>Only problem with this code is lack of smooth transitioning. Again this is WIP and I haven’t found a clear solution.</p><p><strong>Logic — </strong>One thing to keep in mind while developing any component is to restrict the data manipulation/business logic to reducers. Its has been difficult for me to separate out presentational logic with business logic. But as a rule of thumb you should keep most of your logic in reducer and let the component be as functional and light as possible.</p><p>Also try not to define a function inside a function. Independent functions will help you scale and refractor you code smoothly in later phases.</p><p>In the next part we will try to explore some recurring patters in <strong>reducers, actions </strong>and<strong> tests.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7bfdd38859b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>