<?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: amir nazari</title>
    <description>The latest articles on DEV Community by amir nazari (@anazari96).</description>
    <link>https://dev.to/anazari96</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%2F779823%2F6a710f61-f719-48f4-9d34-e10dcd80e7a5.jpeg</url>
      <title>DEV Community: amir nazari</title>
      <link>https://dev.to/anazari96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anazari96"/>
    <language>en</language>
    <item>
      <title>How to create an ERC20 token from zero to hero?</title>
      <dc:creator>amir nazari</dc:creator>
      <pubDate>Mon, 31 Jan 2022 21:56:10 +0000</pubDate>
      <link>https://dev.to/anazari96/how-to-create-an-erc20-token-from-zero-to-hero-28o9</link>
      <guid>https://dev.to/anazari96/how-to-create-an-erc20-token-from-zero-to-hero-28o9</guid>
      <description>&lt;h2&gt;
  
  
  What is a smart contract?
&lt;/h2&gt;

&lt;p&gt;A smart contract is like a regular contract with rules and conditions between buyer and seller. Still, the difference is in a smart contract, these terms of the agreement are written with executable code on the machines, and then they are running on a distributed system.&lt;/p&gt;

&lt;p&gt;One of the most popular smart contract networks is Ethereum, and its programming language is Solidity. You can write your smart contract with this powerful language and publish it on the Ethereum blockchain network as a &lt;strong&gt;Token&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://soliditylang.org/"&gt;Solidity&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Token?
&lt;/h2&gt;

&lt;p&gt;A token is an interchangeable item with many different properties and behavior. Tokens are used in many different ways, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reputation points in an online platform&lt;/li&gt;
&lt;li&gt;an online piece of art&lt;/li&gt;
&lt;li&gt;skills of a character in a game&lt;/li&gt;
&lt;li&gt;lottery tickets&lt;/li&gt;
&lt;li&gt;voting&lt;/li&gt;
&lt;li&gt;financial assets like a share in a company&lt;/li&gt;
&lt;li&gt;a fiat currency like USD&lt;/li&gt;
&lt;li&gt;an ounce of gold&lt;/li&gt;
&lt;li&gt;and so more …&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This powerful feature of Ethereum should have standard cause these tokens wanted to speak to each other with an API; this is where &lt;strong&gt;ERC-20&lt;/strong&gt; plays its role.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is ERC-20?
&lt;/h2&gt;

&lt;p&gt;As we found here, ERC-20 is a standard for writing tokens with smart contract programming language. Now the question is, what is this standard?&lt;br&gt;&lt;br&gt;
In this standard, we need to implement some predetermined events and methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function name() public view returns (string)
function symbol() public view returns (string)
function decimals() public view returns (uint8)
function totalSupply() public view returns (uint256)
function balanceOf(address _owner) public view returns (uint256 balance)
function transfer(address _to, uint256 _value) public returns (bool success)
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
function approve(address _spender, uint256 _value) public returns (bool success)
function allowance(address _owner, address _spender) public view returns (uint256 remaining)

event Transfer(address indexed _from, address indexed _to, uint256 _value)
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function name:&lt;/strong&gt; Will return the token's name defined in the project.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function symbol:&lt;/strong&gt; Will return the sign of the token.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function decimals:&lt;/strong&gt; This is defined to show how many decimals this token can have.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function totlaSupply:&lt;/strong&gt; The total supply of the token in the network.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function balanceOf:&lt;/strong&gt; Return the balance of the given wallet address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function transfer:&lt;/strong&gt; Transfer the given value to the provided wallet address. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function transferFrom:&lt;/strong&gt; Transfer given value from the given sender wallet address to the receiver wallet address. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function approve:&lt;/strong&gt; which will check if the total supply has the amount of token which needs to be allocated to a user.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Function allowance:&lt;/strong&gt; which will check if the sender has enough supply to send to the receiver.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Transfer:&lt;/strong&gt; This event is published when the token is transferred from the sender to the receiver.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Approval:&lt;/strong&gt; This event is published when the spender approves that can send the given value to the receiver. &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The Wallet (MetaMask)
&lt;/h2&gt;

&lt;p&gt;To work with the ETH network and develop on top of that, you need a wallet address. The simplest way to set up our wallet and use it in our journey is to use &lt;a href="https://metamask.io/"&gt;MetaMask&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://metamask.io/"&gt;metamask&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Owing a MetaMask wallet is very easy, and you can do it in a couple of minutes. First, you need to download the MetaMask browser extension and then follow it to finish the process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Be careful; if this account of MetaMask is your one, you should save the credentials somewhere safe and don’t forget it!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Test Environments (Ropsten Test Network)
&lt;/h2&gt;

&lt;p&gt;For beginning the development of the token, you need a place for the test environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.anyblockanalytics.com/networks/ethereum/ropsten/"&gt;Ropsten&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the MetaMask browser extension, you can find a dropdown at the top of it and should be written “Ethereum Mainnet.” Click on that and change it to the Ropsten Test Network.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hJPCPV9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-25-at-7.24.29-AM.png%3Flossy%3D0%26strip%3D1%26webp%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hJPCPV9R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-25-at-7.24.29-AM.png%3Flossy%3D0%26strip%3D1%26webp%3D1" alt="MetaMask screenshot 1" width="359" height="601"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;MetaMask wallet&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wru6Shaj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-25-at-7.24.45-AM.png%3Flossy%3D0%26strip%3D1%26webp%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wru6Shaj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-25-at-7.24.45-AM.png%3Flossy%3D0%26strip%3D1%26webp%3D1" alt="MetaMask screenshot 2" width="359" height="601"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;MetaMask change network&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Using the &lt;a href="https://faucet.ropsten.be/"&gt;Ropsten faucet&lt;/a&gt; you can earn some test ETH for testing purposes. You need to change the network on your MetaMask wallet to Ropsten Test Network and copy-paste your wallet address that starts with “0x…” into the mentioned website text field, then click &lt;strong&gt;“Send me test Ether”&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Development IDE (Remix IDE)
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;An integrated development environment is a software application that provides comprehensive facilities to computer programmers for software development. An IDE usually consists of a source code editor, build automation tools, and a debugger. &lt;/p&gt;

&lt;p&gt;&lt;cite&gt;&lt;a href="https://en.wikipedia.org/wiki/Integrated_development_environment" rel="noopener"&gt;Wikipedia&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://remix.ethereum.org/"&gt;Remix IDE&lt;/a&gt; has all the necessary tools that we need to create, debug, compile and deploy an ERC-20 token.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to create an ERC-20 token
&lt;/h2&gt;

&lt;p&gt;Go to the Remix IDE and make a new solidity file, for example – mytoken.sol&lt;/p&gt;

&lt;p&gt;Paste the following code into your new Solidity script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pragma solidity ^0.4.24;

//Safe Math Interface

contract SafeMath {

    function safeAdd(uint a, uint b) public pure returns (uint c) {
        c = a + b;
        require(c &amp;gt;= a);
    }

    function safeSub(uint a, uint b) public pure returns (uint c) {
        require(b &amp;lt;= a);
        c = a - b;
    }

    function safeMul(uint a, uint b) public pure returns (uint c) {
        c = a * b;
        require(a == 0 || c / a == b);
    }

    function safeDiv(uint a, uint b) public pure returns (uint c) {
        require(b &amp;gt; 0);
        c = a / b;
    }
}


//ERC Token Standard #20 Interface

contract ERC20Interface {
    function totalSupply() public constant returns (uint);
    function balanceOf(address tokenOwner) public constant returns (uint balance);
    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
    function transfer(address to, uint tokens) public returns (bool success);
    function approve(address spender, uint tokens) public returns (bool success);
    function transferFrom(address from, address to, uint tokens) public returns (bool success);

    event Transfer(address indexed from, address indexed to, uint tokens);
    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}


//Contract function to receive approval and execute the function in one call

contract ApproveAndCallFallBack {
    function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}

//Actual token contract

contract SampleToken is ERC20Interface, SafeMath {
    string public symbol;
    string public name;
    uint8 public decimals;
    uint public _totalSupply;

    mapping(address =&amp;gt; uint) balances;
    mapping(address =&amp;gt; mapping(address =&amp;gt; uint)) allowed;

    constructor() public {
        symbol = "ST";
        name = "Sample Token Coin";
        decimals = 2;
        _totalSupply = 100000;
        balances[YOUR_METAMASK_WALLET_ADDRESS] = _totalSupply;
        emit Transfer(address(0), YOUR_METAMASK_WALLET_ADDRESS, _totalSupply);
    }

    function totalSupply() public constant returns (uint) {
        return _totalSupply - balances[address(0)];
    }

    function balanceOf(address tokenOwner) public constant returns (uint balance) {
        return balances[tokenOwner];
    }

    function transfer(address to, uint tokens) public returns (bool success) {
        balances[msg.sender] = safeSub(balances[msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(msg.sender, to, tokens);
        return true;
    }

    function approve(address spender, uint tokens) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        return true;
    }

    function transferFrom(address from, address to, uint tokens) public returns (bool success) {
        balances[from] = safeSub(balances[from], tokens);
        allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
        balances[to] = safeAdd(balances[to], tokens);
        emit Transfer(from, to, tokens);
        return true;
    }

    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
        return allowed[tokenOwner][spender];
    }

    function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
        allowed[msg.sender][spender] = tokens;
        emit Approval(msg.sender, spender, tokens);
        ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
        return true;
    }

    function () public payable {
        revert();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can change the token name and symbol by changing the lines 62 and 63.&lt;br&gt;&lt;br&gt;
You can change the decimal and totalsupply of your token by changing lines 64 and 65.&lt;/p&gt;

&lt;p&gt;It would help if you changed YOUR_METAMASK_WALLET_ADDRESS with your wallet address (the same wallet address that you put in the Ropsten faucet).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : The total supply value must have additional trailing zeros as specified by the decimals field. For example, the decimal value in this contract is two, and we need a total supply of 1000 tokens, so we’ll have to set the real supply variable to 100000 (simply because it won’t allow a decimal point).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s explain each part of this code:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;contract SafeMath&lt;/strong&gt; : This contract is used to safely implement the four primary math operations and use its methods instead of the usual operations to avoid possible problems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;safeAdd&lt;/strong&gt; : This is a method for adding only two positive numbers and returning the result;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;safeSub&lt;/strong&gt; : This is a method for subtracting only two positive numbers that the second one has to be less than the first one and return the results;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;safeMul&lt;/strong&gt; : This is a method for multiplying only two positive numbers and producing the results;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;safeDiv&lt;/strong&gt; : This is a method for dividing only two positive numbers also the second one is not zero and yielding the results;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;contract ERC20Interface&lt;/strong&gt; : This is an interface of the ERC20 standard token that we will use to implement our token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function&lt;/strong&gt;  &lt;strong&gt;totalSupply&lt;/strong&gt; : This function is used to return the token’s total amount.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function balanceOf&lt;/strong&gt; : This function gets an address and returns its balance in the network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function allowance&lt;/strong&gt; : This checks the remaining amounts of the token that the spender can use.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function transfer&lt;/strong&gt; : This will transfer the desired quantity token to the given address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function approve&lt;/strong&gt; : The will permit the spender to use the user token on their behalf.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function transferFrom&lt;/strong&gt; : This will send tokens from the sender to the receiver.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function approveAndCall&lt;/strong&gt; : Executing the transactions of buying and selling the tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;event Transfer&lt;/strong&gt; : This will propagate when some tokens are transferred.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;event Approval&lt;/strong&gt; : When approval is submitted, this will publish to the network.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;function () public payable&lt;/strong&gt;: Fallback function prevents sending tokens directly to the contract address.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;contract ApproveAndCallFallBack&lt;/strong&gt; : A contract function to receive authorization and execute a function in one call.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Compile and Deploy an ERC-20 token
&lt;/h2&gt;

&lt;p&gt;You can click on the compile icon and submit the compile button on the left pane. Your token is ready to deploy now!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XxFqg3l0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-5.59.57-PM-1024x495.png%3Flossy%3D0%26strip%3D1%26webp%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XxFqg3l0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-5.59.57-PM-1024x495.png%3Flossy%3D0%26strip%3D1%26webp%3D1" alt="" width="880" height="425"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Compile ERC-20 Token&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For deploying your token on the Ropsten Test Network, click on the deploy icon on the left pane and select the environment option “Injected Web3.”. Make sure to choose your primary contract name from the contract selector. Now you can submit the “Deploy” button and confirm the transaction on your MetaMask. Currently, you can see your token contract at the bottom of it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wqgVDayg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-6.01.11-PM-1024x530.png%3Flossy%3D0%26strip%3D1%26webp%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wqgVDayg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-6.01.11-PM-1024x530.png%3Flossy%3D0%26strip%3D1%26webp%3D1" alt="" width="880" height="455"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Deploy ERC-20 Token&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Great! Your first token is published on the test network.&lt;/p&gt;

&lt;h2&gt;
  
  
  Add custom token to MetaMask
&lt;/h2&gt;

&lt;p&gt;The last step is adding your custom token to your MetaMask wallet and seeing your balance.&lt;br&gt;&lt;br&gt;
At the deployed contracts tab where you deployed in the IDE, copy the contract address from there, and on your MetaMask, click on the “Import tokens” button, and paste the contract address in the “Token Contract Address” field. MetaMask will retrieve other details from the network.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JvmrzD_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-6.25.34-PM-603x1024.png%3Flossy%3D0%26strip%3D1%26webp%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JvmrzD_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-6.25.34-PM-603x1024.png%3Flossy%3D0%26strip%3D1%26webp%3D1" alt="" width="603" height="1024"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Import token in MetaMask&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PxoxvYmS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-6.25.41-PM-603x1024.png%3Flossy%3D0%26strip%3D1%26webp%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PxoxvYmS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://951422.smushcdn.com/2591829/wp-content/uploads/2022/01/Screen-Shot-2022-01-27-at-6.25.41-PM-603x1024.png%3Flossy%3D0%26strip%3D1%26webp%3D1" alt="" width="603" height="1024"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Add custom token in MetaMask&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations! You have created an ERC-20 token on the Ethereum network successfully.&lt;/p&gt;




&lt;p&gt;If you want to know when the new article will be published, subscribe to my Newsletter on my website!&lt;br&gt;
&lt;a href="https://fibocolon.com/newsletter/"&gt;Subscribe to Newsletter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ethereum</category>
      <category>erc20</category>
      <category>blockchain</category>
      <category>howto</category>
    </item>
    <item>
      <title>SINGLE APP FOR WEB, IOS, AND ANDROID USING REACT-NATIVE ( Components ) – PART 2</title>
      <dc:creator>amir nazari</dc:creator>
      <pubDate>Mon, 10 Jan 2022 13:00:00 +0000</pubDate>
      <link>https://dev.to/anazari96/single-app-for-web-ios-and-android-using-react-native-components-part-2-2lm7</link>
      <guid>https://dev.to/anazari96/single-app-for-web-ios-and-android-using-react-native-components-part-2-2lm7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tZiUOjhN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uav9djvtzvwpsg9dhgaw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tZiUOjhN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uav9djvtzvwpsg9dhgaw.png" alt="Image description" width="880" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the second part of the article. In the &lt;a href="https://dev.to/anazari96/single-app-for-web-ios-and-android-using-react-native-new-approach-part-1-18dg"&gt;previous part&lt;/a&gt;, we have examined how to configure a project and run it on all environments (iOS, Android, and Web). In this part, we want to explain how to create different containers and components to develop the logic and UI part of our project on various platforms.&lt;/p&gt;

&lt;p&gt;Creating a single application for every environment (Web, iOS, and Android) with one codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concept
&lt;/h2&gt;

&lt;p&gt;As we know, Web projects are working with DOM, and native projects have their native components. So we need to separate the UI parts of the different platforms to solve the issue.&lt;/p&gt;

&lt;p&gt;For this purpose, we should keep the logic codes apart from the UI and put them in the different components called containers. For separating the UI parts at platforms, we can use the following two extensions: &lt;code&gt;.native.tsx&lt;/code&gt; , &lt;code&gt;.web.tsx&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At the rest of the article, I explained the listed majors:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to create a mock server for getting data&lt;/li&gt;
&lt;li&gt;How to create container for the logic &lt;/li&gt;
&lt;li&gt;How to create components for the UI of the application&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;For calling AJAX request &lt;code&gt;axios&lt;/code&gt; the library is needed, so let’s execute the following command at the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Mock Server
&lt;/h2&gt;

&lt;p&gt;There are many libraries and tools to make a mock server for your project; I chose the mocks-server to create the mock server. For installing this lib execute the following command in the terminal of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i @mocks-server/main --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add the following code to the &lt;em&gt;scripts&lt;/em&gt; part of your &lt;code&gt;package.json&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"mocks": "mocks-server"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After putting the above code, you can execute the mocks from your terminal with this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm run mocks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the command, you’ll see that some files and directories will be created on the project. The mocks-server will generate a sample API for getting users inside the &lt;em&gt;/mocks/routes/users.js&lt;/em&gt; route.&lt;/p&gt;

&lt;h2&gt;
  
  
  Container
&lt;/h2&gt;

&lt;p&gt;This is a place for putting the logical codes there. Everythings that are the same between different platforms and related to the data should be in containers.&lt;/p&gt;

&lt;p&gt;The containers can be named like XConatiner.jsx, and the “X” will be the name of the related feature or component.&lt;/p&gt;

&lt;p&gt;For now, we need &lt;code&gt;UsersContainer.jsx&lt;/code&gt; to get the user’s data and pass it to the corresponding component.&lt;br&gt;&lt;br&gt;
&lt;em&gt;There are many ways to structure the React and React-Native projects, and this article doesn’t focus on them.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First of all, we need a folder inside the src directory called Users. So let’s execute the following command at the terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ./src &amp;amp;&amp;amp; mkdir Users &amp;amp;&amp;amp; cd ./Users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run the below command to create the mentioned file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ touch UsersContainer.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned, we need to retrieve the users and pass them to the related UI component in this container, so let’s put the below code inside the &lt;code&gt;UsersContainer.jsx&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, {useState, useEffect} from 'react';
import axios from 'axios';
import UsersComponent from './UsersComponent';

export const UsersContainer = () =&amp;gt; {
  const [users, setUsers] = useState([]);

  useEffect(() =&amp;gt; {
    axios
      .get('http://localhost:3100/api/users')
      .then(response =&amp;gt; {
        setUsers(response.data);
      })
      .catch(err =&amp;gt; {
        console.log('error', err);
      });
  }, []);

  return users.length &amp;gt; 0 ? &amp;lt;UsersComponent users={users} /&amp;gt; : null;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Components
&lt;/h2&gt;

&lt;p&gt;Here we have the distinction for the different platforms for the UI part. All the visual things and other specific libraries for one platform should be in these components.&lt;/p&gt;

&lt;p&gt;We need two components for web and mobile, &lt;code&gt;UsersComponent.web.jsx&lt;/code&gt; and &lt;code&gt;UsersComponent.native.jsx&lt;/code&gt;. Create these files inside the &lt;code&gt;Users&lt;/code&gt; directory with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ./src/Users &amp;amp;&amp;amp; touch UsersComponent.web.jsx UsersComponent.native.jsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These components should get users from their parent container and render it. Let’s put the following code for each file:&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 from 'react';

const UsersComponent = ({users}) =&amp;gt; {
  return (
    &amp;lt;ul&amp;gt;
      {users.map(user =&amp;gt; (
        &amp;lt;li key={user.id}&amp;gt;{user.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;
  );
};

export default UsersComponent;

import React from 'react';
import {View, Text} from 'react-native';

const UsersComponent = ({users}) =&amp;gt; {
  return (
    &amp;lt;View&amp;gt;
      {users.map(user =&amp;gt; (
        &amp;lt;Text key={user.id}&amp;gt;{user.name}&amp;lt;/Text&amp;gt;
      ))}
    &amp;lt;/View&amp;gt;
  );
};

export default UsersComponent;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to the container, you noticed that you didn’t mention importing the component .native or .web. Yes! This is the power of the Webpack and Metro, they can understand which one should be compiled, and you don’t need to worry!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>android</category>
      <category>ios</category>
      <category>react</category>
    </item>
    <item>
      <title>Single App for web, iOS, and Android using React-Native ( NEW APPROACH ) – PART 1</title>
      <dc:creator>amir nazari</dc:creator>
      <pubDate>Wed, 05 Jan 2022 23:27:15 +0000</pubDate>
      <link>https://dev.to/anazari96/single-app-for-web-ios-and-android-using-react-native-new-approach-part-1-18dg</link>
      <guid>https://dev.to/anazari96/single-app-for-web-ios-and-android-using-react-native-new-approach-part-1-18dg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CLhFXyXk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m14h7spiurw0nf556tv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CLhFXyXk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m14h7spiurw0nf556tv1.png" alt="Single App For All Env" width="880" height="461"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Many projects need to implement both web and mobile applications, but there is always hard to decide how they should be developed. Some of them implemented web applications initially, and after that, mobile application development started. Also, some libraries are out there to implement both web and mobile applications together, like React-Native-Web. But the negative point with these libraries is they are not compatible with all other libraries and they make the apps lower. There is a different approach that we can use to satisfy these needs, and that's creating separate UI component for web and mobile, but all of them has a single logical part.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concept
&lt;/h2&gt;

&lt;p&gt;Before starting to write code, and creating the file structures let me explain the concept of this approach. In React and React-Native one of the best structures is that separating the logic and data parts of every component from the UI part. So we have "Container" and "Screen" for every component in our project. Also as we need to create a project that works in iOS, Android, and Web, we need to separate every UI component into at least 2 parts; "native" and "web". Our UI components will be like "Component.native.ts" and "Component.web.ts".&lt;/p&gt;

&lt;p&gt;Maybe it looks like weird these extensions but with this separation, we can use different bundlers for each platform and our purpose will reach out.&lt;/p&gt;

&lt;p&gt;In this tutorial, I didn't integrate Redux or any state management to reduce the complexity of the tutorial but as I like Typescript and I'd like to spread the use of this wonderful language, I'll use it in my codes.&lt;/p&gt;

&lt;p&gt;This project will be in 2 parts because it's a quite long.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;First Part:&lt;/strong&gt; Create and Configure the project&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Second Part:&lt;/strong&gt; Create some components for different envrionements&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Phase 1 (Create a Project):
&lt;/h2&gt;

&lt;p&gt;I prefer to use VSCode as an editor but you can choose any other editor or IDE you want.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://code.visualstudio.com/Download"&gt;Download Visual Studio&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First of all, need to create a project, To do so, the React-Native Typescript template should execute in the terminal: &lt;em&gt;(You can change the name of the project by changing the "PROJECT_NAME" in the command below)&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;$ npx react-native init PROJECT_NAME --template react-native-template-typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you are using Macbook with any M1 chips you will face with CocoaPods error and there are some possibilities to fix the issue:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo arch -x86_64 gem install ffi

$ arch -x86_64 pod install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can follow this thread for any other ways that you like to follow for solving this issue:&lt;/p&gt;


&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Gn-iPj_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/64901180/how-to-running-cocoapods-on-apple-silicon-m1" rel="noopener noreferrer"&gt;
              
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Nov 18 '20&lt;/span&gt;
            &lt;span&gt;Comments: 1&lt;/span&gt;
            &lt;span&gt;Answers: 33&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/64901180/how-to-running-cocoapods-on-apple-silicon-m1" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y9mJpuJP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          214
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wif5Zq3z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;I have a Flutter project that I'm trying to run on iOS. It runs normally on my Intel-based Mac, but on my new Apple Silicon-based M1 Mac it fails to install pods.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;LoadError - dlsym(0x7f8926035eb0, Init_ffi_c): symbol not found - /Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/lib/ffi_c.bundle
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Library/Ruby/Gems/2.6.0/gems/ffi-1.13.1/lib/ffi.rb:6:in `rescue in &amp;lt;top (required)&amp;gt;'&lt;/code&gt;&lt;/pre&gt;…
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/64901180/how-to-running-cocoapods-on-apple-silicon-m1" rel="noopener noreferrer"&gt;Open Full Question&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;



&lt;h2&gt;
  
  
  Phase 2 (Adding Web files):
&lt;/h2&gt;

&lt;p&gt;In this step, we need to add some files to our project for adding the web functionality to it. For this purpose, we are using React library because React is already part of the project and we don't need to do a lot for compatibility.&lt;br&gt;&lt;br&gt;
First of all, we should create a &lt;code&gt;public&lt;/code&gt; directory at the root of the project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir public &amp;amp;&amp;amp; cd ./public
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Public folder stands for keeping the web files like index.html, favicon.ico, manifest.json, and robots.txt. So we need to create these files in the &lt;code&gt;public&lt;/code&gt; directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ touch index.html manifest.json robots.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;index.html&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Is the page template. We need to put the following content inside it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;link rel="icon" href="%PUBLIC_URL%/favicon.ico" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1" /&amp;gt;
    &amp;lt;meta name="theme-color" content="#000000" /&amp;gt;
    &amp;lt;meta
      name="description"
      content="Web site created using create-react-app"
    /&amp;gt;
    &amp;lt;link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /&amp;gt;
    &amp;lt;!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    --&amp;gt;
    &amp;lt;link rel="manifest" href="%PUBLIC_URL%/manifest.json" /&amp;gt;
    &amp;lt;!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    --&amp;gt;
    &amp;lt;title&amp;gt;React App&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;noscript&amp;gt;You need to enable JavaScript to run this app.&amp;lt;/noscript&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the &amp;lt;body&amp;gt; tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    --&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;manifest.json&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://hackthestuff.com/article/what-is-manifest-json-file-and-how-it-is-useful"&gt;The manifest.json is a simple JSON file in your website that tells the browser about your website on user's mobile device or desktop. Having a manifest is required by Chrome to show the Add to Home Screen prompt.&lt;/a&gt;&lt;br&gt;&lt;br&gt;
We should fill in this file with the below content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "short_name": "PROJECT_NAME",
  "name": "PROJECT_NAME",
  "icons": [
    {
      "src": "favicon.ico",
      "sizes": "64x64 32x32 24x24 16x16",
      "type": "image/x-icon"
    },
    {
      "src": "logo192.png",
      "type": "image/png",
      "sizes": "192x192"
    },
    {
      "src": "logo512.png",
      "type": "image/png",
      "sizes": "512x512"
    }
  ],
  "start_url": ".",
  "display": "standalone",
  "theme_color": "#000000",
  "background_color": "#ffffff"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You should change the PROJECT_NAME with your project&lt;/em&gt; name.&lt;/p&gt;

&lt;p&gt;As you can see in the contents of the manifest.json we need 3 files; favicon.ico, logo192.png, and logo512.png. You should put your own logo file with these three formats and sizes in the public directory that was created.&lt;/p&gt;

&lt;h3&gt;
  
  
  robots.txt
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://developers.google.com/search/docs/advanced/robots/intro"&gt;A &lt;strong&gt;robots.txt&lt;/strong&gt; file tells search engine crawlers which URLs the crawler can access on your site.&lt;/a&gt;&lt;br&gt;&lt;br&gt;
Add this file with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;You can put any pages that the robots should read in front of the Disallow.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  index.web.tsx
&lt;/h3&gt;

&lt;p&gt;After that we need to create a start point for the web project. This file will be the place that compiler start to create the hirarchy. The place of this file should be inside the &lt;code&gt;src&lt;/code&gt; directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd ./src &amp;amp;&amp;amp; touch index.web.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we need to put some code inside it for rendering our code:&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 from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(
  &amp;lt;React.StrictMode&amp;gt;
    {/* Here will be our codes and components */}
  &amp;lt;/React.StrictMode&amp;gt;,
  document.getElementById('root')
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating these files we need to change some configurations from the project. You can open your editor and open the &lt;code&gt;.eslintrc.js&lt;/code&gt; file, then add &lt;code&gt;'react-app', 'react-app/jest'&lt;/code&gt; to the extends key and it should be like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  root: true,
  extends: ['@react-native-community', 'react-app', 'react-app/jest'],
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  rules: {
    'no-shadow': 'off',
    '@typescript-eslint/no-shadow': ['error'],
  },
};

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

&lt;/div&gt;



&lt;p&gt;Then we need to change 2 property in the &lt;code&gt;tsconfig.json&lt;/code&gt; file; change the &lt;code&gt;"lib"&lt;/code&gt; property to &lt;code&gt;"dom", "dom.iterable", "esnext"&lt;/code&gt; and change the &lt;code&gt;"jsx"&lt;/code&gt; property to &lt;code&gt;"preserve"&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
The file will be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    /* Basic Options */
    "target": "esnext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "lib": ["dom", "dom.iterable", "esnext"], /* Specify library files to be included in the compilation. */
    "allowJs": true, /* Allow javascript files to be compiled. */
    // "checkJs": true, /* Report errors in .js files. */
    "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true, /* Generates corresponding '.d.ts' file. */
    // "sourceMap": true, /* Generates corresponding '.map' file. */
    // "outFile": "./", /* Concatenate and emit output to single file. */
    // "outDir": "./", /* Redirect output structure to the directory. */
    // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "removeComments": true, /* Do not emit comments to output. */
    "noEmit": true, /* Do not emit outputs. */
    // "incremental": true, /* Enable incremental compilation */
    // "importHelpers": true, /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

    /* Strict Type-Checking Options */
    "strict": true, /* Enable all strict type-checking options. */
    // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true, /* Enable strict null checks. */
    // "strictFunctionTypes": true, /* Enable strict checking of function types. */
    // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

    /* Additional Checks */
    // "noUnusedLocals": true, /* Report errors on unused locals. */
    // "noUnusedParameters": true, /* Report errors on unused parameters. */
    // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
    // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

    /* Module Resolution Options */
    "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
    // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
    // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
    // "typeRoots": [], /* List of folders to include type definitions from. */
    // "types": [], /* Type declaration files to be included in compilation. */
    "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
    "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
    "skipLibCheck": false, /* Skip type checking of declaration files. */
    "resolveJsonModule": true /* Allows importing modules with a ‘.json’ extension, which is a common practice in node projects. */

    /* Source Map Options */
    // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
    // "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
    // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
    // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

    /* Experimental Options */
    // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
    // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
  },
  "exclude": [
    "node_modules", "babel.config.js", "metro.config.js", "jest.config.js"
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then last but not least, we need to add some packages to the &lt;code&gt;package.json&lt;/code&gt; and add scripts to it.&lt;/p&gt;

&lt;p&gt;For supporting the DOM and React scripts we should add the following package to the project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"react-dom": "^17.0.2","react-scripts": "^5.0.0"&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;$ npm install react-dom react-scripts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the development purpose we need some other packages that should be added to the &lt;em&gt;devDependencies&lt;/em&gt; inside the &lt;code&gt;package.json&lt;/code&gt;. For adding to the dev part we should pass the &lt;code&gt;--save-dev&lt;/code&gt; at the end of the command. These are the packages that should be added:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;"@types/react-dom": "^17.0.11",&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"@testing-library/jest-dom": "^5.16.1",&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"@testing-library/react": "^12.1.2",&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"@testing-library/user-event": "^13.5.0",&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"@types/node": "^16.11.17",&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;"@types/react": "^17.0.38",&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For adding these packhages to you devDependencies execute the following command inside the terminal at the project root folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm install @types/react-dom @testing-library/jest-dom @testing-library/react @testing-library/user-event @types/node @types/react --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding the dependencies that we need for the project, it's time to add some additional scripts and change the of the previous ones for the readability in the project. Let's change the scripts part in the package.json file with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "build-android": "react-native run-android",
    "build-ios": "react-native run-ios",
    "start-native": "react-native start",
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
    "start-web": "react-scripts start",
    "build-web": "react-scripts build"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! You've finished the configurations of the project. Now you can start the project for the different environments.&lt;/p&gt;

&lt;p&gt;For starting on the web:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm start start-web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  For starting on the iOS:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm start build-ios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  For starting on the Android:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm start build-android
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  The rest of the article will be in the next article that comes in few days. If you want to know when the new article will be published, subscribe to my Newsletter at my own website!
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://fibocolon.com/newsletter/"&gt;Subscribe to Newsletter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>webdev</category>
      <category>android</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
