{"id":7789,"date":"2023-05-05T09:37:13","date_gmt":"2023-05-05T09:37:13","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=7789"},"modified":"2023-07-27T06:39:25","modified_gmt":"2023-07-27T06:39:25","slug":"react-select-with-formik","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react\/react-select-with-formik","title":{"rendered":"How to Bind React-select with Formik"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Like a normal input, we can&#8217;t bind formik&#8217;s handleChange to React-select&#8217;s onChange directly since it gives selectedValue object in the call back parameter instead of an event. So, we need to utilize formik&#8217;s setFieldValue instead as per below example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-linenumbers=\"false\">&lt;Formik {...initialValues}&gt; \r\n{({ values, setFieldValue }) =&gt; \r\n( &lt;ReactSelect name=\"selectValue\" value={values.selectValue} \r\noptions={optionsList} onChange={(selectedOption) =&gt; \r\n{ setFieldValue(\"selectValue\", selectedOption); }} \/&gt; )} \r\n&lt;\/Formik&gt;;<\/pre>\n<p><span style=\"font-weight: 400;\">Though, it\u2019s worth noting that formik is no longer being maintained as per this discussion: <\/span><a href=\"https:\/\/github.com\/jaredpalmer\/formik\/issues\/3613\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/github.com\/jaredpalmer\/formik\/issues\/3613<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Like a normal input, we can&#8217;t bind formik&#8217;s handleChange to React-select&#8217;s onChange directly since it gives selectedValue object in the call back parameter instead of an event. So, we need to utilize formik&#8217;s setFieldValue instead as per below example: &lt;Formik {&#8230;initialValues}&gt; {({ values, setFieldValue }) =&gt; ( &lt;ReactSelect name=&#8221;selectValue&#8221; value={values.selectValue} options={optionsList} onChange={(selectedOption) =&gt; { setFieldValue(&#8220;selectValue&#8221;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8334,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7789","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7789"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=7789"}],"version-history":[{"count":6,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7789\/revisions"}],"predecessor-version":[{"id":8335,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/7789\/revisions\/8335"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8334"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=7789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=7789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=7789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}