{"id":5619,"date":"2023-01-06T10:53:01","date_gmt":"2023-01-06T02:53:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5619"},"modified":"2023-01-06T10:53:01","modified_gmt":"2023-01-06T02:53:01","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e4%be%86%e6%b1%82%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%8d%95%e8%af%8d%e6%95%b0countwords","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e4%be%86%e6%b1%82%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%ad%e5%8d%95%e8%af%8d%e6%95%b0countwords\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528React.JS\u4f86\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u6570(countWords)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.JS, countWords\"><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u6570(countWords)<\/h1>\n<p>React.JS \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u6570(countWords)\u3002<\/p>\n<h2>\u4f7f\u7528 React.JS \u4f86\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u6570(countWords)<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b\u65b0\u7684 React \u7d44\u4ef6\uff0c\u4e26\u5c07\u5176\u547d\u540d\u70ba <span style=\"font-weight: bold;\">CountWords<\/span>\u3002\u5728\u7d44\u4ef6\u4e2d\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b <span style=\"font-weight: bold;\">countWords<\/span> \u65b9\u6cd5\uff0c\u8a72\u65b9\u6cd5\u5c07\u63a5\u53d7\u4e00\u500b\u5b57\u7b26\u4e32\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u8a72\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u7684\u6578\u91cf\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass CountWords extends React.Component {\n  countWords(str) {\n    \/\/ \u5c07\u5b57\u7b26\u4e32\u62c6\u5206\u70ba\u55ae\u8a5e\u6578\u7d44\n    const words = str.split(' ');\n    \/\/ \u8fd4\u56de\u55ae\u8a5e\u6578\u7d44\u7684\u9577\u5ea6\n    return words.length;\n  }\n}\n<\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5728 <span style=\"font-weight: bold;\">CountWords<\/span> \u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e00\u500b <span style=\"font-weight: bold;\">render<\/span> \u65b9\u6cd5\uff0c\u8a72\u65b9\u6cd5\u5c07\u8fd4\u56de\u4e00\u500b <span style=\"font-weight: bold;\">div<\/span> \u5143\u7d20\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u500b <span style=\"font-weight: bold;\">input<\/span> \u5143\u7d20\uff0c\u8a72\u5143\u7d20\u5c07\u5141\u8a31\u7528\u6236\u8f38\u5165\u5b57\u7b26\u4e32\uff0c\u4ee5\u53ca\u4e00\u500b <span style=\"font-weight: bold;\">span<\/span> \u5143\u7d20\uff0c\u8a72\u5143\u7d20\u5c07\u986f\u793a\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u7684\u6578\u91cf\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass CountWords extends React.Component {\n  \/\/ ...\n\n  render() {\n    return (\n      <div>\n        <input type=\"text\" \/>\n        <span>\u5355\u8bcd\u6570\uff1a<\/span>\n      <\/div>\n    );\n  }\n}\n<\/pre>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5728 <span style=\"font-weight: bold;\">CountWords<\/span> \u7d44\u4ef6\u4e2d\u5b9a\u7fa9\u4e00\u500b <span style=\"font-weight: bold;\">handleChange<\/span> \u65b9\u6cd5\uff0c\u8a72\u65b9\u6cd5\u5c07\u63a5\u53d7\u4e00\u500b <span style=\"font-weight: bold;\">event<\/span> \u53c3\u6578\uff0c\u4e26\u5c07\u5176\u4e2d\u7684\u5b57\u7b26\u4e32\u50b3\u905e\u7d66 <span style=\"font-weight: bold;\">countWords<\/span> \u65b9\u6cd5\uff0c\u4ee5\u7372\u53d6\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u7684\u6578\u91cf\uff0c\u4e26\u5c07\u5176\u986f\u793a\u5728 <span style=\"font-weight: bold;\">span<\/span> \u5143\u7d20\u4e2d\u3002<\/p>\n<pre class=\"brush: javascript\">\nclass CountWords extends React.Component {\n  \/\/ ...\n\n  handleChange(event) {\n    \/\/ \u7372\u53d6\u7528\u6236\u8f38\u5165\u7684\u5b57\u7b26\u4e32\n    const str = event.target.value;\n    \/\/ \u8a08\u7b97\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u7684\u6578\u91cf\n    const count = this.countWords(str);\n    \/\/ \u5c07\u5355\u8a5e\u6578\u91cf\u986f\u793a\u5728 span \u5143\u7d20\u4e2d\n    this.span.innerText = count;\n  }\n}\n<\/pre>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u5df2\u7d93\u5b8c\u6210\u4e86 <span style=\"font-weight: bold;\">CountWords<\/span> \u7d44\u4ef6\u7684\u958b\u767c\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u4f86\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u7684\u6578\u91cf\u4e86\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\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u6578(countWords)\u3002\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba <span style=\"font-weight: bold;\">CountWords<\/span> \u7684 React \u7d44\u4ef6\uff0c\u4e26\u5728\u5176\u4e2d\u5b9a\u7fa9\u4e86 <span style=\"font-weight: bold;\">countWords<\/span>\u3001<span style=\"font-weight: bold;\">render<\/span> \u548c <span style=\"font-weight: bold;\">handleChange<\/span> \u65b9\u6cd5\uff0c\u4ee5\u4fbf\u80fd\u5920\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8a5e\u7684\u6578\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528React.JS\u4f86\u6c42\u5b57\u7b26\u4e32\u4e2d\u5355\u8bcd\u6570(countWords)\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u8b93\u5b78\u751f\u5011\u66f4\u5bb9\u6613\u7406\u89e3\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-5619","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-1sD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5619","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=5619"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5619\/revisions"}],"predecessor-version":[{"id":5620,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5619\/revisions\/5620"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5619"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5619"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5619"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}