create account

Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1 by nicknyr

View this thread on: hive.blogpeakd.comecency.com
· @nicknyr · (edited)
$51.99
Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1
https://github.com/facebook/react
https://github.com/steemit/steem-js
<br>
<center><h1>Building a web app with React, Redux, and the Steem.js API, Part 1</h1></center>
<br>
<img src="https://i.imgur.com/NSnwYTj.jpg" title="source: imgur.com" />
> Image from mit.edu

<br>
<hr>
<center><h3><strong>Getting Started with the Steem.js API in a React project</strong></h3></center>
<br>
<p>I recently started poking around the Steem/Steemit documentation on Github and found several APIs made for interacting with the Steem blockchain. There are several APIs available in Javascript, Python, and Ruby and some very intensive documentation available on both <a href="https://github.com/steemit/devportal">Github</a> and on the <a href="https://developers.steem.io/#introduction-welcome">Steem website</a>. 

Over the course of my next few tutorials I will teach you how to programmatically communicate with the Steem blockchain via the Steem Javascript API in a React.js environment. Most of the Steem.js documentation is either outdated or works in a pure Node.js environment, and I have yet to find any documentation online that incorporates the Steem.js API in a React project. I hope to explore the API, integrate it into a React/Redux project, and build a cool little web app.  This project will be a learning experience for both the reader and myself.  Bear in mind that there isn't a whole lot of examples of apps working with the API in a React project and that I am learning as I go.
<br>
<h4>What you will learn in this tutorial series: </h4>
<ul>
<li>Setting up a React project</li>
<li>Using npm to install the Steem.js API</li>
<li>Creating a basic React component with local state</li>
<li>Making an API call to the Steem.js API</li>
<li>Getting up and running with the Steem.js API in a React environment</li>
</ul>
<br>

<h4>Requirements: </h4>
<ul>
<li>Understanding of Javascript/React basics</li>
<li><a href="https://nodejs.org/en/">Node.js</a></li>
<li><a href="https://www.npmjs.com/">Node Package Manager (npm)</a></li>
<li>Some experience working with <a href="https://en.wikipedia.org/wiki/Application_programming_interface">APIs</a></li>
<li><a href="https://redux.js.org/">Redux (later on in the series)</a></li>
<li>And of course, the <a href="https://github.com/steemit/steem-js">Steem.js API</a></li>
</ul>
<br>

<h4>Difficulty: Intermediate</h4>
<hr>
<br>
<center><h3>Getting up and running with our React project:</h3></center>
<br>
In this first part of the tutorial we will get up and running with creating our React project using <a href="https://github.com/facebook/create-react-app">create-react-app</a>, we will install the Steem.js dependency, and we will set up a basic React component that makes a call to the API and stores it in local state and can console.log the API data in the browser. Later on in this series we will incorporate Redux to the project and move away from using local state to manage the data, but for now let's start small and get comfortable making a call to the Steem.js API in a React environment. 

<br>
Go over to the <a href="https://github.com/facebook/create-react-app">create-react-app</a> Github page and follow the directions to get started. Install <b>create-react-app</b> by following the instructions on the Github page:

```
npx create-react-app my-app
cd my-app
npm start
```
<br>

Next navigate to the project folder and you can start the app by running `npm start`.  You should see the basic React project appear in the browser:
<br>
<img src="https://i.imgur.com/7rgodQ9.png" title="Basic React project" />
> Basic React Project
<br>
<hr>
<br>

<center><h3>Installing  Steem.js  in our  project</h3></center>
Next, go to the <a href="https://github.com/steemit/steem-js">Steem.js</a> Github page and install the API.
<br>
```
npm install steem --save
```
<br>

This installs the Steem.js API and adds it to the `package.json` file and allows us to work with it's functions in our project.  Now we can get started with making API calls to the Steem blockchain, but first we need to do a few things. 

First, import the Steem API into the project at the top of the `app.js` file:

```
import steem from 'steem';
```
<br>

After that is done we can remove the logo imported at the top and  all the boilerplate HTML that came with <b>create-react-project</b>. Our `app.js` file should now look like this: 

```
import React, { Component } from 'react';
import './App.css';
import steem from 'steem';

class App extends Component {
  render() {
    return (
      <div className="App">
        // Removed the boilerplate React HTML
      </div>
    );
  }
}

export default App;
```
<br>
<hr>
<br>
<center><h3>Adding state to app.js</h3></center>
<br>
Next let's add state to our component so we can store the data we are retrieving from the API. This is done by adding the following to our  `app.js`  component.
<br>
<br>

```
constructor(props) {
    super(props);

    this.state = {
      userData: []
    }
  }
```
<br>
<hr>
<br>

<center><h3>Using componentDidMount() to make the API request</h3></center>
Now that our component has state and has a place to store the data from the API we can get to the API request itself.  To do this our project will use a React <b>lifecycle method</b>. In this case we will be using <a href="https://reactjs.org/docs/react-component.html#componentdidmount">componentDidMount()</a> to make our API call.
<br>
```
componentDidMount() {
    // Retrieves and console.log data about the creators of Steemit
    steem.api.getAccounts(['ned', 'dan'], (err, result) => {
      console.log(err, result);
    });
  }
```
<br>

Inside the <b>componentDidMount()</b> function we call the Steem.js API and send out the request. In this example request from the documention we retrieve the Steemit profiles of the Steemit creators Ned Scott and Dan Larimer.  I have taken the function and converted it to an <b>ES6 arrow function</b>. 
<hr>
<br>
<center><h3>The user data returned from the API request</h3></center>
<br>
If you run `npm start` and view the project in a browser and go to your browser's dev tools you should now see the data being retrieved in JSON format.
<br>
<br>
<img src="https://i.imgur.com/LqJxKA7.png" title="Data retrieved from API" />

<br>
<hr>
<br>

As you can see the request succcessfully returns the data and it is simple to request basic information about user accounts. Try playing with the request and changing the strings within the array in the `steem.api.getAccounts` array to your own screen name and check out what it returns.

It should return a wealth of information within the JSON object like this:
<br>

<img src="https://i.imgur.com/oEIGu1c.png" title="Ned Scott's profile data" />
> Some of the properties in the returned user data

<br>
<hr>
<br>

As you can see the API returns a lot of data pertaining to user profiles and there is a lot we could potentially do with this. Combine this with the hundreds of other functions in the API and it's not hard to see the potential for projects you could build using Steem.js. Functions from the API cover everything from trending categories, topic tags, retrieving user comments and followers, and even topics pertaining to the economics of Steemit such as escrow, votes, payout, etc...
<br>
<hr>
<br>
<br>

<center><h3>Conclusion of Part 1 </h3></center>
In the coming years  and with the growth of Steemit.com and Busy.org and the Steem blockchain it wouldn't surprise me if social media sites all moved away from proprietary data and turned towards new solutions like blockchain based sites. APIs like Steem.js should lead to the growth in the number of front-ends for the Steem blockchain and hopefully developers will be creating new and exciting interfaces using these APIs.

I'm going to keep this first part of the series short and sweet. I hope this tutorial was concise and clear and that you got a taste of how to work with the Steem.js API in a React project.

In future tutorials I will be covering more advanced topics and we will be building a small app and incorporating Redux.
<br>
<hr>
<br>

<h3>Proof of work: </h3>https://github.com/Nicknyr/Steem.js_API_Tutorial
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authornicknyr
permlinktutorial-building-a-web-app-with-react-redux-and-the-official-steem-js-javscript-api
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","steemit","cryptocurrency","technology"],"image":["https://i.imgur.com/NSnwYTj.jpg","https://i.imgur.com/7rgodQ9.png","https://i.imgur.com/LqJxKA7.png","https://i.imgur.com/oEIGu1c.png"],"links":["https://github.com/facebook/react","https://github.com/steemit/steem-js","https://github.com/steemit/devportal","https://developers.steem.io/#introduction-welcome","https://nodejs.org/en/","https://www.npmjs.com/","https://en.wikipedia.org/wiki/Application_programming_interface","https://redux.js.org/","https://github.com/facebook/create-react-app","https://reactjs.org/docs/react-component.html#componentdidmount","https://github.com/Nicknyr/Steem.js_API_Tutorial"],"app":"steemit/0.1","format":"markdown"}
created2018-06-18 22:07:42
last_update2018-06-24 19:55:51
depth0
children6
last_payout2018-06-25 22:07:42
cashout_time1969-12-31 23:59:59
total_payout_value39.239 HBD
curator_payout_value12.750 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length8,172
author_reputation3,671,908,680,745
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,280,452
net_rshares24,638,319,152,136
author_curate_reward""
vote details (44)
@aneilpatel ·
$3.23
Nice introductory tutorial @nicknyr. Hope to see something more creative than simply querying the API in the next tutorials from you. There are already tutorials about React and SteemJs available, I would suggest you to keep each tutorial such that it produces some end product, for example you could have added some value to the tutorial by showing the response data in a react component on the web browser. 
Keep contributing to the open source community!
πŸ‘  , ,
properties (23)
authoraneilpatel
permlinkre-nicknyr-tutorial-building-a-web-app-with-react-redux-and-the-official-steem-js-javscript-api-20180619t144239700z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["nicknyr"],"app":"steemit/0.1"}
created2018-06-19 14:42:45
last_update2018-06-19 14:42:45
depth1
children2
last_payout2018-06-26 14:42:45
cashout_time1969-12-31 23:59:59
total_payout_value2.428 HBD
curator_payout_value0.805 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length457
author_reputation7,747,395,102,396
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,368,098
net_rshares1,532,480,592,262
author_curate_reward""
vote details (3)
@nicknyr ·
Yeah I hear ya. Just wanted to start small. Some of my past tutorials were criticized for being too long. The next tutorial is going to show React components with the data displayed.
properties (22)
authornicknyr
permlinkre-aneilpatel-re-nicknyr-tutorial-building-a-web-app-with-react-redux-and-the-official-steem-js-javscript-api-20180619t200815757z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-19 20:08:15
last_update2018-06-19 20:08:15
depth2
children0
last_payout2018-06-26 20:08:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length182
author_reputation3,671,908,680,745
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,402,754
net_rshares0
@utopian-io ·
Hey @aneilpatel
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

**Contributing on Utopian**
Learn how to contribute on <a href="https://join.utopian.io">our website</a>.

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for Utopian Witness!</a>
πŸ‘  
properties (23)
authorutopian-io
permlink20180621t081624202z
categoryutopian-io
json_metadata{"tags":["utopian.tip"],"app":"utopian-io"}
created2018-06-21 08:16:24
last_update2018-06-21 08:16:24
depth2
children0
last_payout2018-06-28 08:16:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length410
author_reputation152,955,367,999,756
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,607,224
net_rshares0
author_curate_reward""
vote details (1)
@utopian-io ·
Hey @nicknyr
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Contributing on Utopian**
Learn how to contribute on <a href='https://join.utopian.io'>our website</a> or by watching <a href='https://www.youtube.com/watch?v=8S1AtrzYY1Q'>this tutorial</a> on Youtube.

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-tutorial-building-a-web-app-with-react-redux-and-the-official-steem-js-javscript-api-20180621t114508z
categoryutopian-io
json_metadata"{"app": "beem/0.19.29"}"
created2018-06-21 11:45:09
last_update2018-06-21 11:45:09
depth1
children0
last_payout2018-06-28 11:45:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length504
author_reputation152,955,367,999,756
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,628,933
net_rshares0
@yokunjon · (edited)
$3.12
I thank you for your contribution. Here are my thoughts on your post;

* As @aneilpatel stated, it would be nice to see creative techniques. You achieved your curriculum's main goal as it was using steem.js in React. So, instead of showing every function of steem.js, you can show creative techniques which are special to React and can be used with steem.js.

* Explaining the functions those special to React would be nice. For example, you could explain what componentDidMount does by 2 sentences. I don't want you to write down the whole documentation here as a filler content. But, quick explanations would be nice. Be consider them next time, please.

----
Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/11221423).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  ,
properties (23)
authoryokunjon
permlinkre-nicknyr-tutorial-building-a-web-app-with-react-redux-and-the-official-steem-js-javscript-api-20180621t115611829z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["aneilpatel"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/11221423","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-21 11:56:09
last_update2018-06-21 11:58:03
depth1
children1
last_payout2018-06-28 11:56:09
cashout_time1969-12-31 23:59:59
total_payout_value2.339 HBD
curator_payout_value0.777 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,152
author_reputation19,266,807,595,513
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,630,026
net_rshares1,530,219,102,549
author_curate_reward""
vote details (2)
@utopian-io ·
Hey @yokunjon
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

**Contributing on Utopian**
Learn how to contribute on <a href="https://join.utopian.io">our website</a>.

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for Utopian Witness!</a>
πŸ‘  
properties (23)
authorutopian-io
permlink20180621t201521416z
categoryutopian-io
json_metadata{"tags":["utopian.tip"],"app":"utopian-io"}
created2018-06-21 20:15:21
last_update2018-06-21 20:15:21
depth2
children0
last_payout2018-06-28 20:15:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length408
author_reputation152,955,367,999,756
root_title"Tutorial: Building a web app with React, Redux, and the Steem Javscript API, PART 1"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,686,554
net_rshares2,192,049,138
author_curate_reward""
vote details (1)