{"id":32559,"date":"2024-11-25T13:44:45","date_gmt":"2024-11-25T05:44:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32559\/"},"modified":"2024-11-25T13:44:45","modified_gmt":"2024-11-25T05:44:45","slug":"%e6%8e%a2%e7%b4%a2-react-%e5%8a%a8%e7%94%bb%e7%9a%84%e4%b8%96%e7%95%8c%ef%bc%9a%e7%ae%80%e4%bb%8b","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32559\/","title":{"rendered":"\u63a2\u7d22 React \u52a8\u753b\u7684\u4e16\u754c\uff1a\u7b80\u4ecb"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169371511433020.jpg\" class=\"aligncenter\" title=\"\u63a2\u7d22 React \u52a8\u753b\u7684\u4e16\u754c\uff1a\u7b80\u4ecb\u63d2\u56fe\" alt=\"\u63a2\u7d22 React \u52a8\u753b\u7684\u4e16\u754c\uff1a\u7b80\u4ecb\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u52a8\u753b\u662f\u6539\u5584\u6574\u4f53\u7528\u6237\u4f53\u9a8c\u7684\u597d\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u548c\u7528\u6237\u4e4b\u95f4\u8fdb\u884c\u66f4\u597d\u7684\u4ea4\u4e92\u3002<\/p>\n<p>\u5728\u6211\u4eec\u4e4b\u524d\u7684\u4e00\u4e9b React \u6559\u7a0b\u4e2d\uff0c\u60a8\u719f\u6089\u4e86\u57fa\u672c\u7684 React \u6982\u5ff5\uff0c\u4f8b\u5982 JSX\u3001\u8def\u7531\u548c\u8868\u5355\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u63d0\u5347\u5230\u4e00\u4e2a\u65b0\u7684\u6c34\u5e73\uff0c\u5e76\u5c1d\u8bd5\u7406\u89e3 React \u4e2d\u7684\u52a8\u753b\u3002\u867d\u7136\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5411 React \u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u52a8\u753b\uff0c\u4f46\u6211\u4eec\u5c06\u5728\u672c\u6587\u4e2d\u91cd\u70b9\u4ecb\u7ecd React Transition Group \u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002<\/p>\n<h2>React \u4e2d\u7684\u52a8\u753b<\/h2>\n<p>React \u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u52a8\u753b React \u5e94\u7528\u7a0b\u5e8f\u7684\u9644\u52a0\u5b9e\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u4e4b\u4e00\u79f0\u4e3a React Transition Group\uff0c\u7531 React \u5f00\u53d1\u56e2\u961f\u521b\u5efa\u3002<\/p>\n<p>\u5b83\u4e0d\u662f\u4e00\u4e2a\u8bbe\u7f6e\u52a8\u753b\u6837\u5f0f\u7684\u5e93\uff1b\u76f8\u53cd\uff0c\u5b83\u662f\u4e00\u4e2a\u5177\u6709\u56db\u79cd\u7c7b\u578b\u5185\u7f6e\u7ec4\u4ef6\u7684\u4f4e\u7ea7 API\uff1aTransition\u3001CSSTransition\u3001SwitchTransition \u548c TransitionGroup\u3002\u56e0\u6b64\uff0c\u5728\u72b6\u6001\u66f4\u6539\u671f\u95f4\u5c06 React \u7ec4\u4ef6\u52a8\u753b\u79fb\u5165\u548c\u79fb\u51fa DOM \u975e\u5e38\u7b80\u5355\u3002<\/p>\n<p>React Transition Group \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u5165\u95e8\u5de5\u5177\uff0c\u800c\u4e14\u7531\u4e8e\u5b83\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u51cf\u5c11\u5bf9\u6837\u677f\u4ee3\u7801\u7684\u9700\u6c42\uff0c\u4ece\u800c\u52a0\u5feb\u5f00\u53d1\u8fc7\u7a0b\u3002<\/p>\n<h2>\u5f00\u59cb\u4f7f\u7528<\/h2>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528 create-react-app \u5305\u5b89\u88c5 react \u3002<\/p>\n<pre>npx create-react-app react-animations\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6253\u5f00\u516c\u5171\u6587\u4ef6\u5939\u7684<strong>index.html<\/strong>\u6587\u4ef6\u5e76\u7f16\u8f91\u6807\u9898\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;title&gt;TutsPlus - React Animations&lt;\/title&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8ba9\u6211\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u7684 <strong>src <\/strong> \u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <strong>components<\/strong> \u7684\u6587\u4ef6\u5939\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a <strong>Home.js<\/strong> \u6587\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Home \u7684\u529f\u80fd\u7ec4\u4ef6\u5e76\u6e32\u67d3\u4e00\u4e2a h2 \u6807\u7b7e\u6765\u66f4\u65b0\u6b64\u6587\u4ef6\u3002<\/p>\n<pre>import React from \"react\";\n\nconst Home = () =&amp;gt; {\n  return (\n    \n      &lt;h2&gt;{\"TutsPlus - Welcome to React Animations!\"}&lt;\/h2&gt;\n    &amp;gt;\n  );\n};\n\nexport default Home;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u901a\u8fc7\u5bfc\u5165 Home \u7ec4\u4ef6\u6765\u66f4\u65b0 <strong>App.js<\/strong> \u6587\u4ef6\uff1a<\/p>\n<pre>import React from \"react\";\nimport Home from \".\/components\/Home\";\n\nconst App = () =&amp;gt; {\n  return (\n    \n      &lt;home&gt;&lt;\/home&gt;&amp;gt;\n  );\n};\n\nexport default App;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u901a\u8fc7\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8\u5f00\u53d1\u670d\u52a1\u5668\uff1a<\/p>\n<pre>npm run start\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>\u53cd\u5e94\u8fc7\u6e21\u7ec4\u8bbe\u7f6e<\/h2>\n<p>\u8ba9\u6211\u4eec\u9996\u5148\u5728 React \u4e2d\u5c1d\u8bd5\u4e00\u4e2a\u7b80\u5355\u7684\u52a8\u753b\uff0c\u65b9\u6cd5\u662f\u5c06 react-transition-group \u5305\u5b89\u88c5\u5230\u9879\u76ee\u4e2d\u3002<\/p>\n<pre>npm install react-transition-group\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4ece <strong>Home.js<\/strong> \u6587\u4ef6\u4e2d\u7684 react-transition-group \u5305\u5bfc\u5165\u524d\u9762\u63d0\u5230\u7684\u56db\u4e2a\u7ec4\u4ef6\u3002<\/p>\n<pre>import {Transition, CSSTransition, SwitchTransition, TransitionGroup} from \"react-transition-group\";\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u6bcf\u4e2a\u7ec4\u4ef6\u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<h3>\u8f6c\u6362\u7ec4\u4ef6<\/h3>\n<p>Transition \u7ec4\u4ef6\u63d0\u4f9b\u4e86\u4e00\u4e2a API\uff0c\u7528\u4e8e\u5b9a\u4e49\u7ec4\u4ef6\u5728\u5b89\u88c5\u548c\u5378\u8f7d\u671f\u95f4\u4ece\u4e00\u79cd\u72b6\u6001\u5230\u53e6\u4e00\u79cd\u72b6\u6001\u7684\u8f6c\u6362\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5728 Home \u7ec4\u4ef6\u4e2d\uff0c\u5c06 h2 \u6807\u7b7e\u5305\u88c5\u5728 Transition \u7ec4\u4ef6\u4e2d\uff0c\u5e76\u50cf\u8fd9\u6837\u66f4\u65b0\u4ee3\u7801\u3002<\/p>\n<pre>import React, { useState } from \"react\";\n\nconst duration = 300;\n\nconst defaultStyle = {\n  transition: `opacity ${duration}ms ease-in-out`,\n  opacity: 0,\n};\n\nconst transitionStyles = {\n  entering: { opacity: 1 },\n  entered: { opacity: 1 },\n  exiting: { opacity: 0 },\n  exited: { opacity: 0 },\n};\n\nconst Home = () =&amp;gt; {\n  const [inProp, setInProp] = useState(false);\n  return (\n    \n      &lt;div&gt;\n        &lt;transition in=\"{inProp}\" timeout=\"{300}\"&gt;\n          {(state) =&amp;gt; (\n            &lt;h2 style=\"{{\" ...defaultstyle ...transitionstyles&gt;\n              {\"TutsPlus - Welcome to React Animations\"}\n            &lt;\/h2&gt;\n          )}\n        &lt;\/transition&gt;&lt;button onclick=\"{()\"&gt; setInProp(!inProp)}&amp;gt;\n          Click to {inProp ? \"Exit\" : \"Enter\"}\n        &lt;\/button&gt;\n      &lt;\/div&gt;\n    &amp;gt;\n  );\n};\n\nexport default Home;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528 Transition \u6807\u7b7e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u52a8\u753b\u53d1\u751f\u7684\u90e8\u5206\u3002\u6211\u4eec\u8fd8\u4f7f\u7528 inProp \u72b6\u6001\u4e3a\u8f6c\u6362\u6307\u5b9a\u4e86 in \u5c5e\u6027\uff0c\u8fd9\u4f1a\u5207\u6362\u8f6c\u6362\u72b6\u6001\u3002<\/p>\n<p>\u6b63\u5982\u60a8\u6240\u6ce8\u610f\u5230\u7684\uff0c\u6211\u4eec\u5728\u4e0a\u9762\u7684 defaultStyle \u548c Transition \u7ec4\u4ef6\u4e2d\u4f7f\u7528 timeout \u5c5e\u6027\u6307\u5b9a\u4e86\u52a8\u753b\u6301\u7eed\u65f6\u95f4\u3002\u8fd9\u662f\u56e0\u4e3a React \u5c31\u662f\u8fd9\u6837\u77e5\u9053\u4f55\u65f6\u4ece\u5143\u7d20\u4e2d\u5220\u9664\u52a8\u753b\u7c7b\u4ee5\u53ca\u4f55\u65f6\u4ece DOM \u4e2d\u5220\u9664\u5143\u7d20\u7684\u3002<\/p>\n<p>\u4fdd\u5b58\u4ee5\u4e0a\u66f4\u6539\u5e76\u5237\u65b0\u9875\u9762\u3002\u9875\u9762\u52a0\u8f7d\u540e\uff0c\u51e0\u79d2\u949f\u5185\u60a8\u5c31\u5e94\u8be5\u80fd\u591f\u770b\u5230\u52a8\u753b\u6587\u672c\u3002<\/p>\n<h3>CSSTransition \u7ec4\u4ef6<\/h3>\n<p>\u5f53\u5c1d\u8bd5\u5728 React \u7ec4\u4ef6\u4e2d\u5b9e\u73b0\u57fa\u4e8e CSS \u7684\u52a8\u753b\u65f6\uff0cCSSTransition \u7ec4\u4ef6\u4f1a\u6d3e\u4e0a\u7528\u573a\u3002<\/p>\n<p>\u56e0\u4e3a\u8be5\u7ec4\u4ef6\u57fa\u4e8e Transition \u7ec4\u4ef6\uff0c\u6240\u4ee5\u5b83\u7ee7\u627f\u4e86\u8be5\u7ec4\u4ef6\u7684\u6240\u6709 props\uff0c\u5e76\u4e14\u8fd8\u4f7f\u7528\u51e0\u4e2a\u7c7b\u6765\u5b9a\u4e49\u8f6c\u6362\u3002<\/p>\n<p>\u8981\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u6211\u4eec\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230 <strong>index.css<\/strong> \u6587\u4ef6\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>.react-animations-enter {\n  opacity: 0;\n}\n.react-animations-enter-active {\n  opacity: 1;\n  transition: opacity 200ms;\n}\n.react-animations-exit {\n  opacity: 1;\n}\n.react-animations-exit-active {\n  opacity: 0;\n  transition: opacity 200ms;\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ece *-enter \u5230 *-exit-active\uff0c\u6bcf\u4e2a\u7c7b\u5b9a\u4e49\u4e86\u7ec4\u4ef6\u5904\u4e8e\u201c\u8fdb\u5165\u201d\u3001\u201c\u8fdb\u5165\u201d\u3001\u201c\u9000\u51fa\u201d\u65f6\u7684\u8f6c\u6362\uff0c\u548c\u201c\u9000\u51fa\u201d\u72b6\u6001\u3002<\/p>\n<p>\u7136\u540e\uff0c\u5728 <strong>Home.js<\/strong> \u4e2d\uff0c\u6211\u4eec\u5c06\u7ec4\u4ef6\u5185\u5bb9\u5305\u88c5\u5230 CSSTransition \u6807\u7b7e\u4e2d\uff0c\u4f20\u5165 in \u548c timeout \u5c5e\u6027\u4ee5\u53ca\u6211\u4eec\u4e4b\u524d\u5b9a\u4e49\u7684\u7c7b\uff1a<\/p>\n<pre>&lt;div&gt;\n    &lt;csstransition in=\"{displayText}\" timeout=\"{300}\" classnames=\"react-animations\" unmountonexit&gt;&lt;h2&gt;{\"TutsPlus - Welcome to CSSTransition\"}&lt;\/h2&gt;\n    &lt;\/csstransition&gt;&lt;button onclick=\"{()\"&gt; setDisplayText(!displayText)}&amp;gt;\n          Display Text\n        &lt;\/button&gt;\n&lt;\/div&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u9762\u7684 classNames \u5c5e\u6027\u6709\u4e00\u4e2a react-animations \u503c\uff0c\u8be5\u503c\u9002\u7528\u4e8e\u5b9a\u4e49\u7684\u6240\u6709\u7c7b\u3002<\/p>\n<h3>SwitchTransition \u7c7b<\/h3>\n<p>\u987e\u540d\u601d\u4e49\uff0c\u5f53\u60a8\u60f3\u8981\u6839\u636e\u9009\u5b9a\u7684\u6a21\u5f0f\uff08\u8f93\u5165-\u8f93\u51fa\u6216\u8f93\u51fa-\u8f93\u5165\uff09\u5728\u72b6\u6001\u8f6c\u6362\u4e4b\u95f4\u5207\u6362\u6e32\u67d3\u65f6\uff0c\u6b64\u7ec4\u4ef6\u975e\u5e38\u6709\u7528\u3002\u5f53\u60a8\u5e0c\u671b\u4e00\u4e2a\u7ec4\u4ef6\u5728\u63d2\u5165\u53e6\u4e00\u4e2a\u7ec4\u4ef6\u65f6\u6de1\u51fa\u65f6\uff0c\u5b83\u4f1a\u5f88\u6709\u7528\u3002<\/p>\n<p>\u8981\u8bbf\u95ee\u6b64\u5b9e\u7528\u7a0b\u5e8f\u7684\u5c5e\u6027\uff0c\u6211\u4eec\u8fd8\u5c06\u7ec4\u4ef6\u7684\u5185\u5bb9\u5305\u88c5\u5728 SwitchTransition \u6807\u8bb0\u4e2d\u3002\u8fd8\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cSwitchTransition \u5e94\u4e0e Transition \u6216 CSSTransition \u7ec4\u4ef6\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u5c06\u4ee5\u4e0b\u4ee3\u7801\u6dfb\u52a0\u5230 index.css \u6587\u4ef6\u4e2d\uff0c\u5982\u4e0b\u6240\u793a\u6765\u521b\u5efa\u6211\u4eec\u7684\u7c7b\uff1a<\/p>\n<pre>.fade-enter{\n   opacity: 0;\n}\n\n.fade-exit{\n   opacity: 1;\n}\n\n.fade-enter-active{\n   opacity: 1;\n}\n\n.fade-exit-active{\n   opacity: 0;\n}\n\n.fade-enter-active,\n.fade-exit-active{\n   transition: opacity 500ms;\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u770b\u5b83\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u4ece out-in \u6a21\u5f0f\u5f00\u59cb\uff0c\u8fd9\u662f\u9ed8\u8ba4\u6a21\u5f0f\uff1a<\/p>\n<pre> const [state, setState] = useState(false);\n \n   &lt;switchtransition&gt;&lt;csstransition key=\"{state\" you enjoy our tutorial : to tutsplus addendlistener=\"{(node,\" done&gt; node.addEventListener(\"transitionend\", done, false)}\n       classNames='fade'\n     &amp;gt;\n       &lt;button onclick=\"{()\"&gt; setState(state =&amp;gt; !state)}&amp;gt;\n         {state ? \"Did you Enjoy our Tutorial?\" : \"Welcome to TutsPlus\"}\n       &lt;\/button&gt;\n     &lt;\/csstransition&gt;&lt;\/switchtransition&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684 key \u5c5e\u6027\u4f1a\u8ddf\u8e2a\u7ec4\u4ef6\u4e2d\u7684\u72b6\u6001\uff0c\u800c addEndListener \u5c5e\u6027\u4f1a\u963b\u6b62\u7ec4\u4ef6\u51e0\u4e4e\u7acb\u5373\u7ffb\u8f6c\u3002\u5982\u679c\u6ca1\u6709\u5b83\uff0c\u770b\u8d77\u6765\u5c31\u50cf\u6ca1\u6709\u5b9e\u73b0\u52a8\u753b\u4e00\u6837\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u662f\u8f93\u5165\u8f93\u51fa\u6a21\u5f0f\uff0c\u5176\u4e2d SwitchTransition \u6807\u8bb0\u91c7\u7528 mode \u5c5e\u6027\u4ee5\u53ca in-out \u503c\u3002\u73b0\u5728\u66f4\u65b0\u60a8\u7684\u4ee3\u7801\u4ee5\u67e5\u770b\u5176\u5de5\u4f5c\u539f\u7406\uff1a<\/p>\n<pre>&lt;switchtransition mode='{\"in-out\"}'&gt;\n    {Code goes here}\n&lt;\/switchtransition&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u8fc7\u6e21\u7ec4<\/h3>\n<p>\u8be5\u7ec4\u4ef6\u6709\u52a9\u4e8e\u7ba1\u7406\u5217\u8868\u4e2d\u7684 Transition \u6216 CSSTransition \u7ec4\u4ef6\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5e94\u7528\u5b83\u7684\u793a\u4f8b\u3002<\/p>\n<p>\u50cf\u8fd9\u6837\u66f4\u65b0<strong>Home.js<\/strong>\uff1a<\/p>\n<pre>const [items, setItems] = useState([\"Manal\"]);\n\nconst CONTACTS = [\"Jane\", \"Fred\", \"John\", \"Doe\", \"Brown\"];\n\nconst onAddContacts = () =&amp;gt; {\n    const newItem = CONTACTS.find((item) =&amp;gt; !items.includes(item));\n    \n    if (newItem) {\n      setItems((prev) =&amp;gt; [...prev, newItem]);\n    }\n};\n\n&lt;div&gt;\n      &lt;transitiongroup&gt;&lt;h2&gt;Contacts&lt;\/h2&gt;\n        {items.map((item, index) =&amp;gt; (\n          &lt;csstransition key=\"{index}\" timeout=\"{900}\" classnames=\"fade\"&gt;&lt;p&gt;{item}&lt;\/p&gt;\n          &lt;\/csstransition&gt;\n        ))}\n        &lt;button onclick=\"{onAddContacts}\"&gt;Add a Contact&lt;\/button&gt;\n      &lt;\/transitiongroup&gt;\n&lt;\/div&gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4fdd\u5b58\u4ee5\u4e0a\u5185\u5bb9\u5e76\u5237\u65b0\u9875\u9762\u3002\u5355\u51fb\u8be5\u6309\u94ae\uff0c\u8be5\u9879\u76ee\u5e94\u6dfb\u52a0\u5230\u5e26\u6709\u52a8\u753b\u7684\u5217\u8868\u4e2d\u3002<\/p>\n<p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u540d\u4e3a CONTACTS \u7684\u9759\u6001 data \u96c6\u3002\u7136\u540e\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a onAddContacts \u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5c06\u5904\u7406\u6dfb\u52a0\u65b0\u8054\u7cfb\u4eba\u7684\u64cd\u4f5c\uff0c\u5e76\u5728\u6309\u94ae\u4e0a\u89e6\u53d1\u3002<\/p>\n<p>\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\u90fd\u5305\u542b\u5728 CSSTransition \u6807\u8bb0\u4e2d\uff0c\u4ee5\u5bf9\u65b0\u63d2\u5165\u7684\u9879\u76ee\u8fdb\u884c\u52a8\u753b\u5904\u7406\u3002\u6700\u540e\uff0c\u8be5\u7ec4\u4ef6\u88ab\u5305\u88c5\u5728 TransitionGroup \u7ec4\u4ef6\u4e2d\u4ee5\u7ba1\u7406\u5176\u4e2d\u5305\u542b\u7684\u8f6c\u6362\u3002<\/p>\n<p>\u8fd9\u662f\u5b8c\u6574\u7684 <strong>Home.js<\/strong> \u7ec4\u4ef6\uff1a<\/p>\n<pre>import React, { useState } from \"react\";\n\nimport {\n  Transition,\n  CSSTransition,\n  SwitchTransition,\n  TransitionGroup\n} from \"react-transition-group\";\n\nconst duration = 300;\n\nconst defaultStyle = {\n  transition: `opacity ${duration}ms ease-in-out`,\n  opacity: 0,\n};\n\nconst transitionStyles = {\n  entering: { opacity: 1 },\n  entered: { opacity: 1 },\n  exiting: { opacity: 0 },\n  exited: { opacity: 0 },\n};\n\nconst Home = () =&amp;gt; {\n  const [inProp, setInProp] = useState(false);\n  const [displayText, setDisplayText] = useState(false);\n  const [state, setState] = useState(false);\n\n  const [items, setItems] = useState([\"Manal\"]);\n\n  const CONTACTS = [\"Jane\", \"Fred\", \"John\", \"Doe\", \"Brown\"];\n\n  const onAddContacts = () =&amp;gt; {\n    const newItem = CONTACTS.find((item) =&amp;gt; !items.includes(item));\n    if (newItem) {\n      setItems((prev) =&amp;gt; [...prev, newItem]);\n    }\n  };\n\n  return (\n    \n      &lt;div&gt;\n        &lt;transition in=\"{inProp}\" timeout=\"{300}\"&gt;\n          {(state) =&amp;gt; (\n            &lt;h2 style=\"{{\" ...defaultstyle ...transitionstyles&gt;\n              {\"TutsPlus - Welcome to React Animations\"}\n            &lt;\/h2&gt;\n          )}\n        &lt;\/transition&gt;&lt;button onclick=\"{()\"&gt; setInProp(!inProp)}&amp;gt;\n          Click to {inProp ? \"Exit\" : \"Enter\"}\n        &lt;\/button&gt;\n      &lt;\/div&gt;\n\n      &lt;div&gt;\n        &lt;csstransition in=\"{displayText}\" timeout=\"{300}\" classnames=\"react-animations\" unmountonexit&gt;&lt;h2&gt;{\"TutsPlus - Welcome to CSSTransition\"}&lt;\/h2&gt;\n        &lt;\/csstransition&gt;&lt;button onclick=\"{()\"&gt; setDisplayText(!displayText)}&amp;gt;\n          Display Text\n        &lt;\/button&gt;\n      &lt;\/div&gt;\n\n      &lt;div&gt;\n        &lt;switchtransition mode='{\"in-out\"}'&gt;&lt;csstransition key=\"{state\" you enjoy our tutorial : to tutsplus addendlistener=\"{(node,\" done&gt;\n              node.addEventListener(\"transitionend\", done, false)\n            }\n            classNames=\"fade\"\n          &amp;gt;\n            &lt;button onclick=\"{()\"&gt; setState((state) =&amp;gt; !state)}&amp;gt;\n              {state ? \"Did you Enjoy our Tutorial?\" : \"Welcome to TutsPlus\"}\n            &lt;\/button&gt;\n          &lt;\/csstransition&gt;&lt;\/switchtransition&gt;\n&lt;\/div&gt;\n\n      &lt;div&gt;\n      &lt;transitiongroup&gt;&lt;h2&gt;Contacts&lt;\/h2&gt;\n        {items.map((item, index) =&amp;gt; (\n          &lt;csstransition key=\"{index}\" timeout=\"{900}\" classnames=\"fade\"&gt;&lt;p&gt;{item}&lt;\/p&gt;\n          &lt;\/csstransition&gt;\n        ))}\n        &lt;button onclick=\"{onAddContacts}\"&gt;Add a Contact&lt;\/button&gt;\n      &lt;\/transitiongroup&gt;\n&lt;\/div&gt;\n    &amp;gt;\n  );\n};\n\nexport default Home;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u60a8\u4e86\u89e3\u4e86\u5982\u4f55\u5f00\u59cb\u5728 React \u4e2d\u4f7f\u7528\u52a8\u753b\u3002\u60a8\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 React \u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u4e86\u89e3\u4e86\u5982\u4f55\u5b9e\u73b0\u56db\u4e2a React Transition Group \u7ec4\u4ef6\u3002\u6709\u5173 React \u52a8\u753b\u7684\u6df1\u5165\u4fe1\u606f\uff0c\u6211\u5efa\u8bae\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u3002<\/p>\n<p>\u672c\u6559\u7a0b\u7684\u6e90\u4ee3\u7801\u53ef\u5728 GitHub \u4e0a\u83b7\u53d6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u63a2\u7d22 React \u52a8\u753b\u7684\u4e16\u754c\uff1a\u7b80\u4ecb\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u52a8\u753b\u662f\u6539\u5584\u6574\u4f53\u7528\u6237\u4f53\u9a8c\u7684\u597d\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u4eec\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u548c\u7528\u6237\u4e4b\u95f4\u8fdb\u884c\u66f4\u597d\u7684\u4ea4\u4e92\u3002 \u5728\u6211\u4eec\u4e4b\u524d\u7684\u4e00\u4e9b React \u6559\u7a0b\u4e2d\uff0c\u60a8\u719f\u6089\u4e86\u57fa\u672c\u7684 React \u6982\u5ff5\uff0c\u4f8b\u5982 JSX\u3001\u8def\u7531\u548c\u8868\u5355\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u5b83\u63d0\u5347\u5230\u4e00\u4e2a\u65b0\u7684\u6c34\u5e73\uff0c\u5e76\u5c1d\u8bd5\u7406\u89e3 React \u4e2d\u7684\u52a8\u753b\u3002\u867d\u7136\u6709\u5f88\u591a\u65b9\u6cd5\u53ef\u4ee5\u5411 React \u5e94\u7528\u7a0b\u5e8f\u6dfb\u52a0\u52a8\u753b\uff0c\u4f46\u6211\u4eec\u5c06\u5728\u672c\u6587\u4e2d\u91cd\u70b9\u4ecb\u7ecd React Transition Group \u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u3002 React \u4e2d\u7684\u52a8\u753b React \u63d0\u4f9b\u4e86\u8bb8\u591a\u7528\u4e8e\u52a8\u753b React \u5e94\u7528\u7a0b\u5e8f\u7684\u9644\u52a0\u5b9e\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u4e4b\u4e00\u79f0\u4e3a React Transition Group\uff0c\u7531 React \u5f00\u53d1\u56e2\u961f\u521b\u5efa\u3002 \u5b83\u4e0d\u662f\u4e00\u4e2a\u8bbe\u7f6e\u52a8\u753b\u6837\u5f0f\u7684\u5e93\uff1b\u76f8\u53cd\uff0c\u5b83\u662f\u4e00\u4e2a\u5177\u6709\u56db\u79cd\u7c7b\u578b\u5185\u7f6e\u7ec4\u4ef6\u7684\u4f4e\u7ea7 API\uff1aTransition\u3001CSSTransition\u3001SwitchTransition \u548c TransitionGroup\u3002\u56e0\u6b64\uff0c\u5728\u72b6\u6001\u66f4\u6539\u671f\u95f4\u5c06 React \u7ec4\u4ef6\u52a8\u753b\u79fb\u5165\u548c\u79fb\u51fa DOM \u975e\u5e38\u7b80\u5355\u3002 React Transition Group \u662f\u4e00\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u5165\u95e8\u5de5\u5177\uff0c\u800c\u4e14\u7531\u4e8e\u5b83\u662f\u8f7b\u91cf\u7ea7\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u51cf\u5c11\u5bf9\u6837\u677f\u4ee3\u7801\u7684\u9700\u6c42\uff0c\u4ece\u800c\u52a0\u5feb\u5f00\u53d1\u8fc7\u7a0b\u3002 \u5f00\u59cb\u4f7f\u7528 \u9996\u5148\uff0c\u8ba9\u6211\u4eec\u5728\u7ec8\u7aef\u4e2d\u4f7f\u7528 create-react-app \u5305\u5b89\u88c5 react \u3002 npx create-react-app react-animations \u767b\u5f55\u540e\u590d\u5236 \u6253\u5f00\u516c\u5171\u6587\u4ef6\u5939\u7684index.html\u6587\u4ef6\u5e76\u7f16\u8f91\u6807\u9898\uff0c\u5982\u4e0b\u6240\u793a\uff1a &lt;title&gt;TutsPlus &#8211; React Animations&lt;\/title&gt; \u767b\u5f55\u540e\u590d\u5236 \u8ba9\u6211\u4eec\u5728\u5e94\u7528\u7a0b\u5e8f\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32559","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32559"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32559\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}