DEV Community

Cover image for Creating a React App with Typescript + Tailwind Support
Ethan
Ethan

Posted on

 

Creating a React App with Typescript + Tailwind Support

Introduction

Hello! Here I will try to explain how to set up a React app with both Typescript and Tailwind.


Creating the React app with Typescript

This part is really simple, all you need to do is add the template option to create-react-app command.
Feel free to reaplace "app" with anything else.

Once installed simple cd into the directory.

npx create-react-app app --template typescript

cd app
Enter fullscreen mode Exit fullscreen mode

Adding Tailwind

Finally we need to add tailwind. First we need to install the needed modules

npm install -D tailwindcss postcss autoprefixer 
Enter fullscreen mode Exit fullscreen mode

Next we need to create the config files which can easily be done via the following command:

npx tailwindcss init -p
Enter fullscreen mode Exit fullscreen mode

Next open up the created "tailwind.config.js" file and add the following to "content":

content: [
    './src/**/*.{js,jsx,ts,tsx}',
],
Enter fullscreen mode Exit fullscreen mode

Next we need to add the Tailwind directives to the "src/index.css" file, add the following to the top of the file:

@tailwind base;
@tailwind components;
@tailwind utilities;
Enter fullscreen mode Exit fullscreen mode

Checking that it works

Now that we have set up Typescript and Tailwind we need to check to see if it works.
Open up "src/App.tsx" and change it to the following:

import React from 'react';
import './App.css';

function App() {
  return (
    <h1 className="text-3xl font-bold underline text-red-600">
      Simple React Typescript Tailwind Sample
    </h1>
  );  
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Done, now let's start the application.

npm start
Enter fullscreen mode Exit fullscreen mode

The browser should automatically open and display the index page. If all is okay the header should be bold, underlined and red. 😀

The source for this example can be found here:
https://github.com/ethand91/react-typescript-tailwind


Like me work? I post about a variety of topics, if you would like to see more please like and follow me.
Also I love coffee.

“Buy Me A Coffee”

Top comments (4)

Collapse
 
melodydev0521 profile image
James

This is great!

Collapse
 
ethand91 profile image
Ethan

Thanks ☺️

Collapse
 
svitanok profile image
Ivan • Edited

Thanks a lot!

Collapse
 
ethand91 profile image
Ethan

Your welcome :D

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!