Open In App

How to handle states of mutable data types?

Mutable parameters are those whose value can be modified within a function to which they are passed as a parameter. It means that when a parameter is passed to the function using the caller function, then its value is bound to the parameter in the called function, which means any changes done to the value in that function will also be reflected in the parameter of the caller function.

The state is mutable in react components. To make the React applications interactive we almost use state in every react component. State is initialized with some value and based on user interaction with the application we update the state of the component at some point of time using the setState method. If states of the React component initialized with an array or JavaScript object, It is always good and recommended practice to not change the state by modifying the old array or object itself but use some build-in JavaScript method like a map, filter, or use new JavaScript syntax like spread to return the newly updated state as a whole. 



Example 1: In this example, we will create a todo app, where the user will interact and store the things he or she wants to do, which is basically a mutable state.

Output:

Example 2: In this example, we will remove items from the created listed, to make it blank which is also a mutable state.

Output :


Article Tags :