Intro

Two of my favourites things are React and dinosaurs. In this article I will show how I’ve put them together to develop a server side rendering React application with Deno.

Project Setup

I will assume that we are all familiar with React and Deno.

Now let’s start creating the project structure and the files needed for this tutorial, I’m using Visual Studio Code but any editor will do. Open your terminal and type:

mkdir deno-react-ssr && cd $_
code .

This will create a new folder called deno-react-ssr and will open it with vscode. In this folder we will need to create three files, app.tsx that will contain the code of the React component, server.tsx for the server code and deps.ts will contain all our dependencies. Think of it as our version of a package.json. You will end up with a structure like this:

.
├── app.tsx
├── deps.ts
└── server.tsx

Setting up the dependencies

In deps.ts we will have to export all the dependencies needed for this application to run. Copy the following code and add it to your file.

// @deno-types="https://deno.land/x/types/react/v16.13.1/react.d.ts"
import React from 'https://jspm.dev/react@16.13.1';
// @deno-types="https://deno.land/x/types/react-dom/v16.13.1/server.d.ts"
import ReactDOMServer from 'https://jspm.dev/react-dom@16.13.1/server';
export { React, ReactDOMServer }
export { Application, Context, Router } from 'https://deno.land/x/oak@v4.0.0/mod.ts';

As you can see, in Deno you import the modules directly from a url. I’ve decided to import React and ReactDOMServer from jspm as suggested in the documentation for third party modules but you can use any other CDN that provides the same modules.

One unusual thing that may stand out to you could be this:

// @deno-types="https://deno.land/x/types/react/v16.13.1/react.d.ts"

Since we are using typescript, this line of code will inform Deno of the location of the types it needs to import and will affect the import statement that follows. A more exhaustive explanation can be found in the Deno Type Hint manual.

I’ve also decided to use Oak, a middleware framework for Deno’s http server that also provides a router, so I’m importing all the modules we will use in the server in addition to the Context type that typescript requires.

Create your React component

This is how our app.tsx component will look:

import { React } from "./deps.ts";

const App = () => {
  const [count, setCount] = React.useState(0);

  const garden = {
    backgroundColor: 'green',
    height: 'auto',
    fontSize: '30px',
    maxWidth: '400px',
    padding: '20px 5px',
    width: '100%'
  };

  return (
    <div className="pure-g pure-u">
      <h2>My DenoReact App</h2>
      <button className="pure-button" onClick={() => setCount(count + 1)}>Add a 🦕 in your garden!</button>
      <p style={garden}>
      { Array(count).fill(<span>🦕</span>) }
      </p>
    </div>
  );
};

export default App;

As with any standard React component, we start by importing React from our deps.ts file.

Then we are going to declare our App component that uses hooks to implement a simple button counter that allows you to add as many dinosaurs as you want in your personal garden!

#deno #node #react #web-development #developer

How to Create a Server Side Rendering React App with Deno
35.25 GEEK