<?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 Chaitanya Mankala on Medium]]></title>
        <description><![CDATA[Stories by Chaitanya Mankala on Medium]]></description>
        <link>https://medium.com/@chaitanyamankala?source=rss-5b24e9f4be81------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*j8IhnZZodiw-9jyhKSLm1w.jpeg</url>
            <title>Stories by Chaitanya Mankala on Medium</title>
            <link>https://medium.com/@chaitanyamankala?source=rss-5b24e9f4be81------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:55:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chaitanyamankala/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[Managing Environment Variables for Ionic 2/3 Projects]]></title>
            <link>https://medium.com/@chaitanyamankala/adding-environment-for-ionic-2-3-projects-45ae9d50dcda?source=rss-5b24e9f4be81------2</link>
            <guid isPermaLink="false">https://medium.com/p/45ae9d50dcda</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[ionic]]></category>
            <category><![CDATA[ionic-framework]]></category>
            <category><![CDATA[angular-4]]></category>
            <category><![CDATA[angular2]]></category>
            <dc:creator><![CDATA[Chaitanya Mankala]]></dc:creator>
            <pubDate>Thu, 22 Feb 2018 18:21:08 GMT</pubDate>
            <atom:updated>2019-04-29T13:49:07.396Z</atom:updated>
            <content:encoded><![CDATA[<p>Developing node based express app? You got dotenv, .env etc. Developing an Angular app? ng CLI supports env vars. But, Using Ionic CLI? This article’s for you!</p><p>For an Ionic Project, you may have different Backend URL (local, production etc), or some other config vars based on prod or dev builds. There’s no simple way provided by ionic CLI so that we can pass some flags to change environment. But, nothing is impossible! Here’s my way to do it, my CTO calls it <em>ultra-jugaad</em> (in hindi), hope it helps :P</p><h3><strong>STEP #1 — cross-env</strong></h3><p>install this node module <em>globally</em>,</p><pre>npm i cross-env -g</pre><h3>STEP #2 — The Environments</h3><p>Create these files for setting environment vars, in my case there are two environments prod and dev. So, I have three files in a folder</p><blockquote>src/environments/environment.ts</blockquote><pre>export var environment = {   //THIS IS FOR DEFAULT VALUES</pre><pre>     BASE_URL:’http://localhost:3000/api’,<br>     someVar:&#39;someVal&#39;<br>     ....</pre><pre>}</pre><blockquote>src/environments/environment.prod.ts</blockquote><pre>export var environment = {   //THIS IS FOR PROD VALUES</pre><pre>BASE_URL:’https://prod.server.com/api’,<br>     someProdVar:&#39;someProdVal&#39;<br>     ....</pre><pre>}</pre><blockquote>src/environments/environment.dev.ts</blockquote><pre>export var environment = {   //THIS IS FOR DEV VALUES</pre><pre>BASE_URL:’http://localhost:3000/api’,<br>     someDevVar:&#39;someDevVal&#39;<br>     ....</pre><pre>}</pre><h3>STEP #3 — Use the Environment Values</h3><p>As environment.ts is being exported, you can import in any injectable/component where the value is used. (<strong>Import only environment.ts</strong>, not other files)</p><blockquote>Example</blockquote><pre>import { environment as ENV } from ‘src/environments/environment’ ;</pre><pre>export class SomeLaLaClass{<br>   constructor(){<br>      console.log(ENV.BASE_URL); //prints base url value from env<br>   }<br>}</pre><h3>STEP #4 — The Script</h3><p>create the below script in ./bin/www</p><blockquote>script.js</blockquote><pre>var fs = <strong>require</strong>(&#39;fs&#39;);</pre><pre>function <strong>readWriteSync</strong>() {</pre><pre>let env = process.env.ENV;</pre><pre>if (!process.env.ENV) {</pre><pre>          env = &#39;dev&#39;;  <strong>//SETTING DEFAULT ENV AS DEV</strong></pre><pre>}</pre><pre>var data = fs.<strong>readFileSync</strong>(`src/environments/environment.${env}.ts`, &#39;utf-8&#39;);</pre><pre>fs.<strong>writeFileSync</strong>(&#39;src/environments/environment.ts&#39;, data, &#39;utf-8&#39;);</pre><pre><strong>console</strong>.<strong>log</strong>(&#39;readFileSync complete&#39;);</pre><pre>}</pre><pre><strong>readWriteSync</strong>();</pre><h3>STEP #5 — The Hooks</h3><p>The <strong>only</strong> thing that made this whole process possible is the Ionic CLI build hooks. These can be added in the <strong>package.json</strong></p><blockquote>package.json</blockquote><pre>{</pre><pre>“name”: “My Awesome App”,</pre><pre>“description”: “Its awesome and you know it!”,</pre><pre>“version”: “0.0.1”,</pre><pre>“author”: “The Mankala”,</pre><pre>“homepage”: “http://mankala.com/&quot;,</pre><pre>“scripts”: {</pre><pre>“clean”: “ionic-app-scripts clean”,</pre><pre>“build”: “ionic-app-scripts build”,</pre><pre>“lint”: “ionic-app-scripts lint”,</pre><pre>“ionic:build”: “node ./src/bin/script.js ionic-app-scripts build”,</pre><pre>“ionic:serve”: “ionic-app-scripts serve”,</pre><pre><strong>“ionic:watch:before”</strong>: “node ./src/bin/<strong>script</strong>.js”,</pre><pre><strong>“ionic:build:before”</strong>: “node ./src/bin/<strong>script</strong>.js”</pre><pre>},<br>dependencies:{<br>      ...<br>      ...</pre><pre>}</pre><blockquote>ionic:watch:before is a hook called before ionic serve<br> (<strong>ionic:serve:before</strong> for <strong>Ionic 4</strong> and later)</blockquote><blockquote>ionic:build:before is a hook called before ionic build</blockquote><p>The script is being called before build/serving the project, so that the env is copied<strong> from env.prod </strong>or<strong> env.dev to env</strong></p><h3>STEP #6 — The Execution</h3><p>The hooks are being used to call the script before serve/build, but We need to tell the CLI about the env that has to be copied, for that, the <em>Step #1</em> is used!</p><blockquote>For serving, you can go to terminal and type, (Dev Environment in example)</blockquote><pre>cross-env ENV=’dev’ ionic serve</pre><blockquote>For building , you can go to terminal and type, (Prod Environment and Android platform in example)</blockquote><pre>cross-env ENV=’prod’ ionic cordova build android </pre><p>That’s it, Until Ionic CLI introduces env feature, this works well!<br>Hope it helps!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=45ae9d50dcda" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Tips to Code the Legendary Re…wait for it… act! React !!]]></title>
            <link>https://medium.com/@chaitanyamankala/tips-to-code-the-legendary-re-wait-for-it-act-react-f0ce3ec9a75d?source=rss-5b24e9f4be81------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0ce3ec9a75d</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[tips]]></category>
            <dc:creator><![CDATA[Chaitanya Mankala]]></dc:creator>
            <pubDate>Wed, 08 Nov 2017 19:00:58 GMT</pubDate>
            <atom:updated>2017-11-08T19:00:58.420Z</atom:updated>
            <content:encoded><![CDATA[<p>When I started learning react, looking at all the docs and stuff. It scared the <em>flux </em>out of me! :P But, once I understood what the <em>flux </em>it is, It was no more scary! This story is for the learning react devs out there, like me! Here we go,</p><h3>1. The Life Cycle</h3><p>In any MVC framework based on components, the first thing to be learned is the life cycle of a Component.</p><blockquote>Tip #1 Keep the constructor() simple</blockquote><p>As you know, constructor() is the first dude who runs in a component. This dude is also responsible for triggering the view render. If the view is rendered without any delay, it seems fast. So, use constructor only for var declarations in state or importing props etc. The things which are intended to run as soon as component is mounted, can be placed in componentDidMount()</p><blockquote>Tip #2 shouldComponentREALLYupdate()?</blockquote><p>Sometimes, the component need not re-render on receiving certain props. shouldComponentUpdate() returns <em>true </em>by default. But, we can customize the function in the component and make this <em>guy</em> <strong>not</strong> to return true always.</p><p>Eg:</p><pre>shouldComponentUpdate(nextProp){<br>   if(nextProp == &#39;Not the guy deserving to update the comp&#39;){<br>        return false;<br>   }<br>   return true;<br>}</pre><h3>2. Input binding with state</h3><p>As we know, Input binding with state can be done by running setState() triggered by <em>onChange prop</em> of the input JSX component. According to React docs, they suggest to have an onChange-function for each input (<em>separately</em>).</p><p>From the docs,</p><p>&lt;input onChange={this.handleWhenDudeChanges.bind(this)} value={this.state.dude}/&gt;</p><p>and</p><pre>handleWhenDudeChanges(event){<br>   this.setState({<br>       <strong>dude</strong>:e.target.value<br>   });<br>}</pre><p>But, here’s the tip!</p><blockquote>Tip #3 One generic onChange-function — <em>name </em>prop to the rescue</blockquote><p>Use <em>name </em>prop from the input function and have a generic onChange-function for all input comps.</p><p>like this!</p><pre>&lt;input value={this.state.dudeOne} name=”dudeOne” onChange={this.handleAllDudes.bind(this)}/&gt;<br>&lt;input value={this.state.dudeTwo} name=”dudeTwo” onChange={this.handleAllDudes.bind(this)}/&gt;</pre><p>and…</p><pre>handleAllDudes(event){<br>   this.setState({<br>           <strong>[e.target.name]</strong>:e.target.value<br>    });<br>}</pre><h3>3. ‘This’ Inside Promise</h3><p>‘this’ loses its scope inside the callback function of a promise, when used like <em>this :P ,</em></p><pre>someHonestPromise<br>.then(function(dudeee){<br>     console.log(this.state.someVar);  //--&gt; here &#39;this&#39; is lost<br>   })</pre><blockquote>Tip #4 Use Arrow functions to save the scope</blockquote><p>Arrow functions bind the inner function callbacks to the component scope, so that <em>this</em> is no more <em>undefined.</em></p><p>for example., the above promise can be rewritten as</p><pre>someSuperHonestPromise<br> .then((dude)=&gt;{<br>       console.log(this.state.someVar); //--&gt; Here &#39;this&#39;makes sense<br>})</pre><h3>4. Dependency Injection, Bro!</h3><p>If you are an angular dev, DI is the <em>love. </em>There is nothing officially given by react for DI, but you can make your own DI. Its <strong>ES6 </strong>baby!</p><blockquote>Tip #5 Inject services into components</blockquote><p>Injection is nothing but importing the usually used functions or generic functions from a separate service file.</p><p>For <strong>example</strong>, axios is an AJAX library used for react. We can create a service (DI) which makes all required AJAX calls in the app. These functions(from service) are triggered from components whenever required.</p><p>Coming to the code,</p><p><strong>app/services/axiosService.js</strong></p><pre>import axios from &#39;axios&#39;;</pre><pre>var axiosService = {<br>   getMe,<br>   postMe<br>};</pre><pre>function getMe(){....}<br>function postMe(){...}</pre><pre>export default axiosService;</pre><p>app/components/someAwesomeComponent</p><pre>import axiosService from &#39;../services/axiosService&#39;;<br>....</pre><pre>export default class someAwesomeComponent extends React.component{<br>  ....<br>  axiosService.getMe(); &lt;---- getMe() func is used here from service <br>....<br>}</pre><h3>5. Action + Reducer →REACTion!</h3><p>React has a single source of truth in entire app for any info. Which is state of the app. Actions are simple js objects to say that something happened, reducers are the functions which react to change the app state accordingly.</p><blockquote>Tip #6 Action-Reducer for storing global vars in App</blockquote><p>For example, after logging in the user data (from localstorage)can be sent to state like this,</p><p>userAction.js</p><pre>export const USER_INFO=’USER_INFO’; </pre><pre>export function getUserInfo(info) { <br>     return dispatch =&gt; { <br>            return dispatch({ type: USER_INFO, info}<br>     )}<br>}</pre><p>userReducer.js</p><pre>import { USER_INFO} from ‘./userAction’</pre><pre>export default function userReducer(state = { info: {}}, action){<br>  switch (action.type) { <br>           case USER_INFO: <br>                 return { info: action.info }; <br>           default: <br>                 return state <br>   } <br>}</pre><p>login.js (after logging in and getting user details)</p><pre>store.dispatch(userReducer(info)); //this copies info to app state</pre><p>Now, the user info is in the app state! YOLO! But the question is, how to use the data which is in app state? Here comes, <em>connect </em>from react-redux.</p><p>It goes like…</p><pre>import {connect} from ‘react-redux’;</pre><pre>...</pre><pre>class SomeCompNeedsUserInfo extends React.Component{</pre><pre>  ... //this.props.user_info is mapped from <strong>state&#39;s user_info</strong><br>}</pre><pre>mapStateToProps = (state) =&gt; {state.user_info}</pre><pre>export default conenct (mapStateToProps) (SomeCompNeedsUserInfo)</pre><h3>Conclusion</h3><p>This article is to help beginner and learner react devs to practice right programming.</p><p>Have a legendary coding :P</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0ce3ec9a75d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>