{"id":207526,"date":"2020-12-24T09:13:08","date_gmt":"2020-12-24T01:13:08","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=207526"},"modified":"2020-12-21T10:14:34","modified_gmt":"2020-12-21T02:14:34","slug":"react-study-state","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/react-study-state.html","title":{"rendered":"React\u5b66\u4e60\u7b14\u8bb0-State(\u72b6\u6001)"},"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 \u628a\u7ec4\u4ef6\u770b\u6210\u662f\u4e00\u4e2a\u72b6\u6001\u673a\uff08State Machines\uff09\u3002\u901a\u8fc7\u4e0e\u7528\u6237\u7684\u4ea4\u4e92\uff0c\u5b9e\u73b0\u4e0d\u540c\u72b6\u6001\uff0c\u7136\u540e\u6e32\u67d3 UI\uff0c\u8ba9\u7528\u6237\u754c\u9762\u548c\u6570\u636e\u4fdd\u6301\u4e00\u81f4\u3002
\nReact \u91cc\uff0c\u53ea\u9700\u66f4\u65b0\u7ec4\u4ef6\u7684 state\uff0c\u7136\u540e\u6839\u636e\u65b0\u7684 state \u91cd\u65b0\u6e32\u67d3\u7528\u6237\u754c\u9762\uff08\u4e0d\u8981\u64cd\u4f5c DOM\uff09\u3002
\n\u4ee5\u4e0b\u5b9e\u4f8b\u521b\u5efa\u4e00\u4e2a\u540d\u79f0\u6269\u5c55\u4e3a React.Component \u7684 ES6 \u7c7b\uff0c\u5728 render() \u65b9\u6cd5\u4e2d\u4f7f\u7528 this.state \u6765\u4fee\u6539\u5f53\u524d\u7684\u65f6\u95f4\u3002
\n\u6dfb\u52a0\u4e00\u4e2a\u7c7b\u6784\u9020\u51fd\u6570\u6765\u521d\u59cb\u5316\u72b6\u6001 this.state\uff0c\u7c7b\u7ec4\u4ef6\u5e94\u59cb\u7ec8\u4f7f\u7528 props \u8c03\u7528\u57fa\u7840\u6784\u9020\u51fd\u6570\u3002<\/p>\n

React \u5b9e\u4f8b<\/p>\n

\r\nclass Clock extends React.Component {\r\n  constructor(props) {\r\n    super(props);\r\n    this.state = {date: new Date()};\r\n  }\r\n \r\n  render() {\r\n    return (\r\n      <div>\r\n        <h1>Hello, world!<\/h1>\r\n        <h2>\u73b0\u5728\u662f {this.state.date.toLocaleTimeString()}.<\/h2>\r\n      <\/div>\r\n    );\r\n  }\r\n}\r\n \r\nReactDOM.render(\r\n  <Clock \/>,\r\n  document.getElementById('example')\r\n);<\/pre>\n

\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4f7fClock\u8bbe\u7f6e\u81ea\u5df1\u7684\u8ba1\u65f6\u5668\u5e76\u6bcf\u79d2\u66f4\u65b0\u4e00\u6b21\u3002<\/p>\n

\u5c06\u751f\u547d\u5468\u671f\u65b9\u6cd5\u6dfb\u52a0\u5230\u7c7b\u4e2d<\/strong><\/div>\n

\u5728\u5177\u6709\u8bb8\u591a\u7ec4\u4ef6\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5728\u9500\u6bc1\u65f6\u91ca\u653e\u7ec4\u4ef6\u6240\u5360\u7528\u7684\u8d44\u6e90\u975e\u5e38\u91cd\u8981\u3002
\n\u6bcf\u5f53 Clock \u7ec4\u4ef6\u7b2c\u4e00\u6b21\u52a0\u8f7d\u5230 DOM \u4e2d\u7684\u65f6\u5019\uff0c\u6211\u4eec\u90fd\u60f3\u751f\u6210\u5b9a\u65f6\u5668\uff0c\u8fd9\u5728 React \u4e2d\u88ab\u79f0\u4e3a\u6302\u8f7d\u3002
\n\u540c\u6837\uff0c\u6bcf\u5f53 Clock \u751f\u6210\u7684\u8fd9\u4e2a DOM \u88ab\u79fb\u9664\u7684\u65f6\u5019\uff0c\u6211\u4eec\u4e5f\u4f1a\u60f3\u8981\u6e05\u9664\u5b9a\u65f6\u5668\uff0c\u8fd9\u5728 React \u4e2d\u88ab\u79f0\u4e3a\u5378\u8f7d\u3002
\n\u6211\u4eec\u53ef\u4ee5\u5728\u7ec4\u4ef6\u7c7b\u4e0a\u58f0\u660e\u7279\u6b8a\u7684\u65b9\u6cd5\uff0c\u5f53\u7ec4\u4ef6\u6302\u8f7d\u6216\u5378\u8f7d\u65f6\uff0c\u6765\u8fd0\u884c\u4e00\u4e9b\u4ee3\u7801\uff1a<\/p>\n

React \u5b9e\u4f8b<\/p>\n

\r\nclass Clock extends React.Component {\r\n  constructor(props) {\r\n    super(props);\r\n    this.state = {date: new Date()};\r\n  }\r\n \r\n  componentDidMount() {\r\n    this.timerID = setInterval(\r\n      () => this.tick(),\r\n      1000\r\n    );\r\n  }\r\n \r\n  componentWillUnmount() {\r\n    clearInterval(this.timerID);\r\n  }\r\n \r\n  tick() {\r\n    this.setState({\r\n      date: new Date()\r\n    });\r\n  }\r\n \r\n  render() {\r\n    return (\r\n      <div>\r\n        <h1>Hello, world!<\/h1>\r\n        <h2>\u73b0\u5728\u662f {this.state.date.toLocaleTimeString()}.<\/h2>\r\n      <\/div>\r\n    );\r\n  }\r\n}\r\n \r\nReactDOM.render(\r\n  <Clock \/>,\r\n  document.getElementById('example')\r\n);\r\n<\/pre>\n

\u5b9e\u4f8b\u89e3\u6790\uff1a<\/p>\n

componentDidMount() \u4e0e componentWillUnmount() \u65b9\u6cd5\u88ab\u79f0\u4f5c\u751f\u547d\u5468\u671f\u94a9\u5b50\u3002
\n\u5728\u7ec4\u4ef6\u8f93\u51fa\u5230 DOM \u540e\u4f1a\u6267\u884c componentDidMount() \u94a9\u5b50\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u8fd9\u4e2a\u94a9\u5b50\u4e0a\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\u3002
\nthis.timerID \u4e3a\u5b9a\u65f6\u5668\u7684 ID\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 componentWillUnmount() \u94a9\u5b50\u4e2d\u5378\u8f7d\u5b9a\u65f6\u5668\u3002
\n\u4ee3\u7801\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n

    \n
  • 1.\u5f53 <Clock \/> \u88ab\u4f20\u9012\u7ed9 ReactDOM.render() \u65f6\uff0cReact \u8c03\u7528 Clock \u7ec4\u4ef6\u7684\u6784\u9020\u51fd\u6570\u3002 \u7531\u4e8e Clock \u9700\u8981\u663e\u793a\u5f53\u524d\u65f6\u95f4\uff0c\u6240\u4ee5\u4f7f\u7528\u5305\u542b\u5f53\u524d\u65f6\u95f4\u7684\u5bf9\u8c61\u6765\u521d\u59cb\u5316 this.state \u3002 \u6211\u4eec\u7a0d\u540e\u4f1a\u66f4\u65b0\u6b64\u72b6\u6001\u3002<\/li>\n
  • 2.React \u7136\u540e\u8c03\u7528 Clock \u7ec4\u4ef6\u7684 render() \u65b9\u6cd5\u3002\u8fd9\u662f React \u4e86\u89e3\u5c4f\u5e55\u4e0a\u5e94\u8be5\u663e\u793a\u4ec0\u4e48\u5185\u5bb9\uff0c\u7136\u540e React \u66f4\u65b0 DOM \u4ee5\u5339\u914d Clock \u7684\u6e32\u67d3\u8f93\u51fa\u3002<\/li>\n
  • 3.\u5f53 Clock \u7684\u8f93\u51fa\u63d2\u5165\u5230 DOM \u4e2d\u65f6\uff0cReact \u8c03\u7528 componentDidMount() \u751f\u547d\u5468\u671f\u94a9\u5b50\u3002 \u5728\u5176\u4e2d\uff0cClock \u7ec4\u4ef6\u8981\u6c42\u6d4f\u89c8\u5668\u8bbe\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c\u6bcf\u79d2\u949f\u8c03\u7528\u4e00\u6b21 tick()\u3002<\/li>\n
  • 4.\u6d4f\u89c8\u5668\u6bcf\u79d2\u949f\u8c03\u7528 tick() \u65b9\u6cd5\u3002 \u5728\u5176\u4e2d\uff0cClock \u7ec4\u4ef6\u901a\u8fc7\u4f7f\u7528\u5305\u542b\u5f53\u524d\u65f6\u95f4\u7684\u5bf9\u8c61\u8c03\u7528 setState() \u6765\u8c03\u5ea6UI\u66f4\u65b0\u3002 \u901a\u8fc7\u8c03\u7528 setState() \uff0cReact \u77e5\u9053\u72b6\u6001\u5df2\u7ecf\u6539\u53d8\uff0c\u5e76\u518d\u6b21\u8c03\u7528 render() \u65b9\u6cd5\u6765\u786e\u5b9a\u5c4f\u5e55\u4e0a\u5e94\u5f53\u663e\u793a\u4ec0\u4e48\u3002 \u8fd9\u4e00\u6b21\uff0crender() \u65b9\u6cd5\u4e2d\u7684 this.state.date \u5c06\u4e0d\u540c\uff0c\u6240\u4ee5\u6e32\u67d3\u8f93\u51fa\u5c06\u5305\u542b\u66f4\u65b0\u7684\u65f6\u95f4\uff0c\u5e76\u76f8\u5e94\u5730\u66f4\u65b0 DOM\u3002<\/li>\n
  • 5.\u4e00\u65e6 Clock \u7ec4\u4ef6\u88ab\u4ece DOM \u4e2d\u79fb\u9664\uff0cReact \u4f1a\u8c03\u7528 componentWillUnmount() \u8fd9\u4e2a\u94a9\u5b50\u51fd\u6570\uff0c\u5b9a\u65f6\u5668\u4e5f\u5c31\u4f1a\u88ab\u6e05\u9664\u3002<\/li>\n<\/ul>\n
    \u6570\u636e\u81ea\u9876\u5411\u4e0b\u6d41\u52a8<\/strong><\/div>\n

    \u7236\u7ec4\u4ef6\u6216\u5b50\u7ec4\u4ef6\u90fd\u4e0d\u80fd\u77e5\u9053\u67d0\u4e2a\u7ec4\u4ef6\u662f\u6709\u72b6\u6001\u8fd8\u662f\u65e0\u72b6\u6001\uff0c\u5e76\u4e14\u5b83\u4eec\u4e0d\u5e94\u8be5\u5173\u5fc3\u67d0\u7ec4\u4ef6\u662f\u88ab\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u51fd\u6570\u8fd8\u662f\u4e00\u4e2a\u7c7b\u3002
    \n\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u72b6\u6001\u901a\u5e38\u88ab\u79f0\u4e3a\u5c40\u90e8\u6216\u5c01\u88c5\u3002 \u9664\u4e86\u62e5\u6709\u5e76\u8bbe\u7f6e\u5b83\u7684\u7ec4\u4ef6\u5916\uff0c\u5176\u5b83\u7ec4\u4ef6\u4e0d\u53ef\u8bbf\u95ee\u3002
    \n\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d FormattedDate \u7ec4\u4ef6\u5c06\u5728\u5176\u5c5e\u6027\u4e2d\u63a5\u6536\u5230 date \u503c\uff0c\u5e76\u4e14\u4e0d\u77e5\u9053\u5b83\u662f\u6765\u81ea Clock \u72b6\u6001\u3001\u8fd8\u662f\u6765\u81ea Clock \u7684\u5c5e\u6027\u3001\u4ea6\u6216\u624b\u5de5\u8f93\u5165\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nfunction FormattedDate(props) {\r\n  return <h2>\u73b0\u5728\u662f {props.date.toLocaleTimeString()}.<\/h2>;\r\n}\r\n \r\nclass Clock extends React.Component {\r\n  constructor(props) {\r\n    super(props);\r\n    this.state = {date: new Date()};\r\n  }\r\n \r\n  componentDidMount() {\r\n    this.timerID = setInterval(\r\n      () => this.tick(),\r\n      1000\r\n    );\r\n  }\r\n \r\n  componentWillUnmount() {\r\n    clearInterval(this.timerID);\r\n  }\r\n \r\n  tick() {\r\n    this.setState({\r\n      date: new Date()\r\n    });\r\n  }\r\n \r\n  render() {\r\n    return (\r\n      <div>\r\n        <h1>Hello, world!<\/h1>\r\n        <FormattedDate date={this.state.date} \/>\r\n      <\/div>\r\n    );\r\n  }\r\n}\r\n \r\nReactDOM.render(\r\n  <Clock \/>,\r\n  document.getElementById('example')\r\n);\r\n<\/pre>\n

    \u8fd9\u901a\u5e38\u88ab\u79f0\u4e3a\u81ea\u9876\u5411\u4e0b\u6216\u5355\u5411\u6570\u636e\u6d41\u3002 \u4efb\u4f55\u72b6\u6001\u59cb\u7ec8\u7531\u67d0\u4e9b\u7279\u5b9a\u7ec4\u4ef6\u6240\u6709\uff0c\u5e76\u4e14\u4ece\u8be5\u72b6\u6001\u5bfc\u51fa\u7684\u4efb\u4f55\u6570\u636e\u6216 UI \u53ea\u80fd\u5f71\u54cd\u6811\u4e2d\u4e0b\u65b9\u7684\u7ec4\u4ef6\u3002
    \n\u5982\u679c\u4f60\u60f3\u8c61\u4e00\u4e2a\u7ec4\u4ef6\u6811\u4f5c\u4e3a\u5c5e\u6027\u7684\u7011\u5e03\uff0c\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u72b6\u6001\u5c31\u50cf\u4e00\u4e2a\u989d\u5916\u7684\u6c34\u6e90\uff0c\u5b83\u8fde\u63a5\u5728\u4e00\u4e2a\u4efb\u610f\u70b9\uff0c\u4f46\u4e5f\u6d41\u4e0b\u6765\u3002
    \n\u4e3a\u4e86\u8868\u660e\u6240\u6709\u7ec4\u4ef6\u90fd\u662f\u771f\u6b63\u9694\u79bb\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a App \u7ec4\u4ef6\uff0c\u5b83\u6e32\u67d3\u4e09\u4e2aClock\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nfunction FormattedDate(props) {\r\n  return <h2>\u73b0\u5728\u662f {props.date.toLocaleTimeString()}.<\/h2>;\r\n}\r\n \r\nclass Clock extends React.Component {\r\n  constructor(props) {\r\n    super(props);\r\n    this.state = {date: new Date()};\r\n  }\r\n \r\n  componentDidMount() {\r\n    this.timerID = setInterval(\r\n      () => this.tick(),\r\n      1000\r\n    );\r\n  }\r\n \r\n  componentWillUnmount() {\r\n    clearInterval(this.timerID);\r\n  }\r\n \r\n  tick() {\r\n    this.setState({\r\n      date: new Date()\r\n    });\r\n  }\r\n \r\n  render() {\r\n    return (\r\n      <div>\r\n        <h1>Hello, world!<\/h1>\r\n        <FormattedDate date={this.state.date} \/>\r\n      <\/div>\r\n    );\r\n  }\r\n}\r\n \r\nfunction App() {\r\n  return (\r\n    <div>\r\n      <Clock \/>\r\n      <Clock \/>\r\n      <Clock \/>\r\n    <\/div>\r\n  );\r\n}\r\n \r\nReactDOM.render(<App \/>, document.getElementById('example'));<\/pre>\n

    \u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\u6bcf\u4e2a Clock \u7ec4\u4ef6\u90fd\u5efa\u7acb\u4e86\u81ea\u5df1\u7684\u5b9a\u65f6\u5668\u5e76\u4e14\u72ec\u7acb\u66f4\u65b0\u3002
    \n\u5728 React \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u7ec4\u4ef6\u662f\u6709\u72b6\u6001\u8fd8\u662f\u65e0\u72b6\u6001\u88ab\u8ba4\u4e3a\u662f\u53ef\u80fd\u968f\u65f6\u95f4\u800c\u53d8\u5316\u7684\u7ec4\u4ef6\u7684\u5b9e\u73b0\u7ec6\u8282\u3002
    \n\u6211\u4eec\u53ef\u4ee5\u5728\u6709\u72b6\u6001\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u65e0\u72b6\u6001\u7ec4\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u5728\u65e0\u72b6\u6001\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u6709\u72b6\u6001\u7ec4\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    React \u628a\u7ec4\u4ef6\u770b\u6210\u662f\u4e00\u4e2a\u72b6\u6001\u673a\uff08State Machines\uff09\u3002\u901a\u8fc7\u4e0e\u7528\u6237\u7684\u4ea4\u4e92\uff0c\u5b9e\u73b0\u4e0d\u540c\u72b6\u6001\uff0c\u7136\u540e\u6e32\u67d3 […]<\/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-207526","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\/207526","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=207526"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207526\/revisions"}],"predecessor-version":[{"id":207529,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207526\/revisions\/207529"}],"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=207526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=207526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=207526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}