Pages

Showing posts with label create react app. Show all posts
Showing posts with label create react app. Show all posts

How to Install ReactJS on Ubuntu?

Steps to Install ReactJS on Ubuntu 20.04 and Ubuntu 18.04.

ReactJS is a JavaScript library used for creating interactive user interfaces. It is maintained by Facebook and a community of individual developers and companies. Create simple views for each state in your app, and when your data changes, React will quickly update and render just the right parts. 


When compared to its competitors, Angular and Vue JS, React is far superior. Its adaptability and ease of use have made it the go-to for creating both mobile and web-based applications. It's estimated that over 90,000 websites, including some of the biggest names in technology like Facebook, Netflix, Instagram, Airbnb, and Twitter, use React.


This article explains how to set up ReactJS on both Ubuntu 20.04 and Ubuntu 18.04.


Step 1: Installing NPM 

First of all we have to set npm "node package manager", which is a prerequisite for installing React on Ubuntu. 


To begin with, it's a command-line tool for working with Javascript packages, which means it can be used to set up and maintain a variety of different Javascript-based resources.


More than 800,000 Node.JS packages can be downloaded from npm, which is an online database of open-source software. Npm is free and easy to use. It gives you quick access to thousands of software packages that are available to the public.


Ubuntu Linux users can install npm by logging in as a sudo user and running the following command:


Command:

$ sudo apt install npm


Once the installation is done, you can use the command to check the version of npm that was installed:


Output: 

$ npm --version




Installing npm will also install node.js, and you can check the version of node that was installed with the following command:


Output:

$ node --version




Step 2: Installing create-react-app Utility

The create-react-app program is a tool that sets up your development environment so that you can make a React application. It's much more efficient than starting from scratch, saving you both time and effort.


Simply enter the following npm command into your terminal to install the tool:


$ sudo npm -g install create-react-app


After installation, you can verify the software's version by running the following command:


$ create-react-app --version


Output:



Step 3: Create & Launch Your First React App


The process of making a React app is very easy and uncomplicated. Using the steps below, we can build a React app we'll call tecmint-app.


$ create-react-app tecmint-app


It will hardly take five minutes to install all of the packages, libraries, and tools that are needed for the app. 




Once the app creation is successful, you will get a notification with the basic commands. You can run more commands to start managing the application.



Now, navigate into the app directory to run the application. Enter the below commands to see how to access the application on the browser


Command:

$ cd tecmint-app


$ npm start


The output below demonstrates how to access the application on browser.




Open the browser and look up the IP address of your server.


http://server-ip:3000




The above screen shows that the default React app is running successfully on Ubuntu. 


Final thoughts: 

Once you have completed the steps outlined above, you should now have successfully installed ReactJS and create react app on your Ubuntu. You can now begin building and deploying more React app on your Ubuntu device. You can make great user interfaces and web apps with the power of React at your disposal.


How to create a Simple App With React.js?

 How to Create a Simple App With React.js?

While it's true that React is ideal for some projects (like making attractive websites and launching web applications rapidly), it's not necessarily the best choice for designing a video gaming app.


Video games are complicated programs that need hard-core coding to show a lot of different parts that can be used again and again on the page. React is a great choice for making a gaming app because it lets developers make user interfaces that are easy to understand and fun to use. But React adds less value when building a game where the logic mainly lives inside one component. That is why developers prefer not to use React when designing a video game. 


Now that you have all the basic knowledge of React, let’s dig a little deeper and know the steps for building a simple React application. 


Prior to moving ahead, let's do some preparation and set up a development environment so that you can use the latest JavaScript features more conveniently. JavaScript features provide a nice developer experience and optimize your app for production. 


You must have Node >= 14.0.0 and npm >= 5.6 installed on your computer to make a react project run:


Step 1. Install Create-React-app on your terminal

Please enter the following command into your terminal. By doing this, the current folder will gain a new directory called "my-app."


The command creates an initial structure for the project and installs its dependencies in the directory that was just made.

Among all the folders created, your Javascript code will live in src.


npm init react-app my-app


Step 2. Start up the React.js app

To start making the app, go to the directory that was just made (my-app) and run npm start to start your application.


Your browser displays an HTML page similar to the one below. 


Create react app npm


cd my-app

npm start



Image 1:


As soon as you start your application, below mentioned information appears in the command line:


Image 2:


Step 3: Creating API

In the third step, you have to open the code in a code editor. 


Open the ./src folder in my-app directory and you will see a file named App.js.


When the application first launches, this file defaults to the HTML code displayed below, which is then displayed on the homepage.



import React from 'react';

import logo from './logo.svg';

import './App.css';

function App() {

  return (

    <div className="App">

      <header className="App-header">

        <img src={logo} className="App-logo" alt="logo" />

        <p>

          Edit <code>src/App.js</code> and save to reload.

        </p>

        <a

          className="App-link"

          href="https://reactjs.org"

          target="_blank"

          rel="noopener noreferrer"

        >

          Learn React

        </a>

      </header>

    </div>

  );

}

export default App;




Then in the above code, we remove the unwanted components like App.css and logo.svg. They are no longer needed. 


Let's start by altering the component to convert it into a class component, as this will be much more useful in our situation. We want our App.js component to look the way mentioned below:



import React, { Component } from 'react';

class App extends Component {

  render() {

    return (

      <p>Hello world!</p>

    );

  }

}

export default app.



In order to create a class component, we have to import the component from "React" into our program. Let’s add some custom Javascript code in the return () statement and reload the page to see the result!


Image 3:


Step 4: Create a local state and add a constructor 


Let's create a local state in which we will save the data we have obtained from the API.


Also, we have to add a constructor, along with a pass prop. After that, we'll set a local state with posts set to an empty array because we'll be getting some posts from the API.



import React, { Component } from 'react';


class App extends Component {

constructor(props) {

    super(props);

    this.state = {

      posts: []

    }

  }

render() {

    return (

      <p>Hello world!</p>

    );

  }

}

export default App;



Step 5: Add API

Now it's time to call the APIs. Here we are going to use a faked API from the JSONPlaceholder website (https://jsonplaceholder.typicode.com/). 


To get the data, I’ll use the .fetch method from Javascript. Let’s create a call:



import React, { Component } from 'react';


class App extends Component {

constructor(props) {

    super(props);

    this.state = {

      posts: []

    }

  }

componentDidMount() {

    const url = "https://jsonplaceholder.typicode.com/posts";

    fetch(url)

    .then(response => response.json())

    .then(json => this.setState({ posts: json }))

  }

render() {

    return (

      <p>Hello world!</p>

    );

  }

}

export default App;




We have fetched the data from the API. Make sure to have a look at the data format prior to displaying it as posts.


Step 6: Displaying the data and adding Bootstrap CDN

We have already fetched the data from the API. It's time to take a look at them and see how they will look to others.

To make it good-looking without any styling, we have to add Bootstrap CDN to the application. 


We get a CDN From the bootstrap website and place it in ./public/index.html file <head> section.


....

    <!-- Bootstrap -->

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>React App</title>

  </head>

  <body>

....



Now, we have to use Bootstrap and create a JSX code for our data.


We will add a container, header, and a card.


....

render() {

    return (

      <div className="container">

        <div class="jumbotron">

          <h1 class="display-4">Blog posts</h1>

        </div>

        <div className="card">

          <div className="card-header">

            Featured

          </div>

          <div className="card-body">

            <h5 className="card-title">Special title treatment</h5>

            <p className="card-text">With supporting text below as a natural lead-in to additional content.</p>

            <a href="#" className="btn btn-primary">Go somewhere</a>

          </div>

        </div>

      </div>

    );

  }

...



Using the map() method, let's display the data as posts.



import React, { Component } from 'react';


class App extends Component {

  constructor(props) {

    super(props);

    this.state = {

      posts: []

    }

  }


  componentDidMount() {

    const url = "https://jsonplaceholder.typicode.com/posts";

    fetch(url)

    .then(response => response.json())

    .then(json => this.setState({ posts: json }))

  }


  render() {

    const { posts } = this.state;

    return (

      <div className="container">

        <div class="jumbotron">

          <h1 class="display-4">Blog posts</h1>

        </div>

        {posts.map((post) => (

          <div className="card" key={post.id}>

            <div className="card-header">

              #{post.id} {post.title}

            </div>

            <div className="card-body">

              <p className="card-text">{post.body}</p>

            </div>

          </div>

        ))}

      </div>

    );

  }

}

export default App;



Step 7: Add some styling

Our app is almost ready to use, and now we have to give it a final touch by adding some styling.


To make our blog posts look nice, let's import an App.css file once more and make some adjustments to the padding and margins.



.app {

  padding: 10px;

}


.app .jumbotron {

  text-align: center;

}


.app .card {

  margin-bottom: 10px;

}


.app .card-header {

  color: white;

  font-weight: bold;

}



Woohoo! The app is ready to use!


Final Thoughts: 

I hope these steps will help you create react app more easily and swiftly. Also, it will help you learn more about the framework and create something meaningful. It is important to take the time to understand the fundamentals of the react framework and the best practices that you should use when creating a React app. 


We have broken down the app development process into steps so that you get it easily and create a top-notch basic app for yourself. 


Also, this article will help you to have a clear plan of what the app will do and how it will work before you start coding. With a bit of patience and dedication, you can create a great app with React.js.


Make new Model/Controller/Migration in Laravel

  In this article, we have included steps to create a model and controller or resource controller with the help of command line(CLI). Here w...