{"id":2246,"date":"2022-01-25T18:09:24","date_gmt":"2022-01-25T18:09:24","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=2246"},"modified":"2022-01-25T18:09:24","modified_gmt":"2022-01-25T18:09:24","slug":"how-to-build-ethereum-dapp-with-react-js-%c2%b7-complete-step-by-step-guide","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=2246","title":{"rendered":"How to Build Ethereum Dapp with React.js \u00b7 Complete Step-By-Step Guide"},"content":{"rendered":"\n<p>Today I&#8217;m going to show you how to build an Ethereum Dapp with React.js! Let&#8217;s create a blockchain-based todo list application powered by Ethereum smart contracts on the blockchain. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>I&#8217;ll walk you through step-by-step in the process from start to finish. We&#8217;ll create the React app first, and then connect it to an Ethereum smart contract deployed to the blockchain.https:\/\/www.youtube.com\/embed\/AiWkkj8lSTc<a href=\"https:\/\/www.dappuniversity.com\/bootcamp\">\u00a0JOIN MY FREE TRAINING!<\/a><\/p>\n\n\n\n<p>This is a four part article that has a corresponding video for each section. You can watch me start building the React app in the first video above. I&#8217;ll also guide you through the step-by-step instructions in this tutorial. You&#8217;ll see the rest of the videos as you reach each section below.<\/p>\n\n\n\n<h2>Table of Contents<\/h2>\n\n\n\n<ul><li>Intro<ul><li><a href=\"https:\/\/www.dappuniversity.com\/articles\/ethereum-dapp-react-tutorial#intro\">How Our App Works<\/a><\/li><li><a href=\"https:\/\/www.dappuniversity.com\/articles\/ethereum-dapp-react-tutorial#dependencies\">Dependencies<\/a><\/li><\/ul><\/li><li>Tutorial Steps<ol><li><a href=\"https:\/\/www.dappuniversity.com\/articles\/ethereum-dapp-react-tutorial#projectSetup\">Project Setup<\/a><\/li><li><a href=\"https:\/\/www.dappuniversity.com\/articles\/ethereum-dapp-react-tutorial#listTasks\">List Tasks<\/a><\/li><li><a href=\"https:\/\/www.dappuniversity.com\/articles\/ethereum-dapp-react-tutorial#createTasks\">Create Tasks<\/a><\/li><li><a href=\"https:\/\/www.dappuniversity.com\/articles\/ethereum-dapp-react-tutorial#completeTasks\">Complete Tasks<\/a><\/li><\/ol><\/li><\/ul>\n\n\n\n<h5 id=\"#intro\">How Our App Works<\/h5>\n\n\n\n<p>This tutorial is a continuation of previous tutorial&nbsp;<a href=\"https:\/\/www.dappuniversity.com\/articles\/blockchain-app-tutorial\">where I show you how to build a full stack blockchain based todo list<\/a>. We&#8217;re going to use the smart contract from the previous tutorial as the starting point for the dapp that we&#8217;ll build in this tutorial. You can follow along with the previous tutorial to create the todo list smart contract, or you can simply copy the source code to get started with this one. Either way, you&#8217;ll need a copy of the Ethereum todo list smart contract to follow along with this tutorial.<\/p>\n\n\n\n<p>Let me explain how the application will work that we&#8217;ll build in this tutorial. We&#8217;ll create a React.js application for the todo list that will talk directly to the smart contract on the blockchain. We&#8217;ll use the Ethereum blockchain in this tutorial, which we can access by connecting our client side application to a single Ethereum node. The smart contract written in Solidity will powers the backend of the todo list. We&#8217;ll connect to the blockchain network with our personal account using an Ethereum wallet in order to interact with the todo list application that we&#8217;ll create.<\/p>\n\n\n\n<h5>Application Preview<\/h5>\n\n\n\n<p>Here is a preview of the todo list application that we&#8217;ll build in this tutorial. We&#8217;ll be able to list out all of the tasks in the todo list, create new ones, and complete them.<img src=\"https:\/\/www.dappuniversity.com\/blockchain-app-tutorial\/ethereum-todo-list-application.png\" alt=\"Ethereum Todo List Application\"><\/p>\n\n\n\n<h3 id=\"dependencies\">Installing Dependencies<\/h3>\n\n\n\n<p>Now let&#8217;s install all of the dependencies we need to build our project. First, we&#8217;ll set up a person blockchain to develop the application locally.<\/p>\n\n\n\n<h5>Ganache Personal Blockchain<\/h5>\n\n\n\n<p>The dependency is a personal blockchain, which is a local development blockchain that can be used to mimic the behavior of a public blockchain. I recommend using&nbsp;<a href=\"http:\/\/truffleframework.com\/ganache\" target=\"_blank\" rel=\"noreferrer noopener\">Ganache<\/a>&nbsp;as your personal blockchain for Ethereum development. It is available on Windows, Mac, and Linux as as a desktop application and a command line tool!<img src=\"https:\/\/www.dappuniversity.com\/blockchain-developer-toolkit\/1-ganache.png\" alt=\"Ganache Personal Blockchain Application\"><\/p>\n\n\n\n<p>I&#8217;ll walk you through setting up the desktop application in this tutorial. You can find the latest release for your operating system&nbsp;<a href=\"https:\/\/github.com\/trufflesuite\/ganache\/releases\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>. Once you&#8217;ve downloaded the archived package, extract the installer and run through the setup steps. Once you&#8217;ve installed it, you should see a this screen whenever you open it:<img src=\"https:\/\/www.dappuniversity.com\/blockchain-developer-toolkit\/2-ganache.png\" alt=\"Ganache Personal Blockchain Application\"><\/p>\n\n\n\n<p>Yay! ? Now you have a personal blockchain network running!\/p&gt;<\/p>\n\n\n\n<h5>Metamask Ethereum Wallet<\/h5>\n\n\n\n<p>Now it&#8217;s time to turn your web browser into a blockchain browser. Most major web browsers do not currently connect to blockchain networks, so we&#8217;ll have to install a browser extension that allows them to do this.<img src=\"https:\/\/www.dappuniversity.com\/blockchain-developer-toolkit\/9-metamask.png\" alt=\"Metamask Ethereum Wallet Browser Extension\"><\/p>\n\n\n\n<p>I&#8217;ll the&nbsp;<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/metamask\/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en\" target=\"_blank\" rel=\"noreferrer noopener\">Metamask extension for Google Chrome<\/a>. To install Metamask,&nbsp;<a href=\"https:\/\/chrome.google.com\/webstore\/detail\/metamask\/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en\" target=\"_blank\" rel=\"noreferrer noopener\">visit this link<\/a>&nbsp;or search for the Metamask Chrome plugin in the Google Chrome web store. Once you\u2019ve installed it, be sure that it is checked in your list of extensions. You\u2019ll see the fox icon in the top right hand side of your Chrome browser when it\u2019s installed. Reference the video walk through if you get stuck!<\/p>\n\n\n\n<p>Metamask will also allow us to manage our personal account when we connect to the blockchain, as well as manage our Ether funds that we&#8217;ll need to pay for transactions.<\/p>\n\n\n\n<h5>Node.JS<\/h5>\n\n\n\n<p>Now that you have a private blockchain running, you need install&nbsp;<a href=\"https:\/\/nodejs.org\/en\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node Package Manager<\/a>, or NPM, which comes with Node.js. You can see if you have node already installed by going to your terminal and typing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ node -v<\/code><\/pre>\n\n\n\n<p>If you don&#8217;t have node already installed you can visit the&nbsp;<a href=\"https:\/\/nodejs.org\/en\/\" target=\"_blank\" rel=\"noreferrer noopener\">Node.js website<\/a>&nbsp;to download it.<\/p>\n\n\n\n<h5>Create React App<\/h5>\n\n\n\n<p>Next, you&#8217;ll need to install&nbsp;<a href=\"https:\/\/facebook.github.io\/create-react-app\/\" target=\"_blank\" rel=\"noreferrer noopener\">Create React App<\/a>, which will allow us to bootstrap a new React.js application without too much configuration. You can install it from the command line like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install create-react-app<\/code><\/pre>\n\n\n\n<h2 id=\"projectSetup\">Step 1: Project Setup<\/h2>\n\n\n\n<p><em>Before you get started, ensure that Ganache is running, and that you deployed the todo list smart contract from the&nbsp;<a href=\"https:\/\/www.dappuniversity.com\/articles\/blockchain-app-tutorial\">previous tutorial<\/a>!<\/em><\/p>\n\n\n\n<p>Now let&#8217;s create the project! We&#8217;ll do that with Create React App like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ create-react-app eth-todo-list-react\n$ cd eth-todo-list-react\n<\/code><\/pre>\n\n\n\n<p>Now we can run the web server that came with the react app to make sure that everything worked properly:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm run start<\/code><\/pre>\n\n\n\n<p>Your browser should open automatically. You should see a web page like this:<img src=\"https:\/\/www.dappuniversity.com\/ethereum-dapp-react-tutorial\/smoke-test.png\" alt=\"Create React App\"><\/p>\n\n\n\n<p>Great, that worked! Now let&#8217;s update the&nbsp;<code>package.json<\/code>&nbsp;file to include all of the dependencies we need for the tutorial.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"name\": \"eth-todo-list-react\",\n  \"version\": \"0.1.0\",\n  \"private\": true,\n  \"dependencies\": {\n    \"bootstrap\": \"^4.3.1\",\n    \"react\": \"^16.8.3\",\n    \"react-bootstrap\": \"^1.0.0-beta.5\",\n    \"react-dom\": \"^16.8.3\",\n    \"react-scripts\": \"2.1.5\",\n    \"web3\": \"^1.0.0-beta.46\"\n  },\n  \"scripts\": {\n    \"start\": \"react-scripts start\",\n    \"build\": \"react-scripts build\",\n    \"test\": \"react-scripts test\",\n    \"eject\": \"react-scripts eject\"\n  },\n  \"eslintConfig\": {\n    \"extends\": \"react-app\"\n  },\n  \"browserslist\": &#91;\n    \"&gt;0.2%\",\n    \"not dead\",\n    \"not ie &lt;= 11\",\n    \"not op_mini all\"\n  ]\n}<\/code><\/pre>\n\n\n\n<p>Now we can install these dependencies like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install<\/code><\/pre>\n\n\n\n<p>Just make sure you restart your web server after installing these dependencies. Let me explain the two new dependencies we just installed.<\/p>\n\n\n\n<ol><li><a href=\"https:\/\/web3js.readthedocs.io\/en\/1.0\/\" target=\"_blank\" rel=\"noreferrer noopener\">Web3.js<\/a>&nbsp;&#8211; This is the main JavaScript library for communicating with the Ethereum blockchain. It will effectively turn our React application into a blockchain-enabled application. We&#8217;ll use Web3 to interact with the todo list smart contract backend in this tutorial.<\/li><li><a href=\"https:\/\/react-bootstrap.github.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">React Bootstrap<\/a>&nbsp;&#8211; This is the React version of the&nbsp;<a href=\"https:\/\/getbootstrap.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Bootstrap templating framework<\/a>. It will allow us to create rich, interactive user interfaces without having to write a lot of CSS to get started.<\/li><\/ol>\n\n\n\n<p>Next, we need to further configure our app to import the bootstrap styles. Add this line to your&nbsp;<code>index.js<\/code>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> 'bootstrap\/dist\/css\/bootstrap.css';<\/code><\/pre>\n\n\n\n<p>Next, we&#8217;ll clear out all of the default style sheets so that we don&#8217;t conflict with bootstrap.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ rm src\/index.css<\/code><\/pre>\n\n\n\n<p>Don&#8217;t forget to remove this from the imported files in&nbsp;<code>src\/App.js<\/code>!<\/p>\n\n\n\n<p>Then, we&#8217;ll wipe out all of the css in side the<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">src\/App.css<\/pre>\n\n\n\n<p>file.<\/p>\n\n\n\n<p>Now let&#8217;s actually connect our app to the blockhain and list out the current account that we&#8217;re connected with. In order to do this, let&#8217;s clear out all of the code from the&nbsp;<code>src\/App.js<\/code>&nbsp;file, and replace it with this code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> React, { Component } <strong>from<\/strong> 'react'\n<strong>import<\/strong> Web3 <strong>from<\/strong> 'web3'\n<strong>import<\/strong> '.\/App.css'\n\n<strong>class<\/strong> <strong>App<\/strong> <strong>extends<\/strong> <strong>Component<\/strong> {\n  componentWillMount() {\n    <strong>this<\/strong>.loadBlockchainData()\n  }\n\n  <strong>async<\/strong> loadBlockchainData() {\n    <strong>const<\/strong> web3 = <strong>new<\/strong> Web3(Web3.givenProvider || \"http:\/\/localhost:8545\")\n    <strong>const<\/strong> accounts = <strong>await<\/strong> web3.eth.getAccounts()\n    <strong>this<\/strong>.setState({ account: accounts&#91;0] })\n  }\n\n  <strong>constructor<\/strong>(props) {\n    <strong>super<\/strong>(props)\n    <strong>this<\/strong>.state = { account: '' }\n  }\n\n  render() {\n    <strong>return<\/strong> (\n      &lt;<strong>div<\/strong> className=\"container\"&gt;\n        &lt;<strong>h1l<\/strong>&gt;Hello, World!&lt;\/<strong>h1<\/strong>&gt;\n        &lt;<strong>p<\/strong>&gt;Your account: {this.state.account}&lt;\/<strong>p<\/strong>&gt;\n      &lt;\/<strong>div<\/strong>&gt;\n    );\n  }\n}\n\n<strong>export<\/strong> <strong>default<\/strong> App;<\/code><\/pre>\n\n\n\n<p>Let me explain what this code does.<\/p>\n\n\n\n<ul><li>First, we import the web3 library at the top of the file with&nbsp;<code>import Web3 from 'web3'<\/code>.<\/li><li>Next, we use React&#8217;s build incomponentWillMount()function to load all the blockhain data. This function gets called whenever the component will load properly the first time. React has many component and lifeycle functions which you can read about&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/li><li>Next, we connect to the blockchain by instantiating a new web3 connection, while passing in the url to Ganache&nbsp;<code>http:\/\/localhost:7545<\/code>.<\/li><li>Then, we load the current account we&#8217;re connected with. We add that to React&#8217;s state object, which allows us to track the current state of the component.<\/li><li>Finally, we read the account from the state object and render it within the HTML output from therender()function.<\/li><\/ul>\n\n\n\n<p>At this point, the application should look like this in the browser:<img src=\"https:\/\/www.dappuniversity.com\/ethereum-dapp-react-tutorial\/hello-world.png\" alt=\"Hello World Ethereum Dapp React.js\"><\/p>\n\n\n\n<h2 id=\"projectSetup\">Step 2: List Tasks<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u0438-\u043e\u0431\u0440\u043e\u0431\u043d\u0438\u043a wp-block-embed-\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u0438-\u043e\u0431\u0440\u043e\u0431\u043d\u0438\u043a wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Build React Blockchain App - Ethereum\/React Todo List #2\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/mmI5CpMw3gU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Now let&#8217;s list out the tasks from our smart contract todo list backend that&#8217;s deployed to the blockchain. You can watch me do this on screen in the video above in addition to following the step-by-step instructions in this tutorial section.<\/p>\n\n\n\n<p>In order to list the tasks out from the smart contract, we need two pieces of information.<\/p>\n\n\n\n<ul><li>The smart contract ABI &#8211; this a JSON description of how the smart contract behaves. It describes its functions, behavior, etc&#8230;<\/li><li>The smart contract address &#8211; this will be the address of the smart contract deployed to Ganache, the personal blockchain network. Make sure you deployed the smart contract to the blockchain already. You can follow the steps on how to do that in the<a href=\"https:\/\/www.dappuniversity.com\/articles\/blockchain-app-tutorial\">previous tutorial<\/a>!<\/li><\/ul>\n\n\n\n<p>Let&#8217;s create a new file to store these two pieces of information:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ touch src\/config.js<\/code><\/pre>\n\n\n\n<p>Add the smart contract ABI and the smart contract address to the file like this. NOTE: You should use the actual address of the smart contract that you deployed to your own blockhain.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>export<\/strong> <strong>const<\/strong> TODO_LIST_ADDRESS = 'YOUR_SMART_CONTRACT_ADDRESS_GOES_HERE'\n\n<strong>export<\/strong> <strong>const<\/strong> TODO_LIST_ABI = &#91;\n  {\n    \"constant\": true,\n    \"inputs\": &#91;\n      {\n        \"name\": \"\",\n        \"type\": \"uint256\"\n      }\n    ],\n    \"name\": \"tasks\",\n    \"outputs\": &#91;\n      {\n        \"name\": \"id\",\n        \"type\": \"uint256\"\n      },\n      {\n        \"name\": \"content\",\n        \"type\": \"string\"\n      },\n      {\n        \"name\": \"completed\",\n        \"type\": \"bool\"\n      }\n    ],\n    \"payable\": false,\n    \"stateMutability\": \"view\",\n    \"type\": \"function\",\n    \"signature\": \"0x8d977672\"\n  },\n  {\n    \"constant\": true,\n    \"inputs\": &#91;],\n    \"name\": \"taskCount\",\n    \"outputs\": &#91;\n      {\n        \"name\": \"\",\n        \"type\": \"uint256\"\n      }\n    ],\n    \"payable\": false,\n    \"stateMutability\": \"view\",\n    \"type\": \"function\",\n    \"signature\": \"0xb6cb58a5\"\n  },\n  {\n    \"inputs\": &#91;],\n    \"payable\": false,\n    \"stateMutability\": \"nonpayable\",\n    \"type\": \"constructor\",\n    \"signature\": \"constructor\"\n  },\n  {\n    \"anonymous\": false,\n    \"inputs\": &#91;\n      {\n        \"indexed\": false,\n        \"name\": \"id\",\n        \"type\": \"uint256\"\n      },\n      {\n        \"indexed\": false,\n        \"name\": \"content\",\n        \"type\": \"string\"\n      },\n      {\n        \"indexed\": false,\n        \"name\": \"completed\",\n        \"type\": \"bool\"\n      }\n    ],\n    \"name\": \"TaskCreated\",\n    \"type\": \"event\",\n    \"signature\": \"0x05d0fb833127fc08168556d0e7ca9554fc3f6bc843b3b7d2bf1c35aea6bab660\"\n  },\n  {\n    \"anonymous\": false,\n    \"inputs\": &#91;\n      {\n        \"indexed\": false,\n        \"name\": \"id\",\n        \"type\": \"uint256\"\n      },\n      {\n        \"indexed\": false,\n        \"name\": \"completed\",\n        \"type\": \"bool\"\n      }\n    ],\n    \"name\": \"TaskCompleted\",\n    \"type\": \"event\",\n    \"signature\": \"0xe21fa966ca5cd02748c0752352d18c48165e61cb55b4c29cccf924b5a95fcff1\"\n  },\n  {\n    \"constant\": false,\n    \"inputs\": &#91;\n      {\n        \"name\": \"_content\",\n        \"type\": \"string\"\n      }\n    ],\n    \"name\": \"createTask\",\n    \"outputs\": &#91;],\n    \"payable\": false,\n    \"stateMutability\": \"nonpayable\",\n    \"type\": \"function\",\n    \"signature\": \"0x111002aa\"\n  },\n  {\n    \"constant\": false,\n    \"inputs\": &#91;\n      {\n        \"name\": \"_id\",\n        \"type\": \"uint256\"\n      }\n    ],\n    \"name\": \"toggleCompleted\",\n    \"outputs\": &#91;],\n    \"payable\": false,\n    \"stateMutability\": \"nonpayable\",\n    \"type\": \"function\",\n    \"signature\": \"0x455f5024\"\n  }\n]<\/code><\/pre>\n\n\n\n<p>Now let&#8217;s import these values into our&nbsp;<code>src\/App.js<\/code>&nbsp;file like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> { TODO_LIST_ABI, TODO_LIST_ADDRESS } <strong>from<\/strong> '.\/config'<\/code><\/pre>\n\n\n\n<p>Yay! Now we have everything we need to connect to our smart contract. We can connect to the todo list smart contract inside the<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">loadBlockchainData()<\/pre>\n\n\n\n<p>function like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>async<\/strong> <strong>loadBlockchainData<\/strong>() {\n    <strong>const<\/strong> web3 = <strong>new<\/strong> Web3(Web3.givenProvider || \"http:\/\/localhost:8545\")\n    <strong>const<\/strong> accounts = <strong>await<\/strong> web3.eth.getAccounts()\n    <strong>this<\/strong>.setState({ account: accounts&#91;0] })\n    <strong>const<\/strong> todoList = <strong>new<\/strong> web3.eth.Contract(TODO_LIST_ABI, TODO_LIST_ADDRESS)\n    <strong>this<\/strong>.setState({ todoList })\n    <strong>const<\/strong> taskCount = <strong>await<\/strong> todoList.methods.taskCount().call()\n    <strong>this<\/strong>.setState({ taskCount })\n    <strong>for<\/strong> (<strong>var<\/strong> i = 1; i &lt;= taskCount; i++) {\n      <strong>const<\/strong> task = <strong>await<\/strong> todoList.methods.tasks(i).call()\n      <strong>this<\/strong>.setState({\n        tasks: &#91;...<strong>this<\/strong>.state.tasks, task]\n      })\n    }\n}<\/code><\/pre>\n\n\n\n<p>I&#8217;ll explain how this works:<\/p>\n\n\n\n<ul><li>First, we instantiate the smart contract with the ABI and the address with&nbsp;<code>const todoList = new web3.eth.Contract(TODO_LIST_ABI, TODO_LIST_ADDRESS)<\/code>, and store the result to the component state.<\/li><li>Then, we fetch the total number of tasks in the todo list to determine the number of times we want to call the&nbsp;<code>tasks()<\/code>&nbsp;function to fetch each task (the only way to return all the tasks is to fetch them one by one). Then we fetch each task and store it to the component state.<\/li><\/ul>\n\n\n\n<p>Next, we set the default state in the component like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  <strong>constructor<\/strong>(props) {\n    <strong>super<\/strong>(props)\n    <strong>this<\/strong>.state = {\n      account: '',\n      taskCount: 0,\n      tasks: &#91;]\n    }\n}<\/code><\/pre>\n\n\n\n<p>Next, we list the tasks out inside the&nbsp;<code>render()<\/code>&nbsp;function like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>render() {\nreturn (\n  &lt;<strong>div<\/strong>&gt;\n    &lt;<strong>nav<\/strong> className=\"navbar navbar-dark fixed-top bg-dark flex-md-nowrap p-0 shadow\"&gt;\n      &lt;<strong>a<\/strong> className=\"navbar-brand col-sm-3 col-md-2 mr-0\" href=\"http:\/\/www.dappuniversity.com\/free-download\" target=\"_blank\"&gt;Dapp University | Todo List&lt;\/<strong>a<\/strong>&gt;\n      &lt;<strong>ul<\/strong> className=\"navbar-nav px-3\"&gt;\n        &lt;<strong>li<\/strong> className=\"nav-item text-nowrap d-none d-sm-none d-sm-block\"&gt;\n          &lt;<strong>small<\/strong>&gt;&lt;<strong>a<\/strong> className=\"nav-link\" href=\"#\"&gt;&lt;<strong>span<\/strong> id=\"account\"&gt;&lt;\/<strong>span<\/strong>&gt;&lt;\/<strong>a<\/strong>&gt;&lt;\/<strong>small<\/strong>&gt;\n        &lt;\/<strong>li<\/strong>&gt;\n      &lt;\/<strong>ul<\/strong>&gt;\n    &lt;\/<strong>nav<\/strong>&gt;\n    &lt;<strong>div<\/strong> className=\"container-fluid\"&gt;\n      &lt;<strong>div<\/strong> className=\"row\"&gt;\n        &lt;<strong>main<\/strong> role=\"main\" className=\"col-lg-12 d-flex justify-content-center\"&gt;\n          &lt;<strong>div<\/strong> id=\"loader\" className=\"text-center\"&gt;\n            &lt;<strong>p<\/strong> className=\"text-center\"&gt;Loading...&lt;\/<strong>p<\/strong>&gt;\n          &lt;\/<strong>div<\/strong>&gt;\n          &lt;<strong>div<\/strong> id=\"content\"&gt;\n            &lt;<strong>form<\/strong>&gt;\n              &lt;<strong>input<\/strong> id=\"newTask\" type=\"text\" className=\"form-control\" placeholder=\"Add task...\" required \/&gt;\n              &lt;<strong>input<\/strong> type=\"submit\" hidden=\"\" \/&gt;\n            &lt;\/<strong>form<\/strong>&gt;\n            &lt;<strong>ul<\/strong> id=\"taskList\" className=\"list-unstyled\"&gt;\n              { this.state.tasks.map((task, key) =&gt; {\n                return(\n                  &lt;<strong>div<\/strong> className=\"taskTemplate\" className=\"checkbox\" key={key}&gt;\n                    &lt;<strong>label<\/strong>&gt;\n                      &lt;<strong>input<\/strong> type=\"checkbox\" \/&gt;\n                      &lt;<strong>span<\/strong> className=\"content\"&gt;{task.content}&lt;\/<strong>span<\/strong>&gt;\n                    &lt;\/<strong>label<\/strong>&gt;\n                  &lt;\/<strong>div<\/strong>&gt;\n                )\n              })}\n            &lt;\/<strong>ul<\/strong>&gt;\n            &lt;<strong>ul<\/strong> id=\"completedTaskList\" className=\"list-unstyled\"&gt;\n            &lt;\/<strong>ul<\/strong>&gt;\n          &lt;\/<strong>div<\/strong>&gt;\n        &lt;\/<strong>main<\/strong>&gt;\n      &lt;\/<strong>div<\/strong>&gt;\n    &lt;\/<strong>div<\/strong>&gt;\n  &lt;\/<strong>div<\/strong>&gt;\n);<\/code><\/pre>\n\n\n\n<p>Finally, we can replace all the styles in our&nbsp;<code>src\/App.css<\/code>&nbsp;file with some custom styles like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>main<\/strong> {\n  <strong>margin-top<\/strong>: 60px;\n}\n\n<strong>form<\/strong> {\n  <strong>width<\/strong>: 350px;\n  <strong>margin-bottom<\/strong>: 10px;\n}\n\n<strong>ul<\/strong> {\n  <strong>margin-bottom<\/strong>: 0px;\n}\n\n#completedTaskList .content {\n  <strong>color<\/strong>: grey;\n  <strong>text-decoration<\/strong>: line-through;\n}<\/code><\/pre>\n\n\n\n<p>Now you should see tasks listed on the page in your web browser!<img src=\"https:\/\/www.dappuniversity.com\/ethereum-dapp-react-tutorial\/list-tasks.png\" alt=\"Hello World Ethereum Dapp React.js Todo Tasks\"><\/p>\n\n\n\n<h2 id=\"listTasks\">Step 3: Create Tasks<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u0438-\u043e\u0431\u0440\u043e\u0431\u043d\u0438\u043a wp-block-embed-\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u0438-\u043e\u0431\u0440\u043e\u0431\u043d\u0438\u043a wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Etherem Smart Contracts with React JS - Ethereum\/React Todo List #3\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/a_9SdGgB6u8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Now let&#8217;s create new tasks with our todo list smart contract from our React.js application. You can watch me do this on screen in the video above in addition to following the step-by-step instructions in this tutorial section.<\/p>\n\n\n\n<p>First, we&#8217;ll start by creating a new component to manage the todo list like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ touch src\/TodoList.js<\/code><\/pre>\n\n\n\n<p>Then, we&#8217;ll import that component inside the main App component like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> TodoList <strong>from<\/strong> '.\/TodoList'<\/code><\/pre>\n\n\n\n<p>Now, let&#8217;s add some code for this todo list component. We&#8217;ll move a lot of the code from the main&nbsp;<code>App<\/code>&nbsp;component into this file. We will preserve the &#8220;listing&#8221; behavior that we built into this component like this. What&#8217;s critical about this section is that we&#8217;ll wire up the form submission to create new tasks on the blockchain. Use this code below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>import<\/strong> React, { Component } <strong>from<\/strong> 'react'\n\n<strong>class<\/strong> <strong>TodoList<\/strong> <strong>extends<\/strong> <strong>Component<\/strong> {\n\n  render() {\n    <strong>return<\/strong> (\n      &lt;div id=\"content\"&gt;\n        &lt;form onSubmit={(event) =&gt; {\n          event.preventDefault()\n          <strong>this<\/strong>.props.createTask(<strong>this<\/strong>.task.value)\n        }}&gt;\n          &lt;input id=\"newTask\" ref={(input) =&gt; <strong>this<\/strong>.task = input} type=\"text\" className=\"form-control\" placeholder=\"Add task...\" required \/&gt;\n          &lt;input type=\"submit\" hidden={true} \/&gt;\n        &lt;\/form&gt;\n        &lt;ul id=\"taskList\" className=\"list-unstyled\"&gt;\n          { <strong>this<\/strong>.props.tasks.map((task, key) =&gt; {\n            <strong>return<\/strong>(\n              &lt;div className=\"taskTemplate\" className=\"checkbox\" key={key}&gt;\n                &lt;label&gt;\n                  &lt;input type=\"checkbox\" \/&gt;\n                  &lt;span className=\"content\"&gt;{task.content}&lt;\/span&gt;\n                &lt;\/label&gt;\n              &lt;\/div&gt;\n            )\n          })}\n        &lt;\/ul&gt;\n        &lt;ul id=\"completedTaskList\" className=\"list-unstyled\"&gt;\n        &lt;\/ul&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n\n<strong>export<\/strong> <strong>default<\/strong> TodoList;<\/code><\/pre>\n\n\n\n<p>The important part of this code to notice in this step is that we created an&nbsp;<code>onSubmit<\/code>&nbsp;handler for the form that gets triggered whenever a new task is added from the browser. Inside here, we call&nbsp;<code>this.props.createTask(this.task.value)<\/code>. This a function that we&#8217;ll define in the parent&nbsp;<code>App<\/code>&nbsp;component and pass down via&nbsp;<code>props<\/code>&nbsp;to the&nbsp;<code>TodoList<\/code>&nbsp;component.<\/p>\n\n\n\n<p>Now, let&#8217;s define the&nbsp;<code>createTask()<\/code>&nbsp;function in the parent component like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>createTask(content) {\n    <strong>this<\/strong>.setState({ loading: true })\n    <strong>this<\/strong>.state.todoList.methods.createTask(content).send({ from: <strong>this<\/strong>.state.account })\n    .once('receipt', (receipt) =&gt; {\n      <strong>this<\/strong>.setState({ loading: false })\n})<\/code><\/pre>\n\n\n\n<p>I&#8217;ll explain what this does.<\/p>\n\n\n\n<ul><li>First, it updates the loading state to&nbsp;<code>true<\/code>.<\/li><li>Then, it calls the&nbsp;<code>createTask()<\/code>&nbsp;function on the smart contract itself with web3.js. It tells the blockchain that it&#8217;s coming from our account. Then, it updates the loading state tofalsewhenever the transaction receipt is returned.<\/li><\/ul>\n\n\n\n<p>Now, we must bind the function like this inside the&nbsp;<code>constructor()<\/code>&nbsp;function like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>this<\/strong>.createTask = <strong>this<\/strong>.createTask.bind(<strong>this<\/strong>)<\/code><\/pre>\n\n\n\n<p>And finally, render out the todo list component, passing the newly created function down via props like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;main role=\"main\" className=\"col-lg-12 d-flex justify-content-center\"&gt;\n  { <strong>this<\/strong>.state.loading\n    ? &lt;<strong>div<\/strong> id=\"loader\" className=\"text-center\"&gt;&lt;<strong>p<\/strong> className=\"text-center\"&gt;Loading...&lt;\/<strong>p<\/strong>&gt;&lt;\/<strong>div<\/strong>&gt;\n    : &lt;<strong>TodoList<\/strong> tasks={this.state.tasks} createTask={this.createTask} \/&gt;\n  }\n&lt;\/<strong>main<\/strong>&gt;<\/code><\/pre>\n\n\n\n<p>Finally, we can keep track of the loading state by first setting default state in the&nbsp;<code>constructor()<\/code>&nbsp;function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>constructor<\/strong>(props) {\n    <strong>super<\/strong>(props)\n    <strong>this<\/strong>.state = {\n      account: '',\n      taskCount: 0,\n      tasks: &#91;],\n      loading: true\n}<\/code><\/pre>\n\n\n\n<p>Then, set it to&nbsp;<code>false<\/code>&nbsp;whenever the blockchain data is loaded from the&nbsp;<code>loadBlockchainData()<\/code>&nbsp;function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>this<\/strong>.setState({ <strong>loading<\/strong>: false })<\/code><\/pre>\n\n\n\n<p>Yay! Now you can create todo items with the smart contract.<\/p>\n\n\n\n<h2 id=\"completeTasks\">Step 4: Complete Tasks<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u0438-\u043e\u0431\u0440\u043e\u0431\u043d\u0438\u043a wp-block-embed-\u0432\u0441\u0442\u0430\u0432\u0438\u0442\u0438-\u043e\u0431\u0440\u043e\u0431\u043d\u0438\u043a wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Build A Complete Ethereum Dapp With React JS - Ethereum\/React Todo List #4\" width=\"840\" height=\"473\" src=\"https:\/\/www.youtube.com\/embed\/KjvqfHyrJVQ?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Now let&#8217;s finish the final step, which is to mark tasks as completed on our todo list smart contract from our React.js application. You can watch me do this on screen in the video above in addition to following the step-by-step instructions in this tutorial section.<\/p>\n\n\n\n<p>We can toggle tasks as completed by triggering an&nbsp;<code>onClick<\/code>&nbsp;event for the task&#8217;s checkbox that will call a&nbsp;<code>toggleCompleted()<\/code>&nbsp;function that we&#8217;ll define inside the parent&nbsp;<code>App<\/code>&nbsp;component, much like we did in the previous step when we created a new task with the form.<\/p>\n\n\n\n<p>We can replace the checkbox form input with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;input\n  type=\"checkbox\"\n  name={task.id}\n  defaultChecked={task.completed}\n  <strong>ref<\/strong>={(input) =&gt; {\n    <strong>this<\/strong>.checkbox = input\n  }}\n  onClick={(<strong>event<\/strong>) =&gt; {\n    <strong>this<\/strong>.props.toggleCompleted(<strong>this<\/strong>.checkbox.name)\n}}\/&gt;\n<\/code><\/pre>\n\n\n\n<p>Now, we can create the<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">toggleCompleted()<\/pre>\n\n\n\n<p>function in the parent&nbsp;<code>App<\/code>&nbsp;component like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>toggleCompleted(taskId) {\n    <strong>this<\/strong>.setState({ loading: true })\n    <strong>this<\/strong>.state.todoList.methods.toggleCompleted(taskId).send({ from: <strong>this<\/strong>.state.account })\n    .once('receipt', (receipt) =&gt; {\n      <strong>this<\/strong>.setState({ loading: false })\n    })\n}<\/code><\/pre>\n\n\n\n<p>This works a lot like the&nbsp;<code>createTask()<\/code>&nbsp;function we created in the previous section. It calls the smart contract function with the current account, and manages the component loading state.<\/p>\n\n\n\n<p>Finally, we must bind the newly created function to the component like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>this<\/strong>.toggleCompleted = <strong>this<\/strong>.toggleCompleted.bind(<strong>this<\/strong>)<\/code><\/pre>\n\n\n\n<p>Now, your COMPLETED todo list application should look like this:<img src=\"https:\/\/www.dappuniversity.com\/ethereum-dapp-react-tutorial\/complete-app.png\" alt=\"Hello World Ethereum Dapp React.js Todo List Full App\"><\/p>\n\n\n\n<p>Congratulations! ? You have successfully built a full stack blockchain application powered by Ethereum smart contracts! You can download the full source code to this tutorial&nbsp;<a href=\"https:\/\/github.com\/dappuniversity\/eth-todo-list-react\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>Happy with this tutorial? Then you&nbsp;<strong>NEED<\/strong>&nbsp;to&nbsp;<a href=\"https:\/\/www.dappuniversity.com\/bootcamp\">join my free training here<\/a>&nbsp;where I&#8217;ll show you how to build a real world blockchain app so that you can become a highly paid blockchain developer!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I&#8217;m going to show you how to build an Ethereum Dapp with React.js! Let&#8217;s create a blockchain-based todo list application powered by Ethereum smart contracts on the blockchain.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[178],"tags":[174,65],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2246"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2246"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2246\/revisions"}],"predecessor-version":[{"id":2250,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/2246\/revisions\/2250"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}