<?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 Jordan Trickett on Medium]]></title>
        <description><![CDATA[Stories by Jordan Trickett on Medium]]></description>
        <link>https://medium.com/@jordan1trickett?source=rss-371915659605------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*1EqquyM2rwx8pZFV0kfnRw.png</url>
            <title>Stories by Jordan Trickett on Medium</title>
            <link>https://medium.com/@jordan1trickett?source=rss-371915659605------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 14:02:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jordan1trickett/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[Daily Discovery: Using Environment Variables for API Endpoint Refactor (React.js)]]></title>
            <link>https://medium.com/@jordan1trickett/daily-discovery-using-environment-variables-for-api-endpoint-refactor-react-js-a6772800987e?source=rss-371915659605------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6772800987e</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[ajax]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Jordan Trickett]]></dc:creator>
            <pubDate>Mon, 30 Mar 2020 09:11:34 GMT</pubDate>
            <atom:updated>2020-03-30T09:11:34.493Z</atom:updated>
            <content:encoded><![CDATA[<p>Refactor AJAX requests so one variable can handle both development and production API endpoints.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cZ8l6zTznQz0kxNfh-K04A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@blakeconnally?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Blake Connally</a> on <a href="https://unsplash.com/s/photos/coding?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><blockquote>Hello world! I’ve just graduated General Assembly’s Software Engineering Immersive and really inspired by the new community I’ve entered. On my way to and from campus, I spent a lot of time reading Medium articles. They were all super helpful and I wanted to try and give something back to the community.</blockquote><blockquote>I’m a newbie coder and definitely not a writer. So I hope this is helpful and any feedback is appreciated.</blockquote><p>I decided to write this article because I was really excited by how effective the result was at drying up my code. It also took a fair bit of googling to piece together the process. So I hope this article can be a bit more direct to this specific goal and help anyone in a similar position.</p><h4>Goal of the process</h4><p>As mentioned in the subheading the goal of this process is to have one variable that can handle all AJAX requests, whether running on the local server or deployed to production.</p><blockquote>In this example I will be using axios to handle the AJAX requests. The process will still be the same using another library.</blockquote><h4>Previous solution</h4><p>Previously to achieve this I would use a conditional statement, that evaluated the NODE_ENV variable and set a value for URL based on the result.</p><blockquote>NODE_ENV is a built-in environment variable to node.js. I will go into a bit more detail about this in the background section.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wHvurUZr-ghI8DsHM1G59Q.png" /><figcaption>previous solution with conditional</figcaption></figure><p>The problem with this is, every AJAX requires the same conditional. Making your code very repetitive, unclear at first glance and not massively but ever so slightly less efficient.</p><p>Every time I did this I knew there must be a better way. If NODE_ENV knows which environment the app is running in, then surely I can use an environment variable to achieve this.</p><h4>Background</h4><p>As mentioned above NODE_ENV is a built-in environment variable of node.js. This variable as seen above is accessed through process.env.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Ogf2DQq_t2jTbd9WMmhYGQ.png" /><figcaption>access NODE_ENV value</figcaption></figure><p>When you run npm start, NODE_ENV is always equal to ‘development’, when you run npm test it is always equal to ‘test’, and when you run npm run build to make a production bundle, it is always equal to ‘production’. This is how the above conditional works. This same logic is also how we can achieve our goal.</p><p>We can define multiple environment files (.env files), which can then be used to store environment-specific data. .env files should be created in the root directory of your project.</p><p>The possible .env files, the environment they relate to and how this is run is as follows;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MyHsxP-LQwvSD2MkRes2-Q.png" /><figcaption>possible environment files</figcaption></figure><blockquote>Note: Files on the left have more priority than files on the right</blockquote><p>This process relies on the react-scripts package to load the correct .env file based on the environment that the app is loaded in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SRdUCpOG4IsyKR-_UGl21A.png" /><figcaption>react scripts</figcaption></figure><blockquote>Note: this feature is available with react-scripts@0.2.3 and higher.</blockquote><p>Now using the logic above lets put this into practice.</p><h4>Process</h4><p>To define the environment-specific variables, we first need to create the .env files for our environments in the root directory of our project. I am only doing development and production on this project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*B8iYVByP2WOMiwRO_3IptQ.png" /><figcaption>define the environment files</figcaption></figure><p>Now to create an environment variable with the environment-specific endpoint as the value. The syntax of how you define this variable is important. They must always begin with REACT_APP.</p><blockquote>As the goal of this process is to have one variable that can be used for both environments I will be using the same definition in both files — REACT_APP_API</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*CIgHQIU3Y1mIy99zEyxd2g.png" /><figcaption>development variable</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*YmDdXaw5_QqgBF8K44dcqw.png" /><figcaption>production variable</figcaption></figure><blockquote>Note: You need to restart the development server after changing .env files.</blockquote><p>These environment variables will then be defined for you on process.env, just like the NODE_ENV.</p><p>So the only thing left to do now is reference this variable when you make your AJAX requests.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I91_XlzClXKZnTqEM4aHAg.png" /><figcaption>ajax request with an environment variable</figcaption></figure><p>Thanks for reading. If anyone has any more information on the topic or feedback please comment. I hope this was helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6772800987e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Daily Discovery: Default Props (React.js)]]></title>
            <link>https://medium.com/@jordan1trickett/daily-discovery-default-props-react-js-a15892ecb3b9?source=rss-371915659605------2</link>
            <guid isPermaLink="false">https://medium.com/p/a15892ecb3b9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[first-post]]></category>
            <dc:creator><![CDATA[Jordan Trickett]]></dc:creator>
            <pubDate>Fri, 27 Mar 2020 08:48:25 GMT</pubDate>
            <atom:updated>2020-03-27T09:30:09.425Z</atom:updated>
            <content:encoded><![CDATA[<h3>Daily Discovery: defaultProps (React.js)</h3><p>How to render a functional component with a prop or a default fallback value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S_Hy7DI9iH8EUiRpaVgVxw.jpeg" /></figure><blockquote><em>Hello world! This is my first time here and I’m just as excited as I am apprehensive. I’ve just graduated General Assembly’s Software Engineering Immersive and really inspired by the new community I’ve entered. On my way to and from campus, I spent a lot of time reading Medium articles. They were all super helpful and I wanted to try and give something back to the community.</em></blockquote><blockquote><em>I’m a newbie coder and definitely not a writer. So I hope this is helpful and any feedback is appreciated.</em></blockquote><h3>Components</h3><p>A central premise of React is components. Components allow us to break parts of a web app down into small, modular pieces. That are simpler to manage, re-use and combine. This becomes extremely useful in large scale complex sites.</p><p>Each component can be rendered with its own content, presentation, data and behavior. How the component is rendered depends on the state defined by that component and/or the props passed down to it from the parent component.</p><h3>Props</h3><p>Properties. Are the way child components receive data from parent components and allow for different instances of the same component to be rendered.</p><p>Below is the syntax for passing a prop called message to a functional component called Spinner.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*n_zf27_49pZhErd203HOHA.png" /><figcaption>Passing a prop to a component</figcaption></figure><p>Accessed in the component through props object and with the defined key name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/1*ofjUhUWaHaah4fJmpdSdXw.png" /><figcaption>Accessing a prop</figcaption></figure><p>Now what if we decide to use this spinner component somewhere else in our app that doesn’t require a custom message</p><h3>|| Operator</h3><p>Previously to achieve this I would use the logical operator || as below. This follows the same principle as vanilla javascript, but with regards to rendering, the first value of the || operator to evaluate to true, is returned and therefore rendered.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*Q1W2WIeiAdTCz9-byHJhGQ.png" /><figcaption>Rendering with the || operator</figcaption></figure><p>But as you can probably imagine as our components get more complex this process could make our code harder to follow.</p><h3>Enter Default Props</h3><blockquote>In this example, I am creating a basic spinner function that takes a message as a prop to display or if no prop is provided it will use the default fallback value.</blockquote><p>This process makes your code more elegant and components more flexible. Also, minimising the risk of breaking some code.</p><p>Below the functional component, we add the static property defaultProps to the component and define the object values. The syntax for this is important.</p><ul><li>The object name must match the component name</li><li>Followed by defaultProps</li><li>Then the key of the object must be the same as the prop referenced in the JSX.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*bPQdMoa7xDHp-DL198JhdQ.png" /><figcaption>Defining default props</figcaption></figure><p>Inside the component, we reference the prop object where you would like the value to appear as you would normally. Notice the|| operator has not been used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*whBQ9p5v4sn7THIkPCjcBQ.png" /></figure><p>Now whether we render the component with or without a message prop we will always display a value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*q0w3MlqopXZMInL_iFf7tg.png" /><figcaption>Render component without prop</figcaption></figure><h3>The Result</h3><p>With a message prop</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/758/1*d16hxPKg4u1Qx4xvefrkzQ.png" /></figure><p>Without a message prop and utilising the default prop</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*rRF7qoa3tF0Qi61qYvs-lA.png" /></figure><blockquote>Thanks so much for reading. Please let me know if this was helpful or more importantly if I messed anything up so I can continue to learn.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a15892ecb3b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>