React \u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\u7684 JAVASCRIPT \u5e93\u3002React \u4e3b\u8981\u7528\u4e8e\u6784\u5efaUI\uff0c\u5f88\u591a\u4eba\u8ba4\u4e3a React \u662f MVC \u4e2d\u7684 V\uff08\u89c6\u56fe\uff09\u3002React \u8d77\u6e90\u4e8e Facebook \u7684\u5185\u90e8\u9879\u76ee\uff0c\u7528\u6765\u67b6\u8bbe Instagram \u7684\u7f51\u7ad9\uff0c\u5e76\u4e8e 2013 \u5e74 5 \u6708\u5f00\u6e90\u3002React \u62e5\u6709\u8f83\u9ad8\u7684\u6027\u80fd\uff0c\u4ee3\u7801\u903b\u8f91\u975e\u5e38\u7b80\u5355\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u5df2\u5f00\u59cb\u5173\u6ce8\u548c\u4f7f\u7528\u5b83\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 map() \u65b9\u6cd5\u6765\u521b\u5efa\u5217\u8868\u3002<\/p>\n
React \u5b9e\u4f8b<\/p>\n
\r\n\u4f7f\u7528 map() \u65b9\u6cd5\u904d\u5386\u6570\u7ec4\u751f\u6210\u4e86\u4e00\u4e2a 1 \u5230 5 \u7684\u6570\u5b57\u5217\u8868:\r\n\r\nconst numbers = [1, 2, 3, 4, 5];\r\nconst listItems = numbers.map((numbers) =>\r\n <li>{numbers}<\/li>\r\n);\r\n \r\nReactDOM.render(\r\n <ul>{listItems}<\/ul>,\r\n document.getElementById('example')\r\n);<\/pre>\n\u6211\u4eec\u53ef\u4ee5\u5c06\u4ee5\u4e0a\u5b9e\u4f8b\u91cd\u6784\u6210\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u7ec4\u4ef6\u63a5\u6536\u6570\u7ec4\u53c2\u6570\uff0c\u6bcf\u4e2a\u5217\u8868\u5143\u7d20\u5206\u914d\u4e00\u4e2a key\uff0c\u4e0d\u7136\u4f1a\u51fa\u73b0\u8b66\u544a a key should be provided for list items\uff0c\u610f\u601d\u5c31\u662f\u9700\u8981\u5305\u542b key\uff1a<\/p>\n
React \u5b9e\u4f8b<\/p>\n
\r\nfunction NumberList(props) {\r\n const numbers = props.numbers;\r\n const listItems = numbers.map((number) =>\r\n <li key={number.toString()}>\r\n {number}\r\n <\/li>\r\n );\r\n return (\r\n <ul>{listItems}<\/ul>\r\n );\r\n}\r\n \r\nconst numbers = [1, 2, 3, 4, 5];\r\nReactDOM.render(\r\n <NumberList numbers={numbers} \/>,\r\n document.getElementById('example')\r\n);<\/pre>\nKeys<\/strong><\/div>\nKeys \u53ef\u4ee5\u5728 DOM \u4e2d\u7684\u67d0\u4e9b\u5143\u7d20\u88ab\u589e\u52a0\u6216\u5220\u9664\u7684\u65f6\u5019\u5e2e\u52a9 React \u8bc6\u522b\u54ea\u4e9b\u5143\u7d20\u53d1\u751f\u4e86\u53d8\u5316\u3002\u56e0\u6b64\u4f60\u5e94\u5f53\u7ed9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u8d4b\u4e88\u4e00\u4e2a\u786e\u5b9a\u7684\u6807\u8bc6\u3002<\/p>\n
\r\nconst numbers = [1, 2, 3, 4, 5];\r\nconst listItems = numbers.map((number) =>\r\n <li key={number.toString()}>\r\n {number}\r\n <\/li>\r\n);<\/pre>\n\u4e00\u4e2a\u5143\u7d20\u7684 key \u6700\u597d\u662f\u8fd9\u4e2a\u5143\u7d20\u5728\u5217\u8868\u4e2d\u62e5\u6709\u7684\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u5b57\u7b26\u4e32\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f7f\u7528\u6765\u81ea\u6570\u636e\u7684 id \u4f5c\u4e3a\u5143\u7d20\u7684 key:<\/p>\n
\r\nconst todoItems = todos.map((todo) =>\r\n <li key={todo.id}>\r\n {todo.text}\r\n <\/li>\r\n);<\/pre>\n\u5f53\u5143\u7d20\u6ca1\u6709\u786e\u5b9a\u7684 id \u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ed6\u7684\u5e8f\u5217\u53f7\u7d22\u5f15 index \u4f5c\u4e3a key\uff1a<\/p>\n
\r\nconst todoItems = todos.map((todo, index) =>\r\n \/\/ \u53ea\u6709\u5728\u6ca1\u6709\u786e\u5b9a\u7684 id \u65f6\u4f7f\u7528\r\n <li key={index}>\r\n {todo.text}\r\n <\/li>\r\n);<\/pre>\n\u5982\u679c\u5217\u8868\u53ef\u4ee5\u91cd\u65b0\u6392\u5e8f\uff0c\u6211\u4eec\u4e0d\u5efa\u8bae\u4f7f\u7528\u7d22\u5f15\u6765\u8fdb\u884c\u6392\u5e8f\uff0c\u56e0\u4e3a\u8fd9\u4f1a\u5bfc\u81f4\u6e32\u67d3\u53d8\u5f97\u5f88\u6162\u3002<\/p>\n
\u7528keys\u63d0\u53d6\u7ec4\u4ef6<\/strong><\/div>\n\u5143\u7d20\u7684 key \u53ea\u6709\u5728\u5b83\u548c\u5b83\u7684\u5144\u5f1f\u8282\u70b9\u5bf9\u6bd4\u65f6\u624d\u6709\u610f\u4e49\u3002<\/p>\n
\u6bd4\u65b9\u8bf4\uff0c\u5982\u679c\u4f60\u63d0\u53d6\u51fa\u4e00\u4e2a ListItem \u7ec4\u4ef6\uff0c\u4f60\u5e94\u8be5\u628a key \u4fdd\u5b58\u5728\u6570\u7ec4\u4e2d\u7684\u8fd9\u4e2a <ListItem \/> \u5143\u7d20\u4e0a\uff0c\u800c\u4e0d\u662f\u653e\u5728 ListItem \u7ec4\u4ef6\u4e2d\u7684 <li> \u5143\u7d20\u4e0a\u3002<\/p>\n
\u9519\u8bef\u7684\u793a\u8303<\/strong><\/span><\/div>\n\r\nfunction ListItem(props) {\r\n const value = props.value;\r\n return (\r\n \/\/ \u9519\u5566\uff01\u4f60\u4e0d\u9700\u8981\u5728\u8fd9\u91cc\u6307\u5b9akey:\r\n <li key={value.toString()}>\r\n {value}\r\n <\/li>\r\n );\r\n}\r\n\r\nfunction NumberList(props) {\r\n const numbers = props.numbers;\r\n const listItems = numbers.map((number) =>\r\n \/\/\u9519\u5566\uff01\u5143\u7d20\u7684key\u5e94\u8be5\u5728\u8fd9\u91cc\u6307\u5b9a\uff1a\r\n <ListItem value={number} \/>\r\n );\r\n return (\r\n <ul>\r\n {listItems}\r\n <\/ul>\r\n );\r\n}\r\n\r\nconst numbers = [1, 2, 3, 4, 5];\r\nReactDOM.render(\r\n <NumberList numbers={numbers} \/>,\r\n document.getElementById('example')\r\n);<\/pre>\nkey\u7684\u6b63\u786e\u4f7f\u7528\u65b9\u5f0f<\/strong><\/span><\/div>\nReact \u5b9e\u4f8b<\/p>\n
\r\nfunction ListItem(props) {\r\n \/\/ \u5bf9\u5566\uff01\u8fd9\u91cc\u4e0d\u9700\u8981\u6307\u5b9akey:\r\n return <li>{props.value}<\/li>;\r\n}\r\n \r\nfunction NumberList(props) {\r\n const numbers = props.numbers;\r\n const listItems = numbers.map((number) =>\r\n \/\/ \u53c8\u5bf9\u5566\uff01key\u5e94\u8be5\u5728\u6570\u7ec4\u7684\u4e0a\u4e0b\u6587\u4e2d\u88ab\u6307\u5b9a\r\n <ListItem key={number.toString()}\r\n value={number} \/>\r\n \r\n );\r\n return (\r\n <ul>\r\n {listItems}\r\n <\/ul>\r\n );\r\n}\r\n \r\nconst numbers = [1, 2, 3, 4, 5];\r\nReactDOM.render(\r\n <NumberList numbers={numbers} \/>,\r\n document.getElementById('example')\r\n);<\/pre>\n\u5f53\u4f60\u5728 map() \u65b9\u6cd5\u7684\u5185\u90e8\u8c03\u7528\u5143\u7d20\u65f6\uff0c\u4f60\u6700\u597d\u968f\u65f6\u8bb0\u5f97\u4e3a\u6bcf\u4e00\u4e2a\u5143\u7d20\u52a0\u4e0a\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684 key\u3002<\/p>\n
\u5143\u7d20\u7684 key \u5728\u4ed6\u7684\u5144\u5f1f\u5143\u7d20\u4e4b\u95f4\u5e94\u8be5\u552f\u4e00<\/strong><\/div>\n\u6570\u7ec4\u5143\u7d20\u4e2d\u4f7f\u7528\u7684 key \u5728\u5176\u5144\u5f1f\u4e4b\u95f4\u5e94\u8be5\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u7136\u800c\uff0c\u5b83\u4eec\u4e0d\u9700\u8981\u662f\u5168\u5c40\u552f\u4e00\u7684\u3002\u5f53\u6211\u4eec\u751f\u6210\u4e24\u4e2a\u4e0d\u540c\u7684\u6570\u7ec4\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u76f8\u540c\u7684\u952e\u3002<\/p>\n
React \u5b9e\u4f8b<\/p>\n
\r\nfunction Blog(props) {\r\n const sidebar = (\r\n <ul>\r\n {props.posts.map((post) =>\r\n <li key={post.id}>\r\n {post.title}\r\n <\/li>\r\n )}\r\n <\/ul>\r\n );\r\n const content = props.posts.map((post) =>\r\n <div key={post.id}>\r\n <h3>{post.title}<\/h3>\r\n <p>{post.content}<\/p>\r\n <\/div>\r\n );\r\n return (\r\n <div>\r\n {sidebar}\r\n <hr \/>\r\n {content}\r\n <\/div>\r\n );\r\n}\r\n \r\nconst posts = [\r\n {id: 1, title: 'Hello World', content: 'Welcome to learning React!'},\r\n {id: 2, title: 'Installation', content: 'You can install React from npm.'}\r\n];\r\nReactDOM.render(\r\n <Blog posts={posts} \/>,\r\n document.getElementById('example')\r\n);<\/pre>\nkey \u4f1a\u4f5c\u4e3a\u7ed9 React \u7684\u63d0\u793a\uff0c\u4f46\u4e0d\u4f1a\u4f20\u9012\u7ed9\u4f60\u7684\u7ec4\u4ef6\u3002\u5982\u679c\u60a8\u7684\u7ec4\u4ef6\u4e2d\u9700\u8981\u4f7f\u7528\u548c key \u76f8\u540c\u7684\u503c\uff0c\u8bf7\u5c06\u5176\u4f5c\u4e3a\u5c5e\u6027\u4f20\u9012\uff1a<\/p>\n
\r\nconst content = posts.map((post) =>\r\n <Post\r\n key={post.id}\r\n id={post.id}\r\n title={post.title} \/>\r\n);<\/pre>\n\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0cPost \u7ec4\u4ef6\u53ef\u4ee5\u8bfb\u51fa props.id\uff0c\u4f46\u662f\u4e0d\u80fd\u8bfb\u51fa props.key\u3002<\/p>\n
\u5728 jsx \u4e2d\u5d4c\u5165 map()<\/strong><\/div>\n\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a\u5355\u72ec\u7684 listItems \u53d8\u91cf\u5e76\u5c06\u5176\u5305\u542b\u5728 JSX \u4e2d:<\/p>\n
\r\nfunction NumberList(props) {\r\n const numbers = props.numbers;\r\n const listItems = numbers.map((number) =>\r\n <ListItem key={number.toString()}\r\n value={number} \/>\r\n\r\n );\r\n return (\r\n <ul>\r\n {listItems}\r\n <\/ul>\r\n );\r\n}<\/pre>\nJSX \u5141\u8bb8\u5728\u5927\u62ec\u53f7\u4e2d\u5d4c\u5165\u4efb\u4f55\u8868\u8fbe\u5f0f\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5728 map() \u4e2d\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n
React \u5b9e\u4f8b<\/p>\n
\r\nfunction NumberList(props) {\r\n const numbers = props.numbers;\r\n return (\r\n <ul>\r\n {numbers.map((number) =>\r\n <ListItem key={number.toString()}\r\n value={number} \/>\r\n \r\n )}\r\n <\/ul>\r\n );\r\n}\r\n<\/pre>\n\u8fd9\u4e48\u505a\u6709\u65f6\u53ef\u4ee5\u4f7f\u4f60\u7684\u4ee3\u7801\u66f4\u6e05\u6670\uff0c\u4f46\u6709\u65f6\u8fd9\u79cd\u98ce\u683c\u4e5f\u4f1a\u88ab\u6ee5\u7528\u3002\u5c31\u50cf\u5728 JavaScript \u4e2d\u4e00\u6837\uff0c\u4f55\u65f6\u9700\u8981\u4e3a\u4e86\u53ef\u8bfb\u6027\u63d0\u53d6\u51fa\u4e00\u4e2a\u53d8\u91cf\uff0c\u8fd9\u5b8c\u5168\u53d6\u51b3\u4e8e\u4f60\u3002\u4f46\u8bf7\u8bb0\u4f4f\uff0c\u5982\u679c\u4e00\u4e2a map() \u5d4c\u5957\u4e86\u592a\u591a\u5c42\u7ea7\uff0c\u90a3\u4f60\u5c31\u53ef\u4ee5\u63d0\u53d6\u51fa\u7ec4\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 map() \u65b9\u6cd5\u6765\u521b\u5efa\u5217\u8868\u3002 React \u5b9e\u4f8b \u4f7f\u7528 map() \u65b9\u6cd5 […]<\/p>\n","protected":false},"author":321,"featured_media":207359,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-207539","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=207539"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207539\/revisions"}],"predecessor-version":[{"id":207542,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207539\/revisions\/207542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/207359"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=207539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=207539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=207539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}