What is Azure Bastion Service

Most of you might be familiar with RDP and SSH if you manage a remote server or virtual machine. Virtual machines and servers are remotely accessed using RDP and SSH. We can also use RDP and SSH to…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How to Update State in React

Learning how to update State is one of the learning curves in React. Despite React being a JavaScript library, it does not strictly follow the JavaScript way of updating values which in this article refers to state.

One of the rules of state management in React is not to mutate component states.

In this article, we will discuss how to update state in React without mutating the state. This article focuses on updating state values that are reference-based, such as arrays and objects, rather than primitives.

In this lesson, we will cover:

State Updates

Do not update arrays or objects directly.

Instead, there are special techniques to update arrays and objects by first creating a new array or object. Even though this does require a tiny, tiny bit of extra processing power, it allows React to do far less work when re-rendering a component.

Adding Element

You can add elements to the start of an array using the spread operator.

Add elements to the end of an array by reversing the order of elements in updatedColors.

Elements can be added at any index using the slice method available on all arrays.

Removing Elements

Elements can be removed from an array using the filter method.

Changing Objects

Objects in an array can be modified using the map function.

Changing Properties in Objects

Properties in an object can be modified using the spread syntax(…)

Removing Properties in An Object

Properties in an object can be removed using destructuring

State management as it regards updating, adding, and removing state in React is constant. It is something every React developer will do. As users interact with our application there are bound to be interface changes. State is what React uses to change its user interface based on certain actions carried out by the user of the application. We covered major ways of changing states and not following the anti-pattern of state management.

Add a comment

Related posts:

How i start to write stories

About some months ago it really needs a lot of time for me to decided that i need to starting writing, it is because i want to be a good writer. So at that time, i logged into Medium and write new…

Fatalismo Russo II

Passei o dia me sentindo como uma barata tonta — nunca senti empatia por baratas e não faço ideia se baratas têm sentimentos, mas foi assim que passei o dia. Tudo se desenrolou para que esse…

Traveling

Travelling is to move or undergo transmission from one place to another. It can be for multiple purposes as it can be a business journey, family or friends travel, and adventurous travel. Traveling…