<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Uncategorized &#8211; Chris Philbin&#039;s Blog</title>
	<atom:link href="https://www.chrisphilbin.net/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.chrisphilbin.net</link>
	<description>Perfection Through Persistence.</description>
	<lastBuildDate>Mon, 01 Jun 2020 11:23:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.2</generator>
	<item>
		<title>Using React Select along with Redux and Hooks</title>
		<link>https://www.chrisphilbin.net/using-react-select-along-with-redux-and-hooks/</link>
		
		<dc:creator><![CDATA[Chris]]></dc:creator>
		<pubDate>Mon, 01 Jun 2020 11:23:48 +0000</pubDate>
				<category><![CDATA[Uncategorized]]></category>
		<guid isPermaLink="false">https://chrisphilbin.net/?p=228</guid>

					<description><![CDATA[React Select is an amazing input control with multiselect, autocomplete, async and creatable support. Here&#8217;s a typical use case in which we are using Redux to manage the app&#8217;s global&#8230;]]></description>
										<content:encoded><![CDATA[
<p>React Select is an amazing input control with multiselect, autocomplete, async and creatable support.</p>



<p>Here&#8217;s a typical use case in which we are using Redux to manage the app&#8217;s global state along with React Hooks to move away from having to write classes to create a new component</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
//Setting up all of the necessary imports

import React, { useState, useEffect } from &#039;react&#039;;
import { useSelector, useDispatch } from &#039;react-redux&#039;

import Select from &#039;react-select&#039;;
import makeAnimated from &#039;react-select/animated&#039;

import Form from &#039;react-bootstrap/Form&#039;
import Button from &#039;react-bootstrap/Button&#039;
</pre></div>


<p>We can really break up the rest of the component into several distinct parts.  I&#8217;ll break them into smaller chunks below, but keep in mind the entire component has been broken up into smaller chunks to allow for some clarity.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
const AddInterestsToEmployeeForm = (props) =&gt; {

    const dispatch = useDispatch()

    useEffect( () =&gt; {
        dispatch(fetchAllInterests())
    }, &#x5B;])

    const interests = useSelector(state =&gt; state.interests.interests)

    const options = &#x5B;]

    interests.map( (interest) =&gt; (
        options.push({ value: interest.name, label: interest.name })
    ))
</pre></div>


<p>Above we&#8217;re fetching all interest objects by using the useEffect hook to dispatch the action and update the Redux store upon every render. From there we&#8217;re using the useSelector hook in order to update the component&#8217;s local state with the interests that were put into the Redux store. Finally we define a blank array and then iterate over the local state in order to produce an array full of objects containing the value and label keys for our Select input.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
    const &#x5B;selectedOptions, setSelectedOptions] = useState(&#x5B;])

    const handleChange = options =&gt; {
        setSelectedOptions(options)
    };

    const handleSubmit = (e) =&gt; {
        e.preventDefault()
        let ee_interests = selectedOptions.map( int =&gt; (int.value))
        dispatch(addInterestsToEmployee(ee_interests))
        
    }

    const animatedComponents = makeAnimated()
</pre></div>


<p>Here we&#8217;re setting up the logic to deal with the user interacting with the Select input field. <strong>const [selectedOptions, setSelectedOptions] = useState([])</strong> is essentially creating a new variable called selectedOptions, creating a function called setSelectedOptions to allow us to update the variable, and useState([]) will set the initial value of selectedOptions to a blank array. </p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
    return (
        &lt;Form onSubmit={handleSubmit}&gt;
            &lt;Select
            closeMenuOnSelect={false}
            components={animatedComponents}
            isMulti
            value={selectedOptions}
            options={options}
            onChange={handleChange}
            /&gt;

            &lt;Button variant=&quot;primary&quot; type=&quot;submit&quot;&gt;Assign&lt;/Button&gt;
        &lt;/Form&gt;
    );
</pre></div>


<p>Last but not least the part that actually gets rendered to the DOM.  We&#8217;ve hooked up the form&#8217;s onSubmit event to the handleSubmit function we created earlier so when the user clicks the &#8220;Assign&#8221; button the form knows what to do. The <strong>Select</strong> tag itself has a lot of different options, which are documented very well on the <a href="https://github.com/jedwatson/react-select">official github repository</a>, but the two most important here are <strong>value</strong> and <strong>onChange</strong>. The select element is getting all of its initial options from the redux store and is keeping track of what options the user has selected by updating the component&#8217;s local state via the <strong>onChange</strong> function we defined. </p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
