A Development Environment for React.js – Setting up and Optimizing it

React and its associated libraries are growing at an astonishing pace. It’s not only being used to build complex user interfaces but also being implemented in online stores, e-commerce websites and other businesses to build fast and scalable apps. 

If you are just getting started with React.js, the first challenge is to get your development environment set up and ready for coding. This can be a little tricky as there are so many different tools, libraries, and frameworks that you need to work with together. In this article we will walk you through the process of setting up an optimal React development environment on macOS or Windows.

In this post, we will look at what React is, why it became so popular recently, how to set up a development environment for React and optimize it for your project.

1. Get the tools you need

To get started, you will need to install a few different tools. 

  • A text editor for writing code, like VS Code or Sublime Text.
  • A browser for testing and debugging your application, like Chrome or Safari.
  • A code compiler that can translate your code from JavaScript into another language. 
  • A build tool that can generate the final code for your application. 
  • A package manager for installing the different tools you need for your application.

This depends on a number of things – your project type, the OS you are working with, the software versions of the tools you are using, etc.

2. Install Node.js and create an npm script

Next, you will need to install Node.js. Visit the official link and download to install. The next step is to set up a React Boilerplate. Setting up the React environment depends on the Node version. 

For old versions of Node and Boilerplate (before 8.10 and 5.6 respectively)

Install Boilerplate globally. To do so, run the following command in the terminal or cmd prompt.

npm install -g create-react-app

If successful, the terminal should show the following output.

For versions of Node and Boilerplate (8.10 and 5.6 and after respectively), the machine uses the latest features of Javascript. Run the command given below.

npx create-react-app my-app

The output should be as given below, thus creating the app name my-app. This can be changed according to your preference. However, this will also be used and be displayed in all future steps.

Running the project requires the following commands as shown in the output above.

cd my-app

npm start

The output will be as shown in the terminal snapshot below.

The App should now be viewable in the browser, as shown.

3. Set up a React development environment with Create-React-App

  • Create-React-app is a tool that can set up a new React development environment for you, including all the necessary Node modules, development server, and build configuration. 
  • Once you have installed create-react-app and run the create-react-app command, it will ask you a couple of questions, after which it will create a new React development environment. 
  • The create-react-app command is also a npm script.

The command is: 

create-react-app myapp

The above command should create a directory my-app in your current directory with all the necessary files required to run a React App. A snapshot of the directory so created is as given below.

The significant files here are index.html and index.js. The index.html file has a div element (id=”root”), which houses all the steps rendered and the React code will be stored in the index.js file.

4. Starting the Development Server:

To do that, go into the current directory (myapp in this case) and execute the following command line.

npm start

The following message should come up on your compiler.

You can go to the URL shown above to see the changes you are making in your App getting reflected. By default, the above URL will show the below page:

5. Optimizing your Development Environment

Debugging 

When you’re working in a development environment, you can easily debug your application using Chrome. 

  • You can debug your application using the built-in debugger in Chrome, which allows you to set breakpoints, view variable values, and step through your code.
  • To use the debugger, you need to enable the developer tools in Chrome. 
  • You can also use the debugger with a remote debugger, which allows you to run the debugger on a separate computer, like your laptop.

Optimizing Your Environment for React Development

While your development environment is ready, you can also optimize it for React development by taking a few extra steps. 

  • The first thing you need to do is install the React development tools. The React development tools provide several features, including automatic refreshing of your application while you are making changes and live reloading your application when you make a change. 
  • Another thing you need to do is install the ESLint code linter for React – The ESLint code linter can find common mistakes and errors in your code, like missing semicolons or using incorrect syntax. 
  • The ESLint code linter also provides suggestions for improving your code, like using a better naming convention or applying a best practice.

Conclusion

React gives you the ability to create reusable UI components that can be used across different parts of your application. With React, you can build your application using these reusable components, which are faster to create, easier to maintain, and easier to understand. Setting up your development environment is the first step towards creating applications with React. When you are done setting up your environment, you can continue with learning how to build your application with React.

 

 

React optimization tips for improved performance

Optimizing application performance is a critical requirement for developers. Delivering a positive user experience is essential and determines the app’s success. In research, Akamai, the world’s leading CDN service with clients like The Washington Post, Flipkart, and Netflix, found that a one-second delay in application load time can result in a 7% reduction in conversions.

If your application is built on ReactJS, fortunately, there are several optimization techniques you can implement to accelerate the performance. In this guide, we elaborate on tips for using React optimization to keep performance high as your app scales.

1. Keep component state local where necessary

In ReactJS, a state update in the parent component re-renders the parent and all its child components. This is why you must ensure that the re-rendering of the components happens only when necessary.

The easiest way to achieve this is to separate the code that handles component states and make it local.

Here’s the example code:

import { useState } from “react”;

export default function App() {

  return (

   <div>

    <FormInput />

    <ChildComponent />

   </div>

  );

}

function FormInput() {

  const [input, setInput] = useState(“”);

  return (

   <div>

    <input    

type=”text”    

value={input}

onChange={(e) => setInput(e.target.value)} 

/>

    <h3>Input text: {input}</h3>

   </div>

  );

}

function ChildComponent() {

console.log(“child component is rendering”);

return <div>This is child component.</div>;

}

Only the input field is responsible for the component state in this code. We separate the input and the state into a FormInput component, making it a sibling to the ChildComponent. Now when the FormInput component state changes, only the component re-renders.

While there will be some instances where you cannot avoid the global component state, this method can still significantly optimize ReactJS performance.

2. Memoizing React components

Memoization is a React optimization technique that caches a component-rendered operation to save the result at the moment. Then for the same input at other instances, it serves the results from the cache memory. It’s the same as for any different caching technique, but with React, it works better as it is integrated at the code level.

Let’s understand this React optimization technique with this code:

import { useState } from “react”;

export default function App() {

  const [input, setInput] = useState(“”);

  const [count, setCount] = useState(0);

  return (

   <div>

    <input    

type=”text”

     value={input}

     onChange={(e) => setInput(e.target.value)}

    />

    <button onClick={() => setCount(count + 1)}>Increment counter</button>

    <h3>Input text: {input}</h3>

    <h3>Count: {count}</h3>

    <hr />   

<ChildComponent count={count} />

   </div>

  );

}

function ChildComponent({ count }) {

console.log(“child component is rendering”);

  return (

   <div>

    <h2>This is a child component.</h2>

    <h4>Count: {count}</h4>

   </div>

  );

}

Here the input field update re-renders both the App component and ChildComponent. But we only want the ChildComponent to re-render because it is responsible for updating the UI. So, we memoize the ChildComponent to optimize the app’s performance.

3. Using React.memo()

React.memo is one of the higher-order components you can use in your ReactJS app to wrap a purely functional component to prevent its re-rendering if the props received in that component never change.

Here’s a syntax example to achieve this:

import React, { useState } from “react”;

// …

const ChildComponent = React.memo(function ChildComponent({ count }) {

console.log(“child component is rendering”);

  return (

   <div>

    <h2>This is a child component.</h2>

    <h4>Count: {count}</h4>

   </div>

  );

});

If the count prop never changes here, React will skip rendering the ChildComponent to reuse the previously rendered result. This can significantly improve React performance.

React.memo() is a good React optimization technique to be used with primitive values, such as a number in our example. Primitive values are always referentially equal and return true if their value never changes. While non-primitive values like objects, including arrays and functions, always return false because they point to different spaces in memory between re-renders.

Here is an example code passing a function to the child component: 

import React, { useState } from “react”;

export default function App() {

  // …

  const incrementCount = () => setCount(count + 1);

  return (

   <div>

    {/* … */}   

<ChildComponent count={count} onClick={incrementCount} />

   </div>

  );

}

const ChildComponent = React.memo(function ChildComponent({ count, onClick }) {

  console.log(“child component is rendering”);

  return (

   <div>

    {/* … */}

    <button onClick={onClick}>Increment</button>

    {/* … */}

   </div>

  );

});

In this code, the incrementCount function passed to the ChildComponent makes the component re-render when the function is redefined. To prevent regular redefining of the function, we can use a useCallback Hook that returns a memoized version of the callback between renders.

This will save memory and make the ReactJS app perform faster and more efficiently, avoiding unnecessary re-renders.

Conclusion

ReactJS is one of the most widely-used frontend development languages. It requires less code for building apps and can be scaled easily. And to keep the performance high when you are building scalable apps, we are sure these tips will help optimize the performance of your React application easily.

Talent500 is the platform for ReactJS developers to explore career-redefining opportunities with Fortune 500 companies and fast-growing startups. Sign up here to join our elite talent pool.

 

Using React with Rails: Things to consider

Frontend development has come a long way in the last decade. The domain is rapidly developing with new technologies to utilize modern infrastructure more efficiently. Of all the technologies, React remains a significant player. According to Statista, ReactJS is the second most widely used JavaScript library, with a market share of 42.62%.

Developers are using ReactJS to make their Ruby on Rails applications more powerful. This article will look at things you need to consider while using React with Rails.

When to use React and Rails?

Ruby on Rails and React are both standalone frameworks. You can build applications using either Rails or ReactJS, but there are scenarios where combining the two offers a technological edge. If you are not sure when to use React with Rails, here are some preferred applications:

  • Complex simple-page applications
  • Applications with a large volume of dynamic content
  • Mobile-first applications that need to be scaled rapidly
  • Applications handling larger database
  • Applications with higher performance speed

If you are working on any such application, then you can plan to use the two technologies in combination.

Now let’s look at the considerations to make.

1. Is fast development possible?

Ruby on Rails has many built-in modules and code libraries that developers can use to rapidly build enterprise applications. There are also options to strengthen communication with frontend libraries like ReactJS. However, to reduce cost and development time, you need to ensure that developers in your team understand how to build a Ruby on Rails app with a ReactJS frontend. Some prerequisites exist to fully utilize the combination for product development, like understanding protocols to transfer data between Rails backend and React frontend.

Also, there are tricks to clean up your React components that can make your apps much faster. It helps you scale the performance of the applications without increasing the resources.

2. What is your app speed requirements? 

Not every app benefits from using React and Ruby on Rails as the tech stack. If your app has to handle a high volume of dynamic data or too much traffic, you can build your app using any one of the frameworks.

However, it’s the best tech stack if your app needs to load lightning fast, even with dynamic content, and handle increasing traffic. When you use Ruby on Rails with React, you can reduce the server response time by up to 80%, making your apps much faster. The low speed of web apps is one of the main reasons businesses lose users.

If minimizing the server request time is your priority, you should consider Rails and ReactJS for your application.

3. Cut down memory usage 

It is a common issue for applications as they scale. If not managed correctly, increased memory usage can lead to poor performance and even timeouts. The solution lies in optimizing the memory utilization by the app. React with Rails API can be used to prevent unnecessary memory usage by optimizing processes to allocate and empty available space more efficiently. Egghead.io, an online learning platform for web developers, struggled with timeouts every hour before they decided to deploy a React with Ruby on Rails solution. It significantly improved their application’s performance and improved memory usage.

4. Development speed, quality & business model

App development is increasingly popular, driven by the increase in the use of smartphones. The ‘time to market’ is significant in a competitive domain. It simply determines how much time it will take to build and publish an application. When Rails is combined with React, you have a complete technology stack to develop and deploy apps in the least time possible. Also, if you aim to offer an app based on the freemium model, Rails works great because it’s a cheaper technology. Also, it provides additional features like scalability once your user base starts growing.

Another reason to try a combination of React with Ruby on Rails is that you can build a fully functional prototype fast. It helps you test the market and get feedback from the users to guide appropriate future decisions. However, if you aim to earn revenues from ads with a huge base of free users, you should not use Rails with React as it is not entirely a free option.

Conclusion 

Whether or not you should use React with Ruby on Rails depends on the project’s requirements. There is no straightforward answer, as the tech stack is not a universal solution but a potential combination for developing dynamic applications with innumerable micro-interactions. If you are building large scalable web applications, you need to consider the points mentioned above.

Talent500 offers app developers a platform to connect with Fortune 500 companies and fast-growing global startups. Join our elite pool of talented developers and get hired by the best tech companies by signing up here.