Usage of map in react: 1. Use "{this.state.ToDoList.map((item,index)=>{return
{item}})}" syntax loops to display an array ToDoList; 2. Use the array to call the "map()" method and return the desired content.
The operating environment of this tutorial: Windows 10 system, react version 18.0.0, Dell G3 computer.
What is the usage of map in react?
The use of the map() method in React and the binding of key values.
1. Here is an example of displaying an array ToDoList in a loop.
import React, { Component } from 'react' export default class App extends Component { constructor(props) { super(props) this.state = { ToDoList:[1,2] } } render() { return ( <div> <input></input><button>提交</button> <ul> {this.state.ToDoList.map((item,index)=>{ return <li key={index}>{item}</li> })} </ul> </div> ) } }
2. Call the map() method directly with an array, return the content you want, and bind the key value.
<ul> {this.state.ToDoList.map((item,index)=>{ return <li key={index}>{item}</li> })} </ul>
2.1 For convenience, the bound key value here is the subscript of the array. This is not recommended in actual use, because when the Diff algorithm in React performs component comparison, if the key called is the array subscript, Changes to the array will cause the array content corresponding to the subscript to be incorrect. For example,
[1,2,3] will become [2,3] after deleting 1, and
will be originally subscripted. Subscript 0 corresponds to 1, subscript 1 corresponds to 2, and subscript 2 corresponds to 3. After the change, subscript 0 corresponds to 2, and subscript 1 corresponds to 3. Obviously this will cause trouble for the Diff algorithm when comparing two virtual DOMs. Because the corresponding content is different when the id is the same, and the meaning of the id is lost. Therefore, it is not recommended to use array subscripts as key values. This is written based on my own understanding. There may be some misunderstandings. Anyway, this is the general meaning. Please correct me if there are any mistakes.
3. The running result is as shown below
Recommended learning: "react video tutorial"
The above is the detailed content of What is the usage of map in react. For more information, please follow other related articles on the PHP Chinese website!

Using ID selectors is not inherently bad in CSS, but should be used with caution. 1) ID selector is suitable for unique elements or JavaScript hooks. 2) For general styles, class selectors should be used as they are more flexible and maintainable. By balancing the use of ID and class, a more robust and efficient CSS architecture can be implemented.

HTML5'sgoalsin2024focusonrefinementandoptimization,notnewfeatures.1)Enhanceperformanceandefficiencythroughoptimizedrendering.2)Improveaccessibilitywithrefinedattributesandelements.3)Addresssecurityconcerns,particularlyXSS,withwiderCSPadoption.4)Ensur

HTML5aimedtoimprovewebdevelopmentinfourkeyareas:1)Multimediasupport,2)Semanticstructure,3)Formcapabilities,and4)Offlineandstorageoptions.1)HTML5introducedandelements,simplifyingmediaembeddingandenhancinguserexperience.2)Newsemanticelementslikeandimpr

IDsshouldbeusedforJavaScripthooks,whileclassesarebetterforstyling.1)Useclassesforstylingtoallowforeasierreuseandavoidspecificityissues.2)UseIDsforJavaScripthookstouniquelyidentifyelements.3)Avoiddeepnestingtokeepselectorssimpleandimproveperformance.4

Classselectorsareversatileandreusable,whileidselectorsareuniqueandspecific.1)Useclassselectors(denotedby.)forstylingmultipleelementswithsharedcharacteristics.2)Useidselectors(denotedby#)forstylinguniqueelementsonapage.Classselectorsoffermoreflexibili

IDsareuniqueidentifiersforsingleelements,whileclassesstylemultipleelements.1)UseIDsforuniqueelementsandJavaScripthooks.2)Useclassesforreusable,flexiblestylingacrossmultipleelements.

Using a class-only selector can improve code reusability and maintainability, but requires managing class names and priorities. 1. Improve reusability and flexibility, 2. Combining multiple classes to create complex styles, 3. It may lead to lengthy class names and priorities, 4. The performance impact is small, 5. Follow best practices such as concise naming and usage conventions.

ID and class selectors are used in CSS for unique and multi-element style settings respectively. 1. The ID selector (#) is suitable for a single element, such as a specific navigation menu. 2.Class selector (.) is used for multiple elements, such as unified button style. IDs should be used with caution, avoid excessive specificity, and prioritize class for improved style reusability and flexibility.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
