Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use variables in ReactJS

Understanding Variables

Before we dive into how to use variables in ReactJS, let's first understand what variables are in the world of programming. In simple terms, a variable is like a container or a box where we store something. In programming, we use variables to store data that we can use later in our code. Just like you can store different things in containers, you can store different types of data in a variable, such as numbers, text (strings), and even complex structures like lists and objects.

Now, let's see how we can create and use variables in ReactJS!

Creating Variables in ReactJS

In ReactJS, we create variables using the JavaScript let, const, and var keywords. Here's a simple code example:

let name = "John";
const age = 30;
var isStudent = false;

In this example, name, age, and isStudent are variables. We use these variables to store a string, a number, and a boolean respectively.

Using Variables in ReactJS

Once we've created variables, we can use them in our ReactJS components. Let's say we have a component that displays a person's name and age, and indicates whether they're a student. Here's how we could use our variables in this component:

function PersonInfo() {
  let name = "John";
  const age = 30;
  var isStudent = false;

  return (
    <div>
      <p>Name: {name}</p>
      <p>Age: {age}</p>
      <p>Student: {isStudent ? "Yes" : "No"}</p>
    </div>
  );
}

In this example, we use the curly braces {} to include the variables in the JSX code. JSX is a syntax extension for JavaScript that is used in ReactJS to describe what the UI should look like.

Variable Scope in ReactJS

In programming, scope refers to where a variable can be used. The scope of a variable in ReactJS depends on where it was declared:

  • If a variable is declared inside a function, it can only be used within that function. This is called a local variable.
  • If a variable is declared outside a function, it can be used anywhere in the code. This is called a global variable.

Let's look at an example:

let globalName = "John";

function PersonInfo() {
  let localName = "Doe";

  return (
    <div>
      <p>Global Name: {globalName}</p>
      <p>Local Name: {localName}</p>
    </div>
  );
}

In this example, globalName is a global variable and can be used anywhere in the code, while localName is a local variable and can only be used within the PersonInfo function.

Changing Variable Values in ReactJS

The value of a variable declared with let or var can be changed, while the value of a const variable cannot be changed. This is why const stands for constant. Here's an example:

let name = "John";
name = "Doe";  // This is allowed

const age = 30;
age = 31;  // This will cause an error

However, in a ReactJS component, changing the value of a variable doesn't cause the component to re-render. If you need the component to update when a variable's value changes, you should use a state variable instead.

State Variables in ReactJS

State variables are special variables that ReactJS tracks. When a state variable's value changes, ReactJS re-renders the component. We use the useState function to create a state variable:

function PersonInfo() {
  const [name, setName] = useState("John");

  return (
    <div>
      <p>Name: {name}</p>
      <button onClick={() => setName("Doe")}>Change Name</button>
    </div>
  );
}

In this example, name is a state variable. When the button is clicked, we call the setName function to change the value of name, which causes the PersonInfo component to re-render with the new name.

Conclusion

The ability to store and manipulate data is a fundamental aspect of programming, and variables provide us with a way to do this. In ReactJS, we can use regular JavaScript variables, but we also have access to state variables, which give us the ability to cause components to re-render whenever their values change. Just like a master chef knows when to use a saucepan or a skillet, a great ReactJS developer knows when to use a regular variable or a state variable. With practice and experimentation, you too can become a master of using variables in ReactJS!