<?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: samira saad</title>
    <description>The latest articles on DEV Community by samira saad (@samirasaad).</description>
    <link>https://dev.to/samirasaad</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%2F367171%2Fbfb6430a-074a-4abc-8379-df90cfed92a5.jpeg</url>
      <title>DEV Community: samira saad</title>
      <link>https://dev.to/samirasaad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samirasaad"/>
    <language>en</language>
    <item>
      <title>Class Component with React Typescript </title>
      <dc:creator>samira saad</dc:creator>
      <pubDate>Sun, 06 Mar 2022 14:45:46 +0000</pubDate>
      <link>https://dev.to/samirasaad/class-component-with-react-typescript-3948</link>
      <guid>https://dev.to/samirasaad/class-component-with-react-typescript-3948</guid>
      <description>&lt;h2&gt;
  
  
  What is a Component?
&lt;/h2&gt;

&lt;p&gt;A component is an independent, reusable code block which divides the UI into smaller pieces.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Class Components?
&lt;/h2&gt;

&lt;p&gt;Class components are ES6 classes that return JSX&lt;/p&gt;




&lt;p&gt;Using TypeScript together with React has proven to be a powerful combination.&lt;/p&gt;

&lt;p&gt;The “older way” of doing components is with class components. And they can keep state per class. State is like props, but private and only controlled by the component itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create class component using TypeScript
&lt;/h2&gt;

&lt;p&gt;Class components need to be extended from the base React.Component class.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import React, { Component } from 'react'; &lt;br&gt;
&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { Component } from 'react';

type CounterProps = {
  header: string;
};

type CounterState = {
  value: number;
};

class Counter extends React.Component&amp;lt;CounterProps, CounterState&amp;gt; {

  state: CounterState = {
    value: 0,
  };

  render() {
    const { header } = this.props;
    const { value } = this.state;

    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1&amp;gt;{header}&amp;lt;/h1&amp;gt;
        &amp;lt;p&amp;gt;{value}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;1-&lt;/strong&gt; The CounterProps type is used to declare what type the properties of this component support.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2-&lt;/strong&gt; This is similar to declaring types with prop-types but without any additional packages and the result of type checking is available at design time. You won't need to run the project to figure out that some types are not correctly passed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3-&lt;/strong&gt; Class properties are useful for storing some data that should not affect re-render of a component. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In&lt;br&gt;
 the example below, it is a helloMessage that changes when a component is mounted. Its update won't trigger re-rendering.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, {Component} from "react";

type CounterProps = {
  value: number;
};

class Counter extends Component&amp;lt;CounterProps&amp;gt; {

   // It won't be possible to assign a value 
  // with a different type to this property.

  helloMessage: string;

  componentDidMount() {
    helloMessage = "Hi! I'm mounted.";
  }

  render() {
    return &amp;lt;p&amp;gt;{this.props.value}&amp;lt;/p&amp;gt;;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  pass a React component down as a prop to a child component
&lt;/h2&gt;

&lt;p&gt;Within TypeScript, React.Component is a generic type (React.Component), so you want to provide it with (optional) prop and state type parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Test extends Component&amp;lt;PropsType,StateType&amp;gt; {
  constructor(props : PropsType){
        super(props)
  }

  render(){
    console.log(this.props) 
    return (
        &amp;lt;p&amp;gt;this.props.whatever&amp;lt;/p&amp;gt; 
    )
  }

};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;And here how to get props of component&lt;br&gt;
*&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type ViewProps = React.ComponentProps&amp;lt;typeof View&amp;gt;
// or
type InputProps = React.ComponentProps&amp;lt;'input'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>typescript</category>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>Environment files in  React.js  app </title>
      <dc:creator>samira saad</dc:creator>
      <pubDate>Fri, 12 Feb 2021 19:34:24 +0000</pubDate>
      <link>https://dev.to/samirasaad/environment-variables-for-a-react-js-app-329j</link>
      <guid>https://dev.to/samirasaad/environment-variables-for-a-react-js-app-329j</guid>
      <description>&lt;h1&gt;
  
  
  What is an environment file ?
&lt;/h1&gt;

&lt;p&gt;environment file or just &lt;strong&gt;env&lt;/strong&gt; is a file holds variables and some sensitive data about your app  &lt;/p&gt;

&lt;h1&gt;
  
  
  Why we need env files in our app?
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;1-&lt;/strong&gt; according to create react app &lt;a href="https://create-react-app.dev/docs/adding-custom-environment-variables/"&gt;documentation&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;WARNING: Do not store any secrets (such as private API keys) in your React app!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;avoid storing any sensitive data in your js files, instead use env files to store them&lt;/p&gt;

&lt;p&gt;sensitive data mean any data you shouldn't share with any one such as &lt;strong&gt;api keys&lt;/strong&gt;, &lt;strong&gt;secret-ids&lt;/strong&gt;, &lt;strong&gt;firebase config keys&lt;/strong&gt;,etc.... &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2-&lt;/strong&gt; To declare different variables for each environment &lt;/p&gt;

&lt;p&gt;variables such as &lt;strong&gt;API url’s&lt;/strong&gt;, &lt;br&gt;
env files allow defining the values depending on the environment&lt;/p&gt;
&lt;h1&gt;
  
  
  How to setup env files inside react app ?
&lt;/h1&gt;

&lt;p&gt;so, how can we define the values depending on the environment&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: this feature is available with &lt;a href="mailto:react-scripts@0.2.3"&gt;react-scripts@0.2.3&lt;/a&gt; and higher.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;1- install env-cmd &lt;a href="https://www.npmjs.com/package/env-cmd"&gt;package&lt;/a&gt; from npm &lt;/p&gt;

&lt;p&gt;2- make a file called &lt;strong&gt;.env.envName&lt;/strong&gt; in your project root&lt;br&gt;
sush as .env.staging, .env.production, ... to differentiate between variables in each environment&lt;/p&gt;

&lt;p&gt;3- inside the env file add your variables in key/value representation with prefix of &lt;strong&gt;REACT_APP&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;EX:&lt;/strong&gt;&lt;br&gt;
REACT_APP_BASE_URL = "https://....."&lt;/p&gt;

&lt;p&gt;your file should look like this after adding your variables&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REACT_APP_API_KEY = "****"
REACT_APP_AUTHDOMAIN =  "****"
REACT_APP_BASEURL = "****"
REACT_APP_PROJECT_ID = "****"
REACT_APP_STORAGEBUCKET = "****"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;make a file with the previous 3 steps for each environment and each file with its environment values&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;4- inside your package.json &lt;br&gt;
change the scripts builds ,..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "start": "env-cmd -f .env.staging react-scripts start",
    "build:staging": "env-cmd -f .env.staging react-scripts build",
    "build:production": "env-cmd -f .env.production react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;-f flag is for custom env file paths as the default is in the project root otherwise you should specify the actual path
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"start": "env-cmd -f ../../.env.staging react-scripts start",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;your build command in each environment is not &lt;strong&gt;npm run build&lt;/strong&gt; any more
its &lt;strong&gt;npm run build:staging&lt;/strong&gt; , &lt;strong&gt;npm run build:production&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How to read variables values in js files ?
&lt;/h1&gt;

&lt;p&gt;to use a value of a particular variable located in env file inside a js file all u need to do is to use the global &lt;strong&gt;process.env.variableName&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;Ex:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(process.env.REACT_APP_API_KEY)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important Notes&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;1-&lt;/strong&gt; Dont forget to add your all env files to git-ignore file to prevent tracking them after any modification&lt;br&gt;
&lt;strong&gt;2-&lt;/strong&gt; After each time u modify in env file stop the project serve and start it over again, otherwise its wont listen to your new changes&lt;br&gt;
&lt;strong&gt;3-&lt;/strong&gt; stick to your company/team for env files naming convention &lt;br&gt;
as not all deployments processes accepts the .env.envName convention&lt;br&gt;
for example vercel doesn't accept '.' in the env file name at all&lt;/p&gt;

&lt;h1&gt;
  
  
  References
&lt;/h1&gt;

&lt;p&gt;1- &lt;a href="https://medium.com/swlh/keeping-env-variables-private-in-react-app-fa44a9b33c31"&gt;https://medium.com/swlh/keeping-env-variables-private-in-react-app-fa44a9b33c31&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2- &lt;a href="https://create-react-app.dev/docs/adding-custom-environment-variables/"&gt;https://create-react-app.dev/docs/adding-custom-environment-variables/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>docker</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
