{"id":3565,"date":"2024-11-10T15:01:52","date_gmt":"2024-11-10T07:01:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3565\/"},"modified":"2024-11-10T15:01:52","modified_gmt":"2024-11-10T07:01:52","slug":"react-%e9%80%89%e6%8b%a9-%e8%87%aa%e5%ae%9a%e4%b9%89%e6%a0%b7%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3565\/","title":{"rendered":"React \u9009\u62e9 + \u81ea\u5b9a\u4e49\u6837\u5f0f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173105928384310.jpg\" class=\"aligncenter\" title=\"React \u9009\u62e9 + \u81ea\u5b9a\u4e49\u6837\u5f0f\u63d2\u56fe\" alt=\"React \u9009\u62e9 + \u81ea\u5b9a\u4e49\u6837\u5f0f\u63d2\u56fe\" \/><\/p>\n<p>react select \u5e93\u662f\u5f00\u53d1\u4eba\u5458\u521b\u5efa\u53ef\u5b9a\u5236\u4e0b\u62c9\u7ec4\u4ef6\u7684\u6d41\u884c\u9009\u62e9\u4e4b\u4e00\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 react select \u4e2d\u521b\u5efa\u6837\u5f0f\u6216\u81ea\u5b9a\u4e49\u6837\u5f0f\uff0c\u4ee5\u521b\u5efa\u9002\u5408\u5e94\u7528\u7a0b\u5e8f\u4e3b\u9898\u548c ui \u8bbe\u8ba1\u7684\u5916\u89c2\u3002<\/p>\n<p><strong>\u9009\u62e9 react select\uff1f<\/strong><br \/> react select \u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5177\u6709\u641c\u7d22\u3001\u591a\u9879\u9009\u62e9\u548c\u53ef\u81ea\u5b9a\u4e49\u9009\u9879\u7b49\u529f\u80fd\u7684\u7075\u6d3b\u4e0b\u62c9\u83dc\u5355\u3002\u4f46\u662f\uff0c\u9ed8\u8ba4\u5916\u89c2\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u9002\u5408\u8bbe\u8ba1\u9700\u6c42\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6837\u5f0f\uff0c\u4ee5\u4fbf\u4e0b\u62c9\u83dc\u5355\u4e0e\u5e94\u7528\u7a0b\u5e8f\u7684\u5916\u89c2\u878d\u5408\u3002<\/p>\n<p><strong>\u5728\u9879\u76ee\u4e2d\u4f7f\u7528 react select<\/strong><br \/> \u8981\u4f7f\u7528 react select\uff0c\u9996\u5148\u5b89\u88c5\u5176\u4f9d\u8d56\u9879\uff1a<\/p>\n<pre>npm install react-select\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\uff0c\u5c06\u5176\u5bfc\u5165\u7ec4\u4ef6\u5e76\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u4e0b\u62c9\u5217\u8868\uff1a<\/p>\n<pre>import select from 'react-select';\n\nconst options = [\n  { value: 'apple', label: 'apple' },\n  { value: 'banana', label: 'banana' },\n  { value: 'cherry', label: 'cherry' },\n];\n\nfunction myselect() {\n  return &lt;select options={options} \/&gt;;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u5b9a\u5236\u98ce\u683c <\/h3>\n<p><strong>\u81ea\u5b9a\u4e49\u7ec4\u4ef6 react select<\/strong><br \/> \u521b\u5efa 1 \u4e2a\u65b0\u6587\u4ef6\uff0c\u5e76\u7528\u4ee5\u4e0b\u811a\u672c\u586b\u5145\u5b83<\/p>\n<pre>import select from 'react-select'\n\nconst customselectcomponent = ({ onchange, options, value, placeholder }: any) =&gt; {\n\n    const defaultvalue = (options: any, value: any) =&gt; {\n        return options ? options.find((option: any) =&gt; option.value === value) : ''\n    }\n\n    const customstyles = {\n        option: (provided: any, state: { isselected: any }) =&gt; ({\n            ...provided,\n            color: state.isselected ? '#ab0202' : '#000000',\n            background: state.isselected ? '#f4f7f9' : '#ffffff',\n            opacity: 1,\n            '&amp;:hover': {\n                backgroundcolor: '#fcc2c2',\n                cursor: 'pointer',\n            },\n        }),\n        menuportal: (base: any) =&gt; ({ ...base, zindex: 9999 }),\n        control: (base: any, state: any) =&gt; ({\n            ...base,\n            background: '#f7f7f7',\n            bordercolor: '#c0c4d6',\n            '&amp;:hover': {\n                bordercolor: state.isfocused ? '#f7f7f7' : 'blue',\n                cursor: 'pointer',\n            },\n        }),\n        singlevalue: (provided: any, state: { isdisabled: any }) =&gt; {\n            const opacity = state.isdisabled ? 1 : 1\n            const transition = 'opacity 300ms'\n\n            return { ...provided, opacity, transition }\n        },\n    }\n\n    return (\n        &lt;select\n            value={defaultvalue(options, value)}\n            onchange={(value) =&gt; onchange(value)}\n            options={options}\n            placeholder={placeholder}\n            styles={customstyles}\n            menuportaltarget={document.body}\n        \/&gt;\n    )\n}\n\nexport default customselectcomponent\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7ec4\u4ef6\u4e2d\u5404\u4e2aprop\u7684\u4f5c\u7528<\/p>\n<ul>\n<li>\n<p>onchange \u662f\u6bcf\u6b21\u4e0b\u62c9\u9009\u9879\u53d1\u751f\u66f4\u6539\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002 <\/p>\n<\/li>\n<li>\n<p>options \u662f\u4e00\u4e2a\u5bf9\u8c61\u6570\u7ec4\uff0c\u4ee3\u8868\u4e0b\u62c9\u5217\u8868\u4e2d\u53ef\u7528\u7684\u9009\u9879\u3002<\/p>\n<\/li>\n<li>\n<p>value \u662f\u5f53\u524d\u4ece\u4e0b\u62c9\u5217\u8868\u4e2d\u9009\u62e9\u7684\u503c\u3002\u6b64\u5c5e\u6027\u7528\u4e8e\u8bbe\u7f6e\u5f53\u524d\u5728\u4e0b\u62c9\u5217\u8868\u4e2d\u9009\u62e9\u7684\u9009\u9879\u3002<\/p>\n<\/li>\n<li>\n<p>\u5360\u4f4d\u7b26\u662f\u5728\u9009\u62e9\u9009\u9879\u4e4b\u524d\u4f5c\u4e3a\u7528\u6237\u6307\u5357\u663e\u793a\u7684\u6587\u672c\u3002\u6b64\u6587\u672c\u5c06\u663e\u793a\u5728\u4e0b\u62c9\u5217\u8868\u4e2d\uff0c\u4f5c\u4e3a\u9009\u62e9\u9009\u9879\u7684\u6307\u5357\u3002<\/p>\n<\/li>\n<\/ul>\n<p>2.<strong>\u5982\u4f55\u4f7f\u7528customselectcomponent<\/strong><\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b<\/p>\n<pre>\nimport CustomSelectComponent from '.\/SelectComponent'\n\nconst App = () =&gt; {\n\n    const [value, setValue] = useState('')\n    const options = [\n        { value: 'apple', label: 'Apple' },\n        { value: 'banana', label: 'Banana' },\n        { value: 'cherry', label: 'Cherry' },\n    ];\n\n    return (\n        &lt;&gt;\n            &lt;CustomSelectComponent\n                placeholder={'Category'}\n                value={value}\n                options={options}\n                onChange={(value: any) =&gt; {\n                    setValue(value.value)\n                }}\n            \/&gt;\n        &lt;\/&gt;)\n}\n\nexport { App }\n\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fReact \u9009\u62e9 + \u81ea\u5b9a\u4e49\u6837\u5f0f\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>react select \u5e93\u662f\u5f00\u53d1\u4eba\u5458\u521b\u5efa\u53ef\u5b9a\u5236\u4e0b\u62c9\u7ec4\u4ef6\u7684\u6d41\u884c\u9009\u62e9\u4e4b\u4e00\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u5982\u4f55\u5728 react select \u4e2d\u521b\u5efa\u6837\u5f0f\u6216\u81ea\u5b9a\u4e49\u6837\u5f0f\uff0c\u4ee5\u521b\u5efa\u9002\u5408\u5e94\u7528\u7a0b\u5e8f\u4e3b\u9898\u548c ui \u8bbe\u8ba1\u7684\u5916\u89c2\u3002 \u9009\u62e9 react select\uff1f react select \u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5177\u6709\u641c\u7d22\u3001\u591a\u9879\u9009\u62e9\u548c\u53ef\u81ea\u5b9a\u4e49\u9009\u9879\u7b49\u529f\u80fd\u7684\u7075\u6d3b\u4e0b\u62c9\u83dc\u5355\u3002\u4f46\u662f\uff0c\u9ed8\u8ba4\u5916\u89c2\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u9002\u5408\u8bbe\u8ba1\u9700\u6c42\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u81ea\u5b9a\u4e49\u6837\u5f0f\uff0c\u4ee5\u4fbf\u4e0b\u62c9\u83dc\u5355\u4e0e\u5e94\u7528\u7a0b\u5e8f\u7684\u5916\u89c2\u878d\u5408\u3002 \u5728\u9879\u76ee\u4e2d\u4f7f\u7528 react select \u8981\u4f7f\u7528 react select\uff0c\u9996\u5148\u5b89\u88c5\u5176\u4f9d\u8d56\u9879\uff1a npm install react-select \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\uff0c\u5c06\u5176\u5bfc\u5165\u7ec4\u4ef6\u5e76\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u4e0b\u62c9\u5217\u8868\uff1a import select from &#8216;react-select&#8217;; const options = [ { value: &#8216;apple&#8217;, label: &#8216;apple&#8217; }, { value: &#8216;banana&#8217;, label: &#8216;banana&#8217; }, { value: &#8216;cherry&#8217;, label: &#8216;cherry&#8217; }, ]; function myselect() { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3565","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3565","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=3565"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3565\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}