What is immutability? And how does it fit in the React world?
When programming in React, one concept you may encounter is immutability (in contrast, mutability).
This is a controversial topic, but no matter how you think about the concept of immutability, React and the power of most of its ecosystem, you need to at least understand why it is so important and what it means.
In programming, if the value of a variable cannot be changed after creation, the variable is immutable.
You are already using immutable variables without knowing it when you manipulate strings. By default, strings are immutable, when you actually change them, you will create a new string and assign it to the same variable name.
Immutable variables can never be changed. To update its value, create a new variable.
The same is true for objects and arrays.
To add new items, not to change the array, but to create a new array by concatenating the old array and the new item.
The object is never updated, but it is copied before the object is changed.
This applies to React in many places.
For example, you should never change
stateThe direct properties of the component, but only through
inRedux, You will never change the state directly, but only through the reducer (ie function).
The question is, why?
There are many reasons, the most important of which are:
- Mutations can be centralized, such as Redux, which can improve debugging capabilities and reduce error sources.
- The code looks more concise and easy to understand. You never expect a function to change certain values without your knowledge, which gives youPredictability. When a function does not change the object but only returns a new object, it is called a pure function.
Download mine for freeResponse Handbook
More response tutorials:
- A simple React application example: Get GitHub user information through API
- Build a simple counter with React
- VS Code setup for React development
- How to pass props to child components through React Router
- Create an application using Electron and React
- Tutorial: Create a spreadsheet with React
- Roadmap for learning React
- Learn how to use Redux
- Getting started with JSX
- Stylized components
- Introduction to Redux Saga
- Introduction to React Router
- Introduction to React
- Reaction component
- Virtual DOM
- Reaction event
- Reaction state
- Reaction props
- Reaction fragment
- React Context API
- Reaction PropTypes
- Reaction Concept: Declarative
- React: How to display other components when clicked
- How to loop inside React JSX
- Props and status in React
- Should you use jQuery or React?
- Gatsby Introduction
- How to reference DOM elements in React
- One-way data flow in React
- React high-end components
- React to life cycle events
- Reaction concept: immutability
- Reaction concept: purity
- Introduction to React hooks
- Introduction to create-react-app
- Reaction concept: composition
- React: demo component and container component
- Code splitting in React
- Server-side rendering with React
- How to install React
- CSS in React
- Use SASS in React
- Processing forms in React
- Reaction strict mode
- Reaction portal
- React rendering props
- Test React components
- How to pass parameters to event handlers in React
- How to deal with errors in React
- How to return multiple elements in JSX
- Conditional rendering in React
- Reaction, how to transfer props to subcomponents
- How to get the value of an input element in React
- How to use useState React hook
- How to use useCallback React hook
- How to use useEffect React hook
- How to use useMemo React hook
- How to use useRef React hook
- How to use useContext React hook
- How to use useReducer React hook
- How to connect your React app to the backend of the same source
- Reaching the router tutorial
- How to use React Developer Tools
- How to learn React
- How to debug a React application
- How to render HTML in React
- How to fix `dangerouslySetInnerHTML` does not match the error in React
- How can I solve the problem of React login form status and browser auto-fill
- How to configure HTTPS in React application on localhost
- How to fix the "Component cannot be updated while rendering other components" error in React
- Can I use React hooks within the conditions?
- Using useState with objects: how to update
- How to move in code blocks using React and Tailwind
- React, focus on an item in React when added to the DOM
- Response, edit text on doubleclick