{"id":207533,"date":"2020-12-26T09:13:10","date_gmt":"2020-12-26T01:13:10","guid":{"rendered":"https:\/\/gulass.cn\/?p=207533"},"modified":"2020-12-21T10:14:42","modified_gmt":"2020-12-21T02:14:42","slug":"react-study-handling","status":"publish","type":"post","link":"https:\/\/gulass.cn\/react-study-handling.html","title":{"rendered":"React\u5b66\u4e60\u7b14\u8bb0-\u4e8b\u4ef6\u5904\u7406"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nReact \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

\"\"
\nReact \u5143\u7d20\u7684\u4e8b\u4ef6\u5904\u7406\u548c DOM \u5143\u7d20\u7c7b\u4f3c\u3002\u4f46\u662f\u6709\u4e00\u70b9\u8bed\u6cd5\u4e0a\u7684\u4e0d\u540c:<\/p>\n

    \n
  • 1.React \u4e8b\u4ef6\u7ed1\u5b9a\u5c5e\u6027\u7684\u547d\u540d\u91c7\u7528\u9a7c\u5cf0\u5f0f\u5199\u6cd5\uff0c\u800c\u4e0d\u662f\u5c0f\u5199\u3002<\/li>\n
  • 2.\u5982\u679c\u91c7\u7528 JSX \u7684\u8bed\u6cd5\u4f60\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32(DOM \u5143\u7d20\u7684\u5199\u6cd5)<\/li>\n<\/ul>\n

    HTML \u901a\u5e38\u5199\u6cd5\u662f\uff1a<\/p>\n

    \r\n<button onclick=\"activateLasers()\">\r\n  \u6fc0\u6d3b\u6309\u94ae\r\n<\/button><\/pre>\n

    React \u4e2d\u5199\u6cd5\u4e3a\uff1a<\/p>\n

    \r\n<button onClick={activateLasers}>\r\n  \u6fc0\u6d3b\u6309\u94ae\r\n<\/button><\/pre>\n

    \u5728 React \u4e2d\u53e6\u4e00\u4e2a\u4e0d\u540c\u662f\u4f60\u4e0d\u80fd\u4f7f\u7528\u8fd4\u56de false \u7684\u65b9\u5f0f\u963b\u6b62\u9ed8\u8ba4\u884c\u4e3a\uff0c \u4f60\u5fc5\u987b\u660e\u786e\u4f7f\u7528 preventDefault\u3002<\/p>\n

    \u4f8b\u5982\uff0c\u901a\u5e38\u6211\u4eec\u5728 HTML \u4e2d\u963b\u6b62\u94fe\u63a5\u9ed8\u8ba4\u6253\u5f00\u4e00\u4e2a\u65b0\u9875\u9762\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n

    \r\n<a href=\"#\" onclick=\"console.log('\u70b9\u51fb\u94fe\u63a5'); return false\">\r\n  \u70b9\u6211\r\n<\/a><\/pre>\n

    \u5728 React \u7684\u5199\u6cd5\u4e3a\uff1a<\/p>\n

    \r\nfunction ActionLink() {\r\n  function handleClick(e) {\r\n    e.preventDefault();\r\n    console.log('\u94fe\u63a5\u88ab\u70b9\u51fb');\r\n  }\r\n \r\n  return (\r\n    <a href=\"#\" onClick={handleClick}>\r\n      \u70b9\u6211\r\n    <\/a>\r\n  );\r\n}<\/pre>\n

    \u5b9e\u4f8b\u4e2d e \u662f\u4e00\u4e2a\u5408\u6210\u4e8b\u4ef6\u3002<\/p>\n

    \u4f7f\u7528 React \u7684\u65f6\u5019\u901a\u5e38\u4f60\u4e0d\u9700\u8981\u4f7f\u7528 addEventListener \u4e3a\u4e00\u4e2a\u5df2\u521b\u5efa\u7684 DOM \u5143\u7d20\u6dfb\u52a0\u76d1\u542c\u5668\u3002\u4f60\u4ec5\u4ec5\u9700\u8981\u5728\u8fd9\u4e2a\u5143\u7d20\u521d\u59cb\u6e32\u67d3\u7684\u65f6\u5019\u63d0\u4f9b\u4e00\u4e2a\u76d1\u542c\u5668\u3002<\/p>\n

    \u5f53\u4f60\u4f7f\u7528 ES6 class \u8bed\u6cd5\u6765\u5b9a\u4e49\u4e00\u4e2a\u7ec4\u4ef6\u7684\u65f6\u5019\uff0c\u4e8b\u4ef6\u5904\u7406\u5668\u4f1a\u6210\u4e3a\u7c7b\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u4e0b\u9762\u7684 Toggle \u7ec4\u4ef6\u6e32\u67d3\u4e00\u4e2a\u8ba9\u7528\u6237\u5207\u6362\u5f00\u5173\u72b6\u6001\u7684\u6309\u94ae\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    \r\nclass Toggle extends React.Component {\r\n  constructor(props) {\r\n    super(props);\r\n    this.state = {isToggleOn: true};\r\n \r\n    \/\/ \u8fd9\u8fb9\u7ed1\u5b9a\u662f\u5fc5\u8981\u7684\uff0c\u8fd9\u6837 `this` \u624d\u80fd\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u4f7f\u7528\r\n    this.handleClick = this.handleClick.bind(this);\r\n  }\r\n \r\n  handleClick() {\r\n    this.setState(prevState => ({\r\n      isToggleOn: !prevState.isToggleOn\r\n    }));\r\n  }\r\n \r\n  render() {\r\n    return (\r\n      <button onClick={this.handleClick}>\r\n        {this.state.isToggleOn ? 'ON' : 'OFF'}\r\n      <\/button>\r\n    );\r\n  }\r\n}\r\n \r\nReactDOM.render(\r\n  <Toggle \/>,\r\n  document.getElementById('example')\r\n);\r\n<\/pre>\n

    \u4f60\u5fc5\u987b\u8c28\u614e\u5bf9\u5f85 JSX \u56de\u8c03\u51fd\u6570\u4e2d\u7684 this\uff0c\u7c7b\u7684\u65b9\u6cd5\u9ed8\u8ba4\u662f\u4e0d\u4f1a\u7ed1\u5b9a this \u7684\u3002\u5982\u679c\u4f60\u5fd8\u8bb0\u7ed1\u5b9a this.handleClick \u5e76\u628a\u5b83\u4f20\u5165 onClick, \u5f53\u4f60\u8c03\u7528\u8fd9\u4e2a\u51fd\u6570\u7684\u65f6\u5019 this \u7684\u503c\u4f1a\u662f undefined\u3002<\/p>\n

    \u8fd9\u5e76\u4e0d\u662f React \u7684\u7279\u6b8a\u884c\u4e3a\uff1b\u5b83\u662f\u51fd\u6570\u5982\u4f55\u5728 JavaScript \u4e2d\u8fd0\u884c\u7684\u4e00\u90e8\u5206\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u5728\u65b9\u6cd5\u540e\u9762\u6dfb\u52a0 () \uff0c\u4f8b\u5982 onClick={this.handleClick}\uff0c\u4f60\u5e94\u8be5\u4e3a\u8fd9\u4e2a\u65b9\u6cd5\u7ed1\u5b9a this\u3002<\/p>\n

    \u5982\u679c\u4f7f\u7528 bind \u8ba9\u4f60\u5f88\u70e6\uff0c\u8fd9\u91cc\u6709\u4e24\u79cd\u65b9\u5f0f\u53ef\u4ee5\u89e3\u51b3\u3002\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528\u5b9e\u9a8c\u6027\u7684\u5c5e\u6027\u521d\u59cb\u5316\u5668\u8bed\u6cd5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5c5e\u6027\u521d\u59cb\u5316\u5668\u6765\u6b63\u786e\u7684\u7ed1\u5b9a\u56de\u8c03\u51fd\u6570\uff1a<\/p>\n

    \r\nclass LoggingButton extends React.Component {\r\n  \/\/ \u8fd9\u4e2a\u8bed\u6cd5\u786e\u4fdd\u4e86 `this` \u7ed1\u5b9a\u5728  handleClick \u4e2d\r\n  \/\/ \u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u6d4b\u8bd5\r\n  handleClick = () => {\r\n    console.log('this is:', this);\r\n  }\r\n \r\n  render() {\r\n    return (\r\n      <button onClick={this.handleClick}>\r\n        Click me\r\n      <\/button>\r\n    );\r\n  }\r\n}<\/pre>\n

    \u5982\u679c\u4f60\u6ca1\u6709\u4f7f\u7528\u5c5e\u6027\u521d\u59cb\u5316\u5668\u8bed\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u4f7f\u7528 \u7bad\u5934\u51fd\u6570\uff1a<\/p>\n

    \r\nclass LoggingButton extends React.Component {\r\n  handleClick() {\r\n    console.log('this is:', this);\r\n  }\r\n \r\n  render() {\r\n    \/\/  \u8fd9\u4e2a\u8bed\u6cd5\u786e\u4fdd\u4e86 `this` \u7ed1\u5b9a\u5728  handleClick \u4e2d\r\n    return (\r\n      <button onClick={(e) => this.handleClick(e)}>\r\n        Click me\r\n      <\/button>\r\n    );\r\n  }\r\n}<\/pre>\n

    \u4f7f\u7528\u8fd9\u4e2a\u8bed\u6cd5\u6709\u4e2a\u95ee\u9898\u5c31\u662f\u6bcf\u6b21 LoggingButton \u6e32\u67d3\u7684\u65f6\u5019\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u4e0d\u540c\u7684\u56de\u8c03\u51fd\u6570\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u6ca1\u6709\u95ee\u9898\u3002\u7136\u800c\u5982\u679c\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u4e00\u4e2a\u5c5e\u6027\u503c\u4f20\u5165\u4f4e\u9636\u7ec4\u4ef6\uff0c\u8fd9\u4e9b\u7ec4\u4ef6\u53ef\u80fd\u4f1a\u8fdb\u884c\u989d\u5916\u7684\u91cd\u65b0\u6e32\u67d3\u3002\u6211\u4eec\u901a\u5e38\u5efa\u8bae\u5728\u6784\u9020\u51fd\u6570\u4e2d\u7ed1\u5b9a\u6216\u4f7f\u7528\u5c5e\u6027\u521d\u59cb\u5316\u5668\u8bed\u6cd5\u6765\u907f\u514d\u8fd9\u7c7b\u6027\u80fd\u95ee\u9898\u3002<\/p>\n

    \u5411\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4f20\u9012\u53c2\u6570<\/strong><\/div>\n

    \u901a\u5e38\u6211\u4eec\u4f1a\u4e3a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4f20\u9012\u989d\u5916\u7684\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u82e5\u662f id \u662f\u4f60\u8981\u5220\u9664\u90a3\u4e00\u884c\u7684 id\uff0c\u4ee5\u4e0b\u4e24\u79cd\u65b9\u5f0f\u90fd\u53ef\u4ee5\u5411\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u4f20\u9012\u53c2\u6570\uff1a<\/p>\n

    \r\n<button onClick={(e) => this.deleteRow(id, e)}>Delete Row<\/button>\r\n<button onClick={this.deleteRow.bind(this, id)}>Delete Row<\/button><\/pre>\n

    \u4e0a\u8ff0\u4e24\u79cd\u65b9\u5f0f\u662f\u7b49\u4ef7\u7684\u3002
    \n\u4e0a\u9762\u4e24\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53c2\u6570 e \u4f5c\u4e3a React \u4e8b\u4ef6\u5bf9\u8c61\u5c06\u4f1a\u88ab\u4f5c\u4e3a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8fdb\u884c\u4f20\u9012\u3002\u901a\u8fc7\u7bad\u5934\u51fd\u6570\u7684\u65b9\u5f0f\uff0c\u4e8b\u4ef6\u5bf9\u8c61\u5fc5\u987b\u663e\u5f0f\u7684\u8fdb\u884c\u4f20\u9012\uff0c\u4f46\u662f\u901a\u8fc7 bind \u7684\u65b9\u5f0f\uff0c\u4e8b\u4ef6\u5bf9\u8c61\u4ee5\u53ca\u66f4\u591a\u7684\u53c2\u6570\u5c06\u4f1a\u88ab\u9690\u5f0f\u7684\u8fdb\u884c\u4f20\u9012\u3002<\/p>\n

    \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u901a\u8fc7 bind \u65b9\u5f0f\u5411\u76d1\u542c\u51fd\u6570\u4f20\u53c2\uff0c\u5728\u7c7b\u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u7684\u76d1\u542c\u51fd\u6570\uff0c\u4e8b\u4ef6\u5bf9\u8c61 e \u8981\u6392\u5728\u6240\u4f20\u9012\u53c2\u6570\u7684\u540e\u9762\uff0c\u4f8b\u5982:<\/p>\n

    \r\nclass Popper extends React.Component{\r\n    constructor(){\r\n        super();\r\n        this.state = {name:'Hello world!'};\r\n    }\r\n    \r\n    preventPop(name, e){    \/\/\u4e8b\u4ef6\u5bf9\u8c61e\u8981\u653e\u5728\u6700\u540e\r\n        e.preventDefault();\r\n        alert(name);\r\n    }\r\n    \r\n    render(){\r\n        return (\r\n            <div>\r\n                <p>hello<\/p>\r\n                {\/* \u901a\u8fc7 bind() \u65b9\u6cd5\u4f20\u9012\u53c2\u6570\u3002 *\/}\r\n                <a href=\"https:\/\/reactjs.org\" onClick={this.preventPop.bind(this,this.state.name)}>Click<\/a>\r\n            <\/div>\r\n        );\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"

    React \u5143\u7d20\u7684\u4e8b\u4ef6\u5904\u7406\u548c DOM \u5143\u7d20\u7c7b\u4f3c\u3002\u4f46\u662f\u6709\u4e00\u70b9\u8bed\u6cd5\u4e0a\u7684\u4e0d\u540c: 1.React \u4e8b\u4ef6\u7ed1\u5b9a\u5c5e\u6027\u7684\u547d\u540d\u91c7 […]<\/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-207533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/207533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=207533"}],"version-history":[{"count":2,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/207533\/revisions"}],"predecessor-version":[{"id":207535,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/207533\/revisions\/207535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/207359"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=207533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=207533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=207533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}