3 React traps you should be wary of as a frontend developer

React is arguably the most used JavaScript framework for frontend development. It offers opportunities and options to developers that other frontend frameworks fail to deliver. For instance, React provides an easy way to handle DOM API which is usually abstracted beneath interfaces making it difficult to interact with them directly. This JavaScript framework provides developers with a virtual browser that is much more developer-friendly than real browsers. The virtual browser acts as the agent between developers and the actual browser.

React is developer-friendly in many more ways, but this article is on some common language pitfalls that developers should avoid. If you are new to React, we recommend you familiarize yourself with these React traps to avoid them easily.

Let’s see what frontend developers must know about these traps.

1. Empty data will still display 0

As a React developer, you must have written code to pull data from the server and display it as a list on the front end. If the data is empty then ideally, it must not be displayed on the screen:

Example code:

const App = () => {

  const [list, setList] = React.useState([]);

  // fetch data …

  return (

   list.length && (

    <div className=”name-list-container”>

    {list.map((name) => {

      return <div className=”name-list-item”>{name}</div>;

     })}

    </div>

   )

  );

};

However, the output of this code will display 0 when the list is an empty array. It can leave you scratching your head as to why it is doing so.

It is not a bug, but a default behavior in React caused by the operating mechanism of JavaScript itself. According to MDN docs, “in JavaScript logical AND (&&) operator (logical conjunction) for a set of boolean operands will be true if and only if all the operands are true. Otherwise, it will be false.

The AND operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.”

For example:

const a = 0;

const b = “fatfish”;

const c = 1;

const d = “medium”;

Output:

console.log(a && b); // 0

console.log(c && d); // medium

However, there are several ways to overcome this React trap. Here are some examples of how you can solve the problem by turning a into a Boolean using ternary expressions.

  1. Convert list.length to boolean

!!list.length && <Component list={list} />;

  1. Use ternary expressions and null

list.length ? <Component list={list} /> : null;

  1. Controlled by a specific logic

list.length >= 1 && <Component list={list} />;

2. Get tricked by the use of “||” and “&&” 

If you are using both AND “&&” and OR “||” operators in a statement, you have to be careful because most React developers mess up in such scenarios.

Let’s consider this code:

const App = (props) => {

  return (

   props.name || props.name2 && <div className=”user-info”>fatfish</div>

  )

}

ReactDOM.render(<App name=”medium” />, document.getElementById(‘app’))

We want to show “fatfish” when the property name or name2 is passed a value. However, the outcome will be different because the code is not working as expected.

Why?

In JavaScript, the && operator has a higher priority, which is why the above code acts like this:

const App = (props) => {

  return (

   props.name || (props.name2 && <div className=”user-info”>fatfish</div>)

  )

}

ReactDOM.render(<App name=”medium” />, document.getElementById(‘app’))

The right way to display “fatfish” when a value is passed to name or name2 is using the following code:

const App = (props) => {

  return (

   (props.name || props.name2) && <div className=”user-info”>fatfish</div>

  )

}

ReactDOM.render(<App name=”medium” />, document.getElementById(‘app’))

3. Using nested multi-layer ternary expressions

React might be robust but be careful if you use multiple ternary expressions nested in your React app. It might very well be a nightmare for you and any other developer. Such nested code is hard to read and debug.

For instance, here’s nested multi-layer ternary React code:

{

  isUserA ? (

   <ComponentA />

  ) : isUserB ? (

   <ComponentB />

  ) : (

   isUserC ? <ComponentC /> : null

  );

}

If you write React code like this, you need to change your style right now.

You can simplify the code with the use of an if-else.

Here’s the above code without nesting:

const renderCompnent = () => {

  let component = null

  if (isUserA) {

   component = <ComponentA />

  } else if (isUserB) {

   component = <ComponentB />

  } else if (isUserC) {

   component = <ComponentC />

  }

  return component

}

As you can see, with nested ternary, React code becomes easy to read. It is a skill that will serve you well in your career as your code will be easy to maintain and debug.

Conclusion 

React is here to stay, and so is JavaScript. You can stand out by mastering the nuisances of React framework that give other developers nightmares. We hope these three commonly faced React traps won’t bother you anymore.

Talent500 is the remote team-building platform Fortune 500 companies use to hire engineering talent. Join our elite pool of talent, and sign up here.

 

5 Reasons why PHP is still so important for web development

For over 25 years, PHP has been one of the most popular server-side languages predominantly used for web development. Interestingly, whenever a new development language in the web domain emerges, chatter starts about whether PHP is relevant or not anymore.

While it is true that PHP has seen a drop in popularity over the recent years (dropping from the 5th position in 2017 to 8th in 2020 in Stack Overflow annual developer survey), it’s far from ‘dead.’ Even today, nearly 80% of all websites are created using PHP in its vanilla form or through frameworks like Laravel, CodeIgniter, and WordPress.

There are many reasons to use PHP for web development, which we will explore in this article.

1. PHP is free and open-source

PHP makes web development extremely economical as a server-side language because it’s an open-source language available for free. As a web developer, you must provide clients with as many options as possible on the economic front. If you are proficient in PHP, you can offer your services for a website, e-commerce store, or any web application development at a lower cost. As an open-source language that has been around for a quarter of a century, PHP has a vast community that ensures that timely updates are made to the programming language such that it is in sync with the latest requirements of web development. The availability of a highly active community of PHP developers ensures that you always have assistance and help available whenever you are stuck with the project.

2. Compatible with all OSes

PHP also enjoys compatibility with all the available operating systems and web development technologies. You can use PHP on Windows, Linux, UNIX, or Mac. While modern web development technologies like Swift for Mac or .Net for Windows have advantages, they are not as robust as PHP. Compatibility with all operating systems implies you can deploy your PHP application or website on any web server. Furthermore, the programming language integrates perfectly with MySQL and Apache servers. As modern web development is about cross-platform compatibility, PHP is an ideal language. If you are building an application that will run on multiple platforms, choose PHP, and you don’t have to worry about what operating system is running on the server anymore.

3. Dynamic, flexible, and secure

Modern web development projects must be secured in the wake of more sophisticated cyber-attacks. One advantage of using PHP for your web applications is its fool-proof encryption. Its encryption capability and scalability make PHP an extraordinarily dynamic and flexible web development technology for any project. PHP scripts can auto-load web apps and websites without any manual intervention. Using PHP, you can script your environment to install security updates automatically. Due to its open-source nature, several encryption libraries are available, like PrivateBinphp-encryption, and Libsodium, which you can directly import your project.

4. Robust PHP frameworks

Modern websites and applications are hardly built from scratch. The availability of frameworks accelerates the development process. PHP has one of the largest collections of frameworks used for web development. That is why almost 80% of websites are built using PHP. You can choose any of the available PHP frameworks to create and launch web apps quickly and easily. These modern PHP frameworks support the MVC (Model View Controller) architecture, which expedites coding and enables developers to create efficient and top-notch solutions. Laravel, CodeIgniter, Cake PHP, Symfony, Zend Framework, and Yii are all PHP frameworks. 

To give you an idea of the popularity of these frameworks, some of the most popular websites in the world use PHP frameworks. This includes Facebook, WordPress, Tumblr, Wikipedia, Slack, and Canva.

A notable mention is Canva, an Australian graphic design platform introduced in 2013. Today, it is one of the most popular online platforms for graphics design. It chose PHP as its server-side language, which indicates that even modern enterprise-level websites use PHP.

5. Database flexibility 

Web applications today consume and produce data in terabytes; that’s a lot. To empower such websites and applications, you need scalable databases that support modern technological infrastructure. MySQL, MongoDB, PostgreSQL, Big Data, etc., are some of the most popular current databases, and PHP is compatible with every single one. It is a highly flexible language in database connectivity, allowing you to use any database in your web application. Another advantage of this flexibility is that you can migrate your web application to another database without worrying about compatibility issues.

Conclusion 

PHP is still one of the leading programming languages for web development. It offers the most cost-friendly, flexible, secure, and robust development options, whether it’s a website, e-commerce store, or any other web application. There are many reasons to use this feature-rich programming language, but we hope the above reasons will encourage you to learn PHP.

Talent500 is a global platform for Fortune 500 companies and fast-growing start-ups to hire PHP developers. Sign up today to get discovered by international companies.