{"id":304,"date":"2021-09-25T16:12:22","date_gmt":"2021-09-25T08:12:22","guid":{"rendered":"http:\/\/gjweb.top\/?p=304"},"modified":"2021-09-25T19:37:42","modified_gmt":"2021-09-25T11:37:42","slug":"render-props-%e5%92%8c-%e9%ab%98%e9%98%b6%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=304","title":{"rendered":"render-props \u548c \u9ad8\u9636\u7ec4\u4ef6"},"content":{"rendered":"\n<ul class=\"wp-block-list\"><li>\u601d\u8003\uff1a\u5982\u679c\u4e24\u4e2a\u7ec4\u4ef6\u4e2d\u7684\u90e8\u5206\u529f\u80fd\u76f8\u4f3c\u6216\u76f8\u540c\uff0c\u8be5\u5982\u4f55\u5904\u7406<\/li><li>\u5904\u7406\u65b9\u5f0f\uff1a \u590d\u7528\u7ec4\u4ef6\u7684\u529f\u80fd\uff08\u8054\u60f3\u51fd\u6570\u5c01\u88c5\uff09<\/li><li>\u590d\u7528\u5565\uff1f <strong>state<\/strong> \u3001 <strong>\u64cd\u4f5cstate\u7684\u65b9\u6cd5<\/strong> \uff08\u7ec4\u4ef6\u72b6\u6001\u903b\u8f91\uff09<\/li><li>\u4e24\u79cd\u65b9\u5f0f\uff1a <strong>render props \u6a21\u5f0f<\/strong> \u3001<strong>\u9ad8\u9636\u7ec4\u4ef6\uff08HOC\uff09<\/strong><\/li><li>\u6ce8\u610f\uff1a\u8fd9\u4e24\u79cd\u65b9\u5f0f <strong>\u4e0d\u662f\u65b0\u7684API<\/strong> \uff0c \u800c\u662f\u5229\u7528React\u81ea\u5e26\u7279\u70b9\u7684\u7f16\u7801\u6280\u5de7\uff0c\u6f14\u5316\u800c\u6210\u7684\u56fa\u5b9a\u6a21\u5f0f<\/li><li>\u4e2a\u4eba\u7406\u89e3\uff1arender-props\u53ea\u662f\u5bf9\u76f8\u4f3c\u529f\u80fd\u8fdb\u884c\u4ee3\u7801\u62bd\u79bb\uff0c\u662f\u4e00\u79cd\u4ee3\u7801\u4f18\u5316\u7684\u7f16\u5199\u4e60\u60ef\uff0c\u5982\u679c\u4f60\u64c5\u957f\u5199\u5c4e\u5c71\uff0c\u5b8c\u5168\u53ef\u4ee5\u4e0d\u9075\u5faa\u8fd9\u79cd\u65b9\u5f0f\u3002<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">render props \u6a21\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.\u601d\u8def\u5206\u6790<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u601d\u8def\uff1a\u5c06\u590d\u7528\u7684 <strong>state<\/strong> \u548c\u64cd\u4f5c <strong>state<\/strong> \u7684\u65b9\u6cd5\u5c01\u88c5\u5230\u4e00\u4e2a\u7ec4\u4ef6\u4e2d<\/li><li>\u95ee\u98981\uff1a\u5982\u4f55\u62ff\u5230\u7ec4\u4ef6\u4e2d\u590d\u7528state\uff1f<\/li><li>\u5728\u4f7f\u7528\u7ec4\u4ef6\u65f6\uff0c\u6dfb\u52a0\u4e00\u4e2a\u503c\u4e3a<strong>\u51fd\u6570\u7684props<\/strong>\uff0c\u901a\u8fc7\u51fd\u6570 <strong>\u53c2\u6570<\/strong> \u6765\u83b7\u53d6 \uff08\u9700\u8981\u7ec4\u4ef6\u5185\u90e8\u5b9e\u73b0\uff09<\/li><li>\u95ee\u98982\uff1a \u5982\u4f55\u6e32\u67d3\u4efb\u610f\u7684UI?<\/li><li>\u4f7f\u7528 <strong>\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c<\/strong> \u4f5c\u4e3a\u8981\u6e32\u67d3\u7684UI\u5185\u5bb9\uff08\u9700\u8981\u7ec4\u4ef6\u5185\u90e8\u5b9e\u73b0\uff09<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Mounse render={(mouse)=&gt;{<br> &nbsp; &nbsp;return(<br> &nbsp;      &lt;p&gt;\u9f20\u6807\u7684\u5f53\u524d\u4f4d\u7f6e {mouse.x},{mouse.y}&lt;\/p&gt;<br> &nbsp;  )<br>}}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.\u4f7f\u7528\u6b65\u9aa4<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>\u521b\u5efaMouse\u7ec4\u4ef6\uff0c\u5728\u7ec4\u4ef6\u4e2d\u63d0\u4f9b\u590d\u7528\u7684\u72b6\u6001\u903b\u8f91\u4ee3\u7801\uff081.\u72b6\u6001\uff0c 2.\u64cd\u4f5c\u72b6\u6001\u7684\u65b9\u6cd5\uff09<\/li><li>\u5c06\u8981\u590d\u7528\u7684\u72b6\u6001\u4f5c\u4e3a <strong>props.render(state)<\/strong> \u65b9\u6cd5\u7684\u53c2\u6570\uff0c\u66b4\u9732\u5230\u7ec4\u4ef6\u5916\u90e8<\/li><li>\u4f7f\u7528 props.render() \u7684\u8fd4\u56de\u503c\u4f5c\u4e3a\u6e32\u67d3\u7684\u5185\u5bb9<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>class Mounse extends React.Component{<br> &nbsp; &nbsp;\/\/ ... \u7701\u7565 state \u548c\u64cd\u4f5c state \u7684\u65b9\u6cd5<br> &nbsp; &nbsp;render(){<br> &nbsp; &nbsp; &nbsp; &nbsp;return this.props.render(this.state)<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Mouse render={()=&gt;&lt;p&gt;\u5f53\u524d\u4f4d\u7f6e\u4e3a{mouse.x},{mouse.y}&lt;\/p&gt;}\/&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.\u4f7f\u7528 children \u66ff\u4ee3 render \u5c5e\u6027<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6ce8\u610f\uff1a \u5e76\u4e0d\u662f\u8be5\u6a21\u5f0f\u53eb <strong>render props<\/strong> \u5c31\u5fc5\u987b\u4f7f\u7528\u540d\u4e3a <strong>render<\/strong> \u7684 <strong>prop<\/strong>\uff0c\u5b9e\u9645\u4e0a\u4f7f\u7528\u4efb\u610f\u540d\u79f0\u7684 <strong>prop<\/strong><\/li><li>\u628a <strong>prop<\/strong> \u662f\u4e00\u4e2a\u51fd\u6570\u5e76\u4e14\u544a\u8bc9\u7ec4\u4ef6\u8981\u6e32\u67d3\u4ec0\u4e48\u5185\u5bb9\u7684\u6280\u672f\u53eb\u505a <strong>render props\u6a21\u5f0f<\/strong><\/li><li>\u5b98\u65b9\u63a8\u8350\uff1a\u4f7f\u7528 <strong>children<\/strong> \u66ff\u4ee3 <strong>render<\/strong> \u5c5e\u6027<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Mouse&gt;<br>    {(x,y)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;return(<br> &nbsp; &nbsp; &nbsp;      &lt;p&gt;\u9f20\u6807\u7684\u4f4d\u7f6e\u662f {x,y}&lt;\/p&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }}<br>&lt;\/Mouse&gt;<br>\/*\u7ec4\u4ef6\u5185\u90e8*\/<br>this.props.children(this.state)<br>\/*Content \u4e2d\u7684\u7528\u6cd5*\/<br>&lt;Consumer&gt;<br> &nbsp;  { data =&gt; &lt;span&gt; { data } &lt;\/span&gt; }<br>&lt;\/Consumer&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.\u4ee3\u7801\u4f18\u5316<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u63a8\u8350\u7ed9 <strong>render props<\/strong> \u6dfb\u52a0 <strong>props<\/strong> \u6821\u9a8c<\/li><li>\u5e94\u8be5\u5728\u7ec4\u4ef6\u5378\u8f7d\u65f6<strong>\u89e3\u9664<\/strong>\u591a\u4f59\u7684<strong>\u5168\u5c40\u4e8b\u4ef6<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>Mouse.propTypes = {<br> &nbsp; &nbsp;children:PropTypes.func.isRequired<br>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>componentWillUnmount(){<br> &nbsp; &nbsp;window.removeEventListener('mousemove',callBack)<br>}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u9ad8\u9636\u7ec4\u4ef6<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.\u601d\u8def\u5206\u6790<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>\u9ad8\u9636\u7ec4\u4ef6<\/strong> <strong>\u662f\u4e00\u4e2a\u51fd\u6570<\/strong> ,\u63a5\u53d7\u8981\u5305\u88c5\u7684\u7ec4\u4ef6\uff0c\u8fd4\u56de\u589e\u5f3a\u540e\u7684\u7ec4\u4ef6<\/li><li>\u9ad8\u9636\u7ec4\u4ef6\u6bcf\u90e8\u521b\u5efa\u4e00\u4e2a\u7c7b\u7ec4\u4ef6\uff0c\u5728\u8fd9\u4e2a\u7c7b\u7ec4\u4ef6\u4e2d\u63d0\u4f9b\u590d\u7528\u7684\u72b6\u6001\u903b\u8f91\u4ee3\u7801\uff0c\u901a\u8fc7 <strong>props<\/strong> \u5c06\u590d\u7528\u7684\u72b6\u6001\u4f20\u9012\u7ed9\u88ab\u5305\u88c5\u7ec4\u4ef6 <strong>WrappedCompoent<\/strong><\/li><\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">const EndancedComponent = withHOC(WrappedComponent)<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9ad8\u9636\u7ec4\u4ef6\u5185\u90e8\u521b\u5efa\u7684\u7c7b\u7ec4\u4ef6<br>class Mounse extends React.Component {<br> &nbsp; &nbsp;render () {<br> &nbsp; &nbsp; &nbsp; &nbsp;return &lt; WrappedComponent {...this.state} \/&gt;<br> &nbsp;  }<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2.\u4f7f\u7528\u6b65\u9aa4<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\uff0c\u540d\u79f0\u7ea6\u5b9a\u4ee5 <strong>with\u5f00\u5934<\/strong><\/li><li>\u6307\u5b9a\u51fd\u6570\u53c2\u6570\uff0c\u53c2\u6570\u5e94\u8be5\u4ee5 <strong>\u5927\u5199\u5b57\u6bcd\u5f00\u5934<\/strong> \uff08\u4f5c\u4e3a\u6e32\u67d3\u7684\u7ec4\u4ef6\uff09<\/li><li>\u5728\u51fd\u6570\u5185\u90e8\u521b\u5efa\u4e00\u4e2a\u7c7b\u7ec4\u4ef6\uff0c <strong>\u63d0\u4f9b\u590d\u7528\u7684\u72b6\u6001\u903b\u8f91\u4ee3\u7801<\/strong>\uff0c\u5e76\u8fd4\u56de<\/li><li>\u5728\u8be5\u7ec4\u4ef6\u4e2d\uff0c\u6e32\u67d3\u53c2\u6570\u7ec4\u4ef6\uff0c\u540c\u65f6\u5c06\u72b6\u6001\u901a\u8fc7 <strong>prop<\/strong> \u4f20\u9012\u7ed9\u53c2\u6570\u7ec4\u4ef6<\/li><li>\u8c03\u7528\u8be5\u9ad8\u9636\u7ec4\u4ef6\uff0c\u4f20\u5165\u8981\u589e\u5f3a\u7684\u7ec4\u4ef6\uff0c\u901a\u8fc7\u8fd4\u56de\u503c\u62ff\u5230\u589e\u5f3a\u540e\u7684\u7ec4\u4ef6\uff0c\u5e76\u5c06\u5176\u6e32\u67d3\u5230\u9875\u9762\u4e2d<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>function withMounse(WrappedComponent){<br> &nbsp; &nbsp;class Mouse extends React.Component {}<br> &nbsp; &nbsp;return Mouse<br>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Mouse\u7ec4\u4ef6\u7684render\u65b9\u6cd5\u4e2d<br>return &lt;WrappedComponent {...this.state} \/&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efa\u7ec4\u4ef6<br>const MousePosition = withMouse(Position)<br>\/\/ \u6e32\u67d3\u7ec4\u4ef6<br>&lt;MousePosition \/&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3.\u8bbe\u7f6e displayName<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528\u9ad8\u9636\u7ec4\u4ef6\u5b58\u5728\u7684\u95ee\u9898\uff1a\u5f97\u5230\u7684\u4e24\u4e2a\u7ec4\u4ef6\u540d\u79f0\u76f8\u540c<\/li><li>\u539f\u56e0\uff1a\u5728\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cReact\u4f7f\u7528 <strong>\u7ec4\u4ef6\u540d\u79f0<\/strong> \u4f5c\u4e3a <strong>displayName<\/strong><\/li><li>\u89e3\u51b3\u65b9\u5f0f\uff1a\u4e3a\u9ad8\u9636\u7ec4\u4ef6\u8bbe\u7f6e <strong>displayName<\/strong> \u4fbf\u4e8e\u8c03\u8bd5\u65f6\u533a\u5206\u4e0d\u540c\u7684\u7ec4\u4ef6<\/li><li>displayName\u7684\u4f5c\u7528\uff1a\u7528\u4e8e\u8bbe\u7f6e\u8c03\u8bd5\u4fe1\u606f \uff08React Developer Tools\u4fe1\u606f\uff09<\/li><\/ul>\n\n\n\n<p>\u8bbe\u7f6e\u65b9\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Mouse.displayName = `WithMouse${getDisplayName(wrappedComponent)}`<br>function getDisplayName(WrappedComponent){<br> &nbsp; &nbsp;return WrappedComponent.displayName || WrappedComponent.name || \"Component\"<br>}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4.\u4f20\u9012 props<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u95ee\u9898\uff1a <strong>props\u4e22\u5931<\/strong><\/li><li>\u539f\u56e0\uff1a\u9ad8\u9636\u7ec4\u4ef6\u6ca1\u6709\u5f80\u4e0b\u4f20\u9012 props<\/li><li>\u89e3\u51b3\u65b9\u5f0f\uff1a\u6e32\u67d3 <strong>WrappedComponent<\/strong> \u65f6\uff0c\u5c06 state \u548c this.peops \u4e00\u8d77\u4f20\u9012\u7ed9\u7ec4\u4ef6<\/li><li>\u4f20\u9012\u65b9\u5f0f\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt; WrapperComponent {...this.state} {...this.props} \/&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u601d\u8003\uff1a\u5982\u679c\u4e24\u4e2a\u7ec4\u4ef6\u4e2d\u7684\u90e8\u5206\u529f\u80fd\u76f8\u4f3c\u6216\u76f8\u540c\uff0c\u8be5\u5982\u4f55\u5904\u7406 \u5904\u7406\u65b9\u5f0f\uff1a \u590d\u7528\u7ec4\u4ef6\u7684\u529f\u80fd\uff08\u8054\u60f3\u51fd\u6570\u5c01\u88c5\uff09 \u590d\u7528\u5565\uff1f s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/304\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}