Diary Of A Net Developer - Learning React
Tags: react, net, aspnetcore, webapi
Published: 2018-08-09 06:43:53

---
title: Diary Of A Net Developer - Learning React
published: true
description: Because .NET Developers can learn React too...
tags: react, .net, asp.net core, web api
canonical_url: https://jonhilton.net/diary-of-a-dot-net-developer-learning-react/
cover_image: https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-22-35-25.png
---


# React you say?

When Angular 1 first appeared on the scene it felt like a breath of fresh air.

Suddenly scenarios that were nigh on impossible using MVC (with a sprinkling of AJAX) were achievable; all you really had to do was reference the Angular script on your web pages and you were good to go.

Fast forward a few years and the javascript/front-end ecosystem has exploded.

Webpack, Typescript, React, Angular (what version is it now? 6?), Babel, Jasmine, Karma...

The list goes on and on.

And therein lies the problem.

If you're a C#/ASP.NET developer looking to dip your toe in these choppy waters then you're faced with an insurmountable mountain of choices and a learning curve that looks suspiciously like a brick wall.

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-22-35-25.png)

# Building something real

We've just started a new project at my work and after some experiments with different SPAs we've settled on React (using Typescript) for our next couple of projects.

My aim here is to simply document some of the key things we're picking up as we go along, in the hope it might help others following in our footsteps.

We ended up choosing React for three important reasons.

1. There is a large community to turn to for advice/articles and learning resources
2. It's easy to get started and build something without having to learn some of the peripheral details (like WebPack) from day one
3. You can build your app using Typescript, we've found the safety that comes from "compile-time" warnings and errors in VS Code invaluable as we fumble our way through these unknown waters

**Just before we continue, if you fancy a gander at the source code for this series [grab it here :-)](https://jonhilton.net/devto/react-source/)**

## Create React App

The project we're working on already has a fully-functional API so we're just building a front-end. For that reason the front-end project doesn't need to be an ASP.NET Core project.

If you're planning on running React and ASP.NET Core Web API in the same project then you'd probably want to use the [Microsoft template](https://docs.microsoft.com/en-us/aspnet/core/spa/react?view=aspnetcore-2.1&tabs=visual-studio) that lets you spin up just such a project.

So brushing aside ASP.NET Core means we can start off with a straight React project and then make API calls to our existing ASP.NET Core Web API.

There's a handy project called Create React App which will take care of most details for you and give you a simpler starting point for your project. It hides some of the details like WebPack away whilst you get started but leaves you free to get into those particular weeds at a later date if you really need/want to.

We elected to use the [Typescript version](https://github.com/wmonk/create-react-app-typescript).

So long as you've got a recent version of Node and Yarn installed (you can use NPM or Yarn, they are interchangeable) you can install **create-react-app** using this command.

``` ps
yarn global add create-react-app
```

This downloads the create-react-app dependency to a global directory on your machine so you can use it in any command prompt (irrespective of which folder you're currently in).

Once yarn's done its thing you can create a new React Typescript app with this command...

``` ps
create-react-app your-app-name --scripts-version=react-scripts-ts
```

Then change directory and use yarn to start the app...

``` ps
cd your-app-name
yarn start
```

If all goes well you'll see something like this...

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-20-51-39.png)

And this should appear in your browser.

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-20-52-07.png)

If you've tried creating any front-end apps recently (Angular, Aurelia etc.) then you're probably used to seeing starter projects looking pretty bloated from the get go.

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-21-23-49.png)

By those standards this is relatively lean...

## A first component

Keen to crack on from this point we eagerly set about building our first component.

Before we started working on the actual project we did a bit of research and actually found the [getting started tutorial over at reactjs.org](https://reactjs.org/tutorial/tutorial.html) to be really helpful to get some of the basic concepts in our minds before we started for real.

For our project we needed to build a simple list component, something like this.

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-21-17-05.png)

I've changed the details of what we were actually building so as to protect our client but this matches the structure of what we were aiming for.

So where to start?

Everything in a fresh create-react-app starts at the very top of the app, in src/index.tsx where we find this code.

``` ts
ReactDOM.render(
  <App />,
  document.getElementById('root') as HTMLElement
);
```

This instructs React to render the App component in an element on our page (#root).

To get going quickly we decided to ignore the existing App component referenced here (which simply renders the "Welcome to React" page we saw earlier) and create our own.

The "React" way to build applications is using components. With that in mind we took another look at our mockup and identified the key parts that might warrant their own component.

We settled on these components...

* My Users (the parent component)
* User Details Row
* Avatar (the image on each row)
* Tasks Button (the button on each row)

So we'd render the "My Users" component, which in turn would render one or more "User Details Rows" which would also include the Avatar and Tasks Button components.

Starting at the top we created this "My Users" component.

**src\MyUsers.tsx**

``` javascript
import * as React from "react";

export default class MyUsers extends React.Component<any, any>
{
    public render() {
        return (
            <div>
                <h1>My Users</h1>
                <div>
                    Each row to go here...
                </div>
            </div>
        );
    }
}
```

This is about as simple as we could get for starters.

By extending React.Component we get to create a render method which then renders the relevant markup for our component.

That funny looking "html" in the javascript is actually a kind of XML syntax (called JSX) which React then converts to javascript under the hood. In case you're interested, the underlying javascript looks like this...

``` javascript
return React.createElement(
    "div",
    null,
    React.createElement(
        "h1",
        null,
        "My Users"
    ),
    React.createElement(
        "div",
        null,
        "Each row to go here..."
    )
);
```

But in truth you generally don't need to worry about that and can stick to the handy JSX code above.


With that component in place we just needed to render it.

**src\Index.tsx**
``` javascript
import MyUsers from './MyUsers';

ReactDOM.render(
  <MyUsers />,
  document.getElementById('root') as HTMLElement
);
```
Gone is the reference to App and in its place is our MyUsers component.

**Handy Tip**

If you're using Visual Studio Code to work on your React App... 

When you reference something like `MyUsers` (see above) without bringing in a reference, VS Code will prompt you (with the lightbulb icon).

Select one of the options to automatically add the reference.

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-22-03-28.png)

Because we ran `yarn start` earlier, the changes to the app were automatically reflected in the browser meaning we could now review our work.

![](https://jonhilton.net/img/diary-of-a-net-developer-part-1/2018-05-16-22-06-28.png)

Granted we're not winning any design awards for this one but it's a solid start.

**Remember, if you fancy a gander at the source code for this series you can [grab it here :-)](https://jonhilton.net/devto/react-source/)**

[Next time](https://dev.to/jonhilt/learning-react---building-up-the-user-interface-using-components-and-dummy-data--106d) I'll cover how we built up the next few components and started plugging in some (initially fake) data.

*Note, this depiction of events is not entirely accurate; sequences shortened and cursing omitted ;-)*


*photo credit: WanderingtheWorld (www.ChrisFord.com) <a href="http://www.flickr.com/photos/44028103@N07/14212558927">'Vines', United States, New York, New York City</a> via <a href="http://photopin.com">photopin</a> <a href="https://creativecommons.org/licenses/by-nc/2.0/">(license)</a>*