JavaScript concepts you need to know to get started with React

Many beginners don’t realize that React is JavaScript library. It’s not a different language, and syntax differences are only cosmetic, so it’s easier to write web apps with React. Under the hood, all of the code is actually JavaScript.

That is why you need at least a basic knowledge of JavaScript to get started with React. I personally think React makes much more sense when you understand JavaScript on a deeper level, but that’s neither here nor there. If you are looking for the bare minimum of JavaScript you need to know, you’ve come to the right place.

Fundamental JavaScript concepts like variables and different types of values are obviously needed, so I won’t even mention them. Instead, I will talk about often misunderstood JavaScript features that are particularly important when developing apps with React.

JavaScript functions and ES6

Component reusability is a foundational concept in React. In this library, components can be of two types – functions and classes. Since the introduction of hooks, functions became the preferred way to write components in React. They have an easier syntax and also they are easier to read. Therefore you absolutely need to understand function declarations, and specifically how arrow functions work in React.

Event handlers, as usual, are also functions, so that’s another reason you need to know JavaScript functions really well.

One important thing about functional components is that they return JSX code. Normally, functions in JavaScript must return a valid value, so why do they return something that looks like HTML?

See, JSX is a template language for React. It may look like HTML, but that’s only to simplify building layouts with React. In reality, JSX is translated to calls to React’s top level API, like React.createElement(). Therefore it is 100% JavaScript. This fact gives React developers the freedom to embed dynamic expressions inside their JSX code and thus build interactive apps with React.

It’s also important to note that the names for functional components are usually capitalized. This is to distinguish components from normal elements, which look and work identical to their counterparts in HTML. For example, you can invoke the same <h1>, <p>, and other elements in React like you do in HTML.

But don’t get confused – both normal elements and custom components are actually JavaScript. Many libraries even provide custom components that allow you to declaratively perform features like redirect users to another page on button click.

Arrow functions are even shorter and have easier syntax. They are often used to define simple presentational components. Knowing the syntax to write arrow functions can be an important instrument for your growth in React.

Template literals

This relatively new JavaScript feature can be very useful in React. Instead of writing static strings, template literals allow you to create dynamic strings. Or at least strings with dynamic parts. This can be invaluable for setting conditional className values, conditional styles, and much more. You can also use template literals as a readable way to push separate strings together.

Template literals are marked by back ticks. If you want to combine a static and dynamic strong, or two dynamic strings, you can simply put them inside template literals. You do need to precede dynamic parts with a dollar sign ($) and curly braces. So a dynamic className value would look something like this:

<div className=`black ${border}`></div>

In this example, the ‘black’ string value will be always applied. The ‘border’ className is interpreted dynamically. Whatever the value of border variable is, it will take place of the variable. Note that you need dollar and curly braces to include this variable.

You can put anything between curly braces. It doesn’t have to be a variable. You can also put ternary operator to conditionally determine the className value.

React is main library for building interactive user interfaces, and template literals are one of the best features to implement dynamic features. This guide goes into great detail on doing string interpolation in React.

Logical operators

Once again, we use React for its dynamic features. Features like dynamic rendering and conditional styles require dynamic JavaScript features. Considering that JSX does not allow you to embed if/else or switch or any other JavaScript statements that require multiple lines of code, simple solutions like && logical operator or || logical operator come in handy. Like ternary operators, you can use them to do simple conditional rendering or any other dynamic feature.

Behavior of logical operators is quite confusing and difficult to understand. Make sure to carefully study how && logical operator works. Same for || logical operator.

AND logical connector checks if the first expression is true. If it is, then the logical operator proceeds to run the second expression. This could be useful if you want to invoke a component only if X condition is true.

These operators help keep your JavaScript and JSX code easy to follow.

JavaScript methods

JSX allows you to embed dynamic expressions into your component. However, you can not use multi-line statements like if or else. You can technically do these things outside of JSX, and then embed variable inside your JSX. But much more commonly you use JavaScript methods like map() and filter() to create elements and components based on data.

In practice, this is very useful as you will often receive data from API structured as an array of objects. map() takes values from each objects and transforms them into elements and components.

You will use these methods all the time. I recommend you practice all the time and if you get stuck, read React guides with examples.

Destructuring and Spread Operator

Essential features in React use destructuring. For example, the useState hook returns two values. You will need to destructure them to store the state variable and updater function in two different variables. Similarly, you can use spread operator when dealing with props, another essential feature in React.

 

Essential gear for a family on a cruise

In my experience, going on a cruise is one of the most fun things a family can do. These large ships have everything anyone could possibly want – from elaborate children’s playgrounds to highly rated restaurants to have a dinner and enjoy superb food.

Still, the quality of your cruising experience depends on your preparations. When packing, you should not forget essentials or you might not have as much fun as you would’ve otherwise. Without further ado, let’s get started.

Essential cruise gear for a family

I’d say first thing you should pack is appropriate clothing. Depending on what type of cruise you will choose, you should pack casual or official clothes, or mix of both. Cruises will often have various dining options and events that require official clothes, or can be done in casual clothes. If you want to do these types of events, then pack official clothes. Children will be okay in their normal clothes. Just don’t forget to bring swimsuits for everyone, otherwise you will miss out on numerous swimming pools cruise ships usually have on board. That includes several pairs for you and your partner, as well as children. Finally, if you want to have a little fun while on a cruise, I highly recommend picking out cruise t-shirts for families. You can find some funny and original designs that will make you stand out and elevate your experience. After the cruise is over, you can keep it as memory, or wear it once again if you decide to go on a cruise another time.

Next, you need to organize logistics. Fortunately, cruises are both the transport and destination, so you don’t have to buy lots of bus and train tickets to go from one place to another. Still, your cruise might be stopping at various countries and cities. So you may need to bring passport with you. In some cases, you will have to get passports for your kids, because they might not have one. You may also need to get different currencies to spend at different cities around the world. If the cruise destination is to Europe, you can simply get euros (in most cases, although some countries do not use euros). You can do this by contacting your bank to request foreign currency. You can also get a card to pay in that currency, or simply pay with your USD card and hope that exchange fees aren’t too high. Obviously, you should bring credit cards with you.

Some items like packing cubes can make it easier to organize your bags, so you can unpack and find everything once you’re on the ship. You can also get apple airtags and keep track of its whereabouts. Its also a good idea to bring any essential medication. Chances are, cruise ships will have some meds on board. But if you have very specific condition, make sure to stock up on essentials before sailing out to sea. Same goes for any favorite self-care items, like shampoos and conditioners that work well with your hair.

Create select dropdowns and set their default value in React

Every dynamic React application should give users some way to interact with the app. Most commonly we use standard input fields where users can enter anything. In this article, we will explore how to set up dropdowns so users can choose one of many provided options.

JSX is a templating language for React. In a lot of ways, it works exactly like HTML. Except one big difference – it’s actually JavaScript. Still, it allows you to create elements like <select> the same way you’d create them in standard HTML. At first glance, <select> element in React looks indistinguishable from the same element in HTML. One big difference is that JSX allows you to embed JavaScript expressions. You can do so by using curly braces {} around the JavaScript expression. This is necessary, so React knows what parts are static and which are dynamic.

How to set default value for <Select> elements in React

You can easily select one of the options by default. This is useful if you need to suggest one of the choices.

First, you need to set up a normal <select> in React. This involves multiple routine steps. Create a state variable and the function to update it. Then tie <select> element’s value to the state variable and set an onChange event handler. Then come up with a few options with unique value values. This may sound strange, but the value is an attribute for <option> element in React and HTML. Developers should set it to a unique value.

Next, simply add the defaultValue attribute to the <select> element to set its default value. You need to specify which option is going to be selected by default. To specify this, simply set defaultValue to the value of one of the options.

You can also set a placeholder as the default option. It will allow you to not use a label. Usually, placeholders look better. Check this guide to see a live demo of <select> default value.

There are a number of libraries that provide custom functionality. Even though you don’t need a custom component to set a default value, you might still need it for other advanced features like multi-select.

These custom components accept options as a prop. You create an array of objects with two properties – label and value. Every object describes one option. Then you set options prop on the select element itself.

When dealing with custom components like these, you can set default values the same way you set options. Use the same attribute defaultValue, but this time you need to set it to an object that describes an option that should be the default.

Generally, I wouldn’t recommend using libraries when you don’t need them. However, sometimes building a certain feature takes too long. Still, you might learn something by building it. I’ve learned a lot by looking at interesting libraries and trying to build their features myself.