{"id":2281,"date":"2017-11-01T10:39:56","date_gmt":"2017-11-01T02:39:56","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2281"},"modified":"2017-11-01T10:39:56","modified_gmt":"2017-11-01T02:39:56","slug":"svg-to-react-%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2281","title":{"rendered":"svg-to-react \u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u5c06 <a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a> \u8f6c\u6362\u4e3a <a href=\"https:\/\/www.xttblog.com\/?tag=React\" name=\"React\" target=\"_blank\" title=\"React\">React<\/a> \u7ec4\u4ef6\u9664\u4e86 <a href=\"https:\/\/www.xttblog.com\/?tag=SVGR\" name=\"SVGR\" target=\"_blank\" title=\"SVGR\">SVGR<\/a>\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6js\u63d2\u4ef6\u3002\u6bd4\u5982\u6211\u4eec\u4eca\u5929\u8981\u5b66\u4e60\u7684 <a href=\"https:\/\/www.xttblog.com\/?tag=svg-to-react\" name=\"svg-to-react\" target=\"_blank\" title=\"svg-to-react\">svg-to-react<\/a>\u3002\u672c\u6587\u5c06\u4ecb\u7ecd <a href=\"https:\/\/www.xttblog.com\/?tag=svg-to-react\" name=\"svg-to-react\" target=\"_blank\" title=\"svg-to-react\">svg-to-react<\/a> \u7684\u76f8\u5173\u7528\u6cd5\u3002\n<\/p>\n<p>\n\t\u548c <a href=\"https:\/\/www.xttblog.com\/?tag=SVGR\" name=\"SVGR\" target=\"_blank\" title=\"SVGR\">SVGR<\/a> \u76f8\u6bd4 <a href=\"https:\/\/www.xttblog.com\/?tag=svg-to-react\" name=\"svg-to-react\" target=\"_blank\" title=\"svg-to-react\">svg-to-react<\/a> \u66f4\u7b80\u5355\uff0c\u5f53\u7136\u652f\u6301\u7684\u529f\u80fd\u4e5f\u66f4\u5c11\u3002<a href=\"https:\/\/www.xttblog.com\/?tag=svg-to-react\" name=\"svg-to-react\" target=\"_blank\" title=\"svg-to-react\">svg-to-react<\/a> \u76ee\u524d\u5df2\u7ecf\u505c\u66f4\u4e86\uff0c\u6700\u65b0\u7248\u672c\u8fd8\u662f3\u5e74\u524d\u53d1\u5e03\u7684\u5185\u5bb9\u3002\n<\/p>\n<h2>\n\tsvg-to-react\u57fa\u672c\u7528\u6cd5<br \/>\n<\/h2>\n<p>\n\t\u5c06<a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a>\u8f6c\u6362\u4e3a<a href=\"https:\/\/www.xttblog.com\/?tag=React\" name=\"React\" target=\"_blank\" title=\"React\">React<\/a>\u7ec4\u4ef6\u3002\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u6765\u4ecb\u7ecd\u5b83\u3002\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar toReact = require(&#039;svg-to-react&#039;);\r\nvar renderComponent = toReact.convert(&#039;&lt;svg xmlns=&quot;http:\/\/www.w3.org\/2000\/svg&quot;&gt;&#039;+\r\n    &#039;&lt;circle stroke-width=&quot;2&quot; stroke=&quot;#424242&quot;&gt;&lt;\/circle&gt;&#039;+\r\n  &#039;&lt;\/svg&gt;&#039;\r\n);<\/pre>\n<p>\n\t\u8f6c\u6362\u4e3aReact\u4ee3\u7801\u540e\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nvar renderComponent = function anonymous(params) {\r\n  return React.createElement(&#039;svg&#039;, params,\r\n    React.createElement(&#039;circle&#039;, {\r\n      strokeWidth: &#039;2&#039;,\r\n      stroke: params.color\r\n    })\r\n  );\r\n}<\/pre>\n<h2>\n\t\u5e38\u7528\u65b9\u6cd5\u4ecb\u7ecd<br \/>\n<\/h2>\n<p>\n\tvar renderFunction = svgToReact.convert(svgString)\n<\/p>\n<p>\n\t<font><font>\u4f7f\u7528svgString\uff0c\u901a\u8fc7<\/font><\/font><a href=\"https:\/\/github.com\/svg\/svgo\"><font><font>SVGO<\/font><\/font><\/a><font><font>\u8fd0\u884c\u5b83\uff0c<\/font><font>\u5e76\u8fd4\u56de\u4e00\u4e2a\u4f7f\u7528<\/font><\/font><a href=\"https:\/\/www.xttblog.com\/?tag=React\" name=\"React\" target=\"_blank\" title=\"React\">React<\/a><font><font>\u7ec4\u4ef6\u6e32\u67d3<\/font><\/font><a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a><font><font>\u7684\u51fd\u6570\u3002<\/font><font>\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a<\/font><\/font><code>params<\/code><font><font>\u5141\u8bb8\u60a8\u81ea\u5b9a\u4e49<\/font><\/font><a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a><font><font>\u4ee5\u53ca\u4f20\u9012\u6807\u51c6\u7684<\/font><\/font><a href=\"https:\/\/www.xttblog.com\/?tag=React\" name=\"React\" target=\"_blank\" title=\"React\">React<\/a><font><font>\u5904\u7406\u7a0b\u5e8f\u548c\u9053\u5177\u7684\u5bf9\u8c61\u3002<\/font><\/font>\n<\/p>\n<p>\n\tsvgToReact.convertFile(filePath, function(err, renderFunction) {})\n<\/p>\n<p>\n\tfilePath\u5728\u56de\u8c03\u4e2d\u8bfb\u53d6<a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a>\u5e76\u8fd4\u56de\u6e32\u67d3\u51fd\u6570\n<\/p>\n<p>\n\tsvgToReact.convertDir(dirPath, function(err, renderFunctions) {})\n<\/p>\n<p>\n\t\u9012\u5f52\u5730\u8bfb\u53d6\u6240\u6709dirPathSVG\u5e76\u8fd4\u56de\u5305\u542b\u6bcf\u4e2a<a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a>\u6587\u4ef6\u7684\u6240\u6709\u6e32\u67d3\u529f\u80fd\u7684\u5bf9\u8c61\u3002\u5bf9\u8c61\u7684\u683c\u5f0f\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\n{\r\n  &quot;svgFile&quot;: function () {},\r\n  &quot;nested\/svgFile&quot;: function() {}\r\n}<\/pre>\n<p>\n\t\u6bcf\u4e2a\u6309\u952e\u90fd\u662f\u4e00\u4e2a\u76f8\u5bf9\u7684\u76ee\u5f55\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u51cf\u53bb\u7684<a href=\"https:\/\/www.xttblog.com\/?tag=SVG\" name=\"SVG\" target=\"_blank\" title=\"SVG\">SVG<\/a>\u7684\u5ef6\u4f38\u3002\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8e&nbsp;<a href=\"https:\/\/www.xttblog.com\/?tag=svg-to-react\" name=\"svg-to-react\" target=\"_blank\" title=\"svg-to-react\">svg-to-react<\/a> \u7684\u4e00\u4e2a\u7b80\u5355\u6559\u7a0b\uff0c\u66f4\u591a\u4f7f\u7528\u65b9\u6cd5\u8bf7\u53c2\u8003\u5b98\u65b9<a href=\"https:\/\/github.com\/publitas\/svg-to-react\" name=\"svg-to-react\" target=\"_blank\" title=\"svg-to-react\">\u6587\u6863<\/a>\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2281\">svg-to-react \u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5c06 SVG \u8f6c\u6362\u4e3a React \u7ec4\u4ef6\u9664\u4e86 SVGR\uff0c\u8fd8\u6709\u5f88\u591a\u5176\u4ed6js\u63d2\u4ef6\u3002\u6bd4\u5982\u6211\u4eec\u4eca\u5929\u8981\u5b66\u4e60\u7684 svg-to- [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[373,52,1565,1563],"class_list":["post-2281","post","type-post","status-publish","format-standard","hentry","category-html5","tag-react","tag-svg","tag-svg-to-react","tag-svgr"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2281"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2281\/revisions"}],"predecessor-version":[{"id":2282,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2281\/revisions\/2282"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}