{"id":5637,"date":"2023-01-06T11:02:08","date_gmt":"2023-01-06T03:02:08","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5637"},"modified":"2023-01-06T11:02:08","modified_gmt":"2023-01-06T03:02:08","slug":"%e5%88%a9%e7%94%a8react-js%e5%88%a4%e6%96%b7ipv4%e5%9c%b0%e5%9d%80%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%9c%89%e6%95%88%e6%80%a7","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%88%a9%e7%94%a8react-js%e5%88%a4%e6%96%b7ipv4%e5%9c%b0%e5%9d%80%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9a%84%e6%9c%89%e6%95%88%e6%80%a7\/","title":{"rendered":"\u5229\u7528React.JS\u5224\u65b7IPv4\u5730\u5740\u5b57\u7b26\u4e32\u7684\u6709\u6548\u6027"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.JS, IPv4, isValidIpv4\"><\/p>\n<h1>\u4f7f\u7528 React.JS \u5224\u65b7\u662f\u5426\u70ba\u7b26\u5408 IPv4 \u5730\u5740\u5b57\u7b26\u4e32(isValidIpv4)<\/h1>\n<p>IPv4 \u5730\u5740\u662f\u4e00\u7a2e\u7db2\u8def\u5354\u5b9a\uff0c\u7528\u65bc\u5c07\u8a2d\u5099\u9023\u63a5\u5230\u7db2\u8def\u4e0a\u3002\u5b83\u662f\u4e00\u7a2e 32 \u4f4d\u7684\u4f4d\u5143\u7d44\uff0c\u7528\u65bc\u6307\u5b9a\u7db2\u8def\u4e0a\u7684\u8a2d\u5099\u3002\u5728\u7db2\u8def\u4e0a\uff0c\u6bcf\u500b\u8a2d\u5099\u90fd\u6709\u4e00\u500b\u552f\u4e00\u7684 IPv4 \u5730\u5740\uff0c\u53ef\u4ee5\u7528\u4f86\u8b58\u5225\u8a72\u8a2d\u5099\u3002<\/p>\n<p>\u5728\u958b\u767c\u7db2\u8def\u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u6709\u6642\u9700\u8981\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002\u5728\u9019\u7a2e\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 React.JS \u4f86\u5be6\u73fe isValidIpv4 \u529f\u80fd\uff0c\u4ee5\u78ba\u4fdd\u8f38\u5165\u7684\u5b57\u4e32\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002<\/p>\n<h2>\u4f7f\u7528 React.JS \u5be6\u73fe isValidIpv4 \u529f\u80fd<\/h2>\n<p>React.JS \u662f\u4e00\u500b JavaScript \u51fd\u5f0f\u5eab\uff0c\u53ef\u4ee5\u7528\u65bc\u958b\u767c\u9ad8\u6548\u7684\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5f0f\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u8def\u61c9\u7528\u7a0b\u5f0f\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u500b\u5f37\u5927\u7684 API\uff0c\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002<\/p>\n<p>\u8981\u5be6\u73fe isValidIpv4 \u529f\u80fd\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528 React.JS \u7684 <code>React.useState()<\/code> \u51fd\u5f0f\uff0c\u5b83\u53ef\u4ee5\u7528\u65bc\u5275\u5efa\u4e00\u500b\u65b0\u7684\u72c0\u614b\uff0c\u7528\u65bc\u5b58\u5132\u8f38\u5165\u7684\u5b57\u4e32\u3002\u7136\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>React.useEffect()<\/code> \u51fd\u5f0f\u4f86\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002<\/p>\n<pre class=\"brush: javascript\">\nconst [ipv4, setIpv4] = React.useState('');\n\nReact.useEffect(() => {\n  const isValidIpv4 = (ipv4) => {\n    \/\/ \u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\n  };\n  setIpv4(isValidIpv4);\n}, [ipv4]);\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 <code>React.useState()<\/code> \u51fd\u5f0f\u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684\u72c0\u614b\uff0c\u7528\u65bc\u5b58\u5132\u8f38\u5165\u7684\u5b57\u4e32\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 <code>React.useEffect()<\/code> \u51fd\u5f0f\u4f86\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002<\/p>\n<p>\u5728 <code>React.useEffect()<\/code> \u51fd\u5f0f\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba <code>isValidIpv4()<\/code> \u7684\u51fd\u5f0f\uff0c\u7528\u65bc\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002\u8a72\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u53c3\u6578\uff0c\u5373\u8f38\u5165\u7684\u5b57\u4e32\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u8868\u793a\u8a72\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002<\/p>\n<p>\u5728 <code>isValidIpv4()<\/code> \u51fd\u5f0f\u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002\u6b63\u5247\u8868\u9054\u5f0f\u53ef\u4ee5\u7528\u65bc\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u7b26\u5408\u7279\u5b9a\u7684\u6a21\u5f0f\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u8868\u793a\u8a72\u5b57\u4e32\u662f\u5426\u7b26\u5408\u8a72\u6a21\u5f0f\u3002<\/p>\n<pre class=\"brush: javascript\">\nconst isValidIpv4 = (ipv4) => {\n  const regex = \/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$\/;\n  return regex.test(ipv4);\n};\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f\u4f86\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002\u5982\u679c\u8a72\u5b57\u4e32\u7b26\u5408\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u5247\u51fd\u5f0f\u5c07\u8fd4\u56de <code>true<\/code>\uff0c\u5426\u5247\u5c07\u8fd4\u56de <code>false<\/code>\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 <code>React.useEffect()<\/code> \u51fd\u5f0f\u4f86\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5728 <code>ipv4<\/code> \u72c0\u614b\u4e2d\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u5be6\u73fe isValidIpv4 \u529f\u80fd\uff0c\u4ee5\u78ba\u4fdd\u8f38\u5165\u7684\u5b57\u4e32\u662f\u6709\u6548\u7684 IPv4 \u5730\u5740\u3002\u6211\u5011\u9996\u5148\u4f7f\u7528 <code>React.useState()<\/code> \u51fd\u5f0f\u5275\u5efa\u4e86\u4e00\u500b\u65b0\u7684\u72c0\u614b\uff0c\u7528\u65bc\u5b58\u5132\u8f38\u5165\u7684\u5b57\u4e32\u3002\u7136\u5f8c\uff0c\u6211\u5011\u4f7f\u7528 <code>React.useEffect()<\/code> \u51fd\u5f0f\u4f86\u6aa2\u67e5\u8f38\u5165\u7684\u5b57\u4e32\u662f\u5426\u70ba\u6709\u6548\u7684 IPv4 \u5730\u5740\uff0c\u4e26\u5c07\u7d50\u679c\u5b58\u5132\u5728 <code>ipv4<\/code> \u72c0\u614b\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528React.JS\u4f86\u5224\u65b7IPv4\u5730\u5740\u5b57\u7b26\u4e32\u662f\u5426\u6709\u6548\uff0c\u4e26\u63d0\u4f9b\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\u4f86\u8a73\u7d30\u8aaa\u660e\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eReact.JS\u4e2d\u7684isValidIpv4\u51fd\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u6aa2\u67e5IPv4\u5730\u5740\u5b57\u7b26\u4e32\u7684\u6709\u6548\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5637","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1sV","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5637"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5637\/revisions"}],"predecessor-version":[{"id":5638,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5637\/revisions\/5638"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}