We can use JavaScript’s Use We can reconstruct the above instance into a component that receives array parameters and assigns one for each list element Of an element When the element is not determined If the list can be reordered, we do not recommend using an index for sorting, as this can cause rendering to become slow. Of the element For example, if you extract a When you are in Used in array elements {post.content} <Post In the above example, we declare a separate JSX allows you to embed any expression in curly braces, so we can use the Doing so can sometimes make your code clearer, but sometimes this style can be abused. As in JavaScript, when you need to extract a variable for readability is entirely up to you. But remember, if a
In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress. map() method to create a list. 3.10.1. React instance ¶
map() method traverses the array to generate a list of numbers 1 to 5:constnumbers=[1,2,3,4,5];constlistItems=numbers.map((numbers)=>
<li>{numbers}li>);ReactDOM.render(<ul>{listItems}ul>,document.getElementById('example'));
key otherwise, there will be a warning a key should be provided for list items which means it needs to be included key : React instance ¶
functionNumberList(props){constnumbers=props.numbers;constlistItems=numbers.map((number)=>
<likey={number.toString()}>{number}li>);return(<ul>{listItems}ul>);}constnumbers=[1,2,3,4,5];ReactDOM.render(<NumberListnumbers={numbers}/>,document.getElementById('example'));
3.10.2. Keys ¶
Keys can help when some elements in the DOM are added or deleted React identify which elements have changed. So you should assign a definite identity to each element in the array. const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<li key={number.toString()}>
{number}
li>
);
key is preferable that this element has a unique string in the list. Usually, we use the id as an element key : const todoItems = todos.map((todo) =>
<li key={todo.id}>
{todo.text}
li>
);
id can use his serial number index index as key : const todoItems = todos.map((todo, index) =>
// Only used when there is no definite ID
<li key={index}>
{todo.text}
li>
);
3.10.3. Extract components with keys ¶
key it only makes sense when it is compared to its sibling node. ListItem components, you should put key the one saved in the array ListItem in the component on the element. 3.10.4. A false demonstration. ¶
function ListItem(props) {
const value = props.value;
return (
// Wrong! You don't need to specify a key here:
{listItems}
);
}
const numbers = [1, 2, 3, 4, 5];
ReactDOM.render(
3.10.5. The correct use of key ¶
React instance ¶
functionListItem(props){//By the way! There is no need to specify a key here:
return{listItems}
);}
constnumbers=[1,2,3,4,5];ReactDOM.render( map() method, you’d better always remember to add a unique key . 3.10.6. Of the element
key among his brother elements, it should be unique. ¶ key should be unique among his brothers. However, they do not need to be globally unique. When we generate two different arrays, we can use the same key. React instance ¶
functionBlog(props){constsidebar=({props.posts.map((post)=>
{post.title}
{content}<p>
<code class="docutils literal notranslate">
<span class="pre">
key
</span>
</code>
will serve as a hint to React, but will not be passed on to your components. If you need to use and
<code class="docutils literal notranslate">
<span class="pre">
key
</span>
</code>
the same value, pass it as an attribute:
</p>
<div class="highlight-default notranslate">
<div class="highlight">
<pre><span></span><span class="n">const</span> <span class="n">content</span> <span class="o">=</span> <span class="n">posts</span><span class="o">.</span><span class="n">map</span><span class="p">((</span><span class="n">post</span><span class="p">)</span> <span class="o">=></span>
key={post.id}
id={post.id}
title={post.title} />
);
In the above example, the Post component can read
props.id
, but cannot be read out
props.key
.
3.10.7. Embed in jsx
map() ¶ listItems variable and include it in the JSX: function NumberList(props) {
const numbers = props.numbers;
const listItems = numbers.map((number) =>
<ListItem key={number.toString()}
value={number} />
);
return (
<ul>
{listItems}
ul>
);
}
map() is used in the following ways: React instance ¶
functionNumberList(props){constnumbers=props.numbers;return(<ul>{numbers.map((number)=>
<ListItemkey={number.toString()}value={number}/>)}ul>);}
map() with too many levels of nesting, you can extract the components.
Principles, Technologies, and Methods of Geographic Information Systems
102