{"id":187096,"date":"2020-10-28T10:19:50","date_gmt":"2020-10-28T02:19:50","guid":{"rendered":"http:\/\/4563.org\/?p=187096"},"modified":"2020-10-28T10:19:50","modified_gmt":"2020-10-28T02:19:50","slug":"react-redux%ef%bc%8c%e7%9a%84%e6%83%85%e5%86%b5%e4%b8%8b%ef%bc%8c%e5%a4%a7%e5%ae%b6%e6%80%8e%e4%b9%88%e6%b5%8b%e8%af%95-high-ordered-component","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=187096","title":{"rendered":"react + redux\uff0c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u5bb6\u600e\u4e48\u6d4b\u8bd5 high-ordered component?"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  react + redux\uff0c\u7684\u60c5\u51b5\u4e0b\uff0c\u5927\u5bb6\u600e\u4e48\u6d4b\u8bd5 high-ordered component?               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <span><i><\/i> 6<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u90fd\u6709 pure component \u548c high ordered component<\/p>\n<p>\u524d\u8005\u5c31\u662f\u7eaf\u7ec4\u4ef6\uff0c\u63a5\u6536 props\uff0c\u7136\u540e\u6839\u636e props \u753b\u51fa\u6765\u3002\u57fa\u672c\u6ca1\u6709\u8ba1\u7b97\uff0c\u5373\u4f7f\u6709\uff0c\u4e5f\u53ea\u9700\u8981 props \u5c31\u53ef\u4ee5\u5b8c\u6210\u3002\uff08\u8fd9\u4e2a\u6d4b\u8bd5\uff0c\u6211\u4eec\u901a\u5e38\u662f\u7528 testlibrary\/react \u6765\u6d4b\u8bd5\u753b\u51fa\u6765\u7684 dom \uff09<\/p>\n<p>\u4f46\u901a\u5e38\uff0cpure component \u5916\u9762\u4f1a\u6709\u4e00\u5c42 hoc, \u5373\u7528 redux \u7684\u65b9\u6cd5 connect \u628a mapStateToProps &amp; mapDispatchToProps \u7ed1\u5b9a\u7684\u90a3\u4e2a\u7ec4\u4ef6\u3002<\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\uff0c\u8fd9\u4e2a hoc \u600e\u4e48\u6d4b\u8bd5\uff1f<\/p>\n<p>mapStateToProps &amp; mapDispatchToProps \u6bd4\u8f83\u597d\u6d4b\u8bd5\uff0c\u53ea\u662f\u51fd\u6570\uff0cexport \u540e\uff0c\u5c31\u53ef\u4ee5\u6d4b\u8bd5\u4e86\u3002<\/p>\n<p>\u4f46\u662f\u8fd9\u4e2a hoc \u672c\u8eab\uff0c\u4e0d\u77e5\u9053\u600e\u4e48\u6d4b\u8bd5\u3002\u56e0\u4e3a\u8fd9\u4e2a\u7ec4\u4ef6\u5728\u4f20\u9012\u7ed9\u5185\u90e8\u7684 pure components \u7684 props \u65f6\uff0c\u8fd8\u662f\u6709\u4e00\u4e9b\u5185\u90e8\u7684\u51fd\u6570\u7684\uff0c\u4e3b\u8981\u662f\u8fd9\u4e9b\u51fd\u6570\uff0c\u4e0d\u77e5\u9053\u600e\u4e48\u6d4b\u8bd5\u3002<\/p>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff1a \u6211\u600e\u4e48\u6d4b\u8bd5\u8fd9\u4e2a getName \u51fd\u6570\uff1f<\/p>\n<pre><code>class mycomp extend React.Component {    function getName() {     \/\/\u6839\u636e props \u6765\u8ba1\u7b97     this.props.   }    render() {     return (       &lt;div&gt;         &lt;wrapped-component           name={this.getName()}         \/&gt;       &lt;\/div&gt;     );   } }  export default connect(mapStateToProps, mapDispatchToProps)(mycomp);  <\/code><\/pre>\n<p>\u8c22\u8c22\uff01<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>1<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4049842\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azcvcza <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e5f\u8bb8\u53ef\u4ee5\u53bb stackoverflow \u627e\u627e \u2018how to test higher order component in react&#8217;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>react + redux\uff0c\u7684\u60c5\u51b5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/187096"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=187096"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/187096\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=187096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=187096"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=187096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}