{"id":5583,"date":"2023-01-06T09:48:14","date_gmt":"2023-01-06T01:48:14","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5583"},"modified":"2023-01-06T09:48:14","modified_gmt":"2023-01-06T01:48:14","slug":"%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e8%a8%88%e7%ae%97%e5%ad%97%e4%b8%b2%e5%ad%97%e7%ac%a6%e5%87%ba%e7%8f%be%e6%ac%a1%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e4%ba%86%e8%a7%a3%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e8%a8%88%e7%ae%97%e5%ad%97%e4%b8%b2%e5%ad%97%e7%ac%a6%e5%87%ba%e7%8f%be%e6%ac%a1%e6%95%b8\/","title":{"rendered":"\u4e86\u89e3\u5982\u4f55\u5229\u7528React.JS\u8a08\u7b97\u5b57\u4e32\u5b57\u7b26\u51fa\u73fe\u6b21\u6578"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React.JS, countCharacters\"><\/p>\n<h1>\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\uff1f<\/h1>\n<p>React.JS \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u7aef\u61c9\u7528\u7a0b\u5e8f\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\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\u3002<\/p>\n<h2>\u4f7f\u7528 React.JS \u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5275\u5efa\u4e00\u500b React \u7d44\u4ef6\uff0c\u7528\u65bc\u8a08\u7b97\u5b57\u4e32\u4e2d\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\u3002\u7d44\u4ef6\u7684\u4ee3\u78bc\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: javascript\">\nclass CountCharacters extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = {\n      count: 0\n    };\n  }\n\n  countCharacters(str) {\n    let count = 0;\n    for (let i = 0; i < str.length; i++) {\n      if (str.charAt(i) !== ' ') {\n        count++;\n      }\n    }\n    this.setState({ count });\n  }\n\n  render() {\n    return (\n      <div>\n        <input\n          type=\"text\"\n          onChange={e => this.countCharacters(e.target.value)}\n        \/>\n        \u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\uff1a{this.state.count}\n      <\/div>\n    );\n  }\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u540d\u70ba <code>CountCharacters<\/code> \u7684 React \u7d44\u4ef6\uff0c\u5b83\u6703\u5728\u7528\u6236\u8f38\u5165\u5b57\u7b26\u4e32\u6642\u8a08\u7b97\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\u3002\u7d44\u4ef6\u4e2d\u7684 <code>countCharacters<\/code> \u65b9\u6cd5\u6703\u904d\u6b77\u5b57\u7b26\u4e32\uff0c\u4e26\u8a08\u7b97\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\uff0c\u6700\u5f8c\u5c07\u7d50\u679c\u5b58\u5132\u5728 <code>state<\/code> \u4e2d\u3002<\/p>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 <code>CountCharacters<\/code> \u7d44\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: javascript\">\nReactDOM.render(\n  <CountCharacters \/>,\n  document.getElementById('root')\n);\n<\/pre>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u5c31\u53ef\u4ee5\u4f7f\u7528 React.JS \u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\u4e86\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 React.JS \u4f86\u8a08\u7b97\u5b57\u4e32\u4e2d\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\u3002\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500b React \u7d44\u4ef6\uff0c\u7136\u5f8c\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u8a72\u7d44\u4ef6\uff0c\u5c31\u53ef\u4ee5\u8f15\u9b06\u5730\u8a08\u7b97\u5b57\u7b26\u51fa\u73fe\u7684\u6b21\u6578\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528React.JS\u4e2d\u7684countCharacters()\u51fd\u6578\u4f86\u8a08\u7b97\u5b57\u4e32\u5b57\u7b26\u51fa\u73fe\u6b21\u6578\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u5b8c\u6210\u5de5\u4f5c\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-5583","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-1s3","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5583","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=5583"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5583\/revisions"}],"predecessor-version":[{"id":5584,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5583\/revisions\/5584"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}