title: Redux State Management with a Input field
body: <p>Bit of a noob to redux but this appears to be quite a difficult question! I hope someone may be able to help me :)</p>
  <p>I have build a page where you can input a search for different types of jobs. From this, it will make a get request to my DB and get all the info on this job. As this page is multi-levelled, I want to use redux to dispatch and pass the state throughout. This will help me pass my data on the job, e.g Data Analyst, through to each component so it can use the data and populate fields.</p>
  <p>However, this was how my input field was originally setup:</p>
  <pre><code>export function SearchBarComp(props) {
  
      const [isExpanded, setExpanded] = useState(false);
      const [parentRef, isClickedOutside ] = useClickOutside();
      const inputRef = useRef();
      const [searchQuery, setSearchQuery] = useState(&quot;&quot;);
      const [isLoading, setLoading] = useState(false);
      const [jobPostings, setjobPostings] = useState([]);
      const [noRoles, setNoRoles] = useState(false)
  
      const isEmpty = !jobPostings || jobPostings.length === 0;
  
      const changeHandler = (e) =&gt; {
          //prevents defaulting, autocomplete
          e.preventDefault();
          if(e.target.value.trim() === '') setNoRoles(false);
  
          setSearchQuery(e.target.value);
      }
  
      const expandedContainer = () =&gt; {
          setExpanded(true);
      }
  
  
      //LINK THE BACKEND!
      const prepareSearchQuery = (query) =&gt; {
          //const url = `http://localhost:5000/api/role/title?title=${query}`;
          const url = `http://localhost:5000/api/role/titlerole?title=${query}`;
  
  
          //replaces bad query in the url
          return encodeURI(url);
      }
  
      const searchRolePosition = async () =&gt; {
          if(!searchQuery || searchQuery.trim() === &quot;&quot;)
              return;
  
          setLoading(true);
          setNoRoles(false);
  
          const URL = prepareSearchQuery(searchQuery);
  
          const response = await axios.get(URL).catch((err) =&gt; {
              console.log(err);
          });
  
  
          if(response) {
              console.log(&quot;Response&quot;, response.data);
  
              if(response.data &amp;&amp; response.data === 0)
                  setNoRoles(true);
  
              setjobPostings(response.data);
          }
  
          setLoading(false);
      }
  
      useDebounce(searchQuery, 500, searchRolePosition)
  
      const collapseContainer = () =&gt; {
          setExpanded(false);
          setSearchQuery(&quot;&quot;);
           setLoading(false);
           setNoRoles(false);
          if (inputRef.current) inputRef.current.value = &quot;&quot;;
  
        };
  
      // console.log(&quot;Value&quot;, searchQuery)
  
      useEffect(()=&gt; {
          if(isClickedOutside)
              collapseContainer();
      }, [isClickedOutside])
  
      return (
      &lt;SearchBarContainer animate = {isExpanded ? &quot;expanded&quot; : &quot;collapsed&quot;} 
                          variants={containerVariants} transition={containerTransition} ref={parentRef}&gt;
          &lt;SearchInputContainer&gt;
              &lt;SearchIconSpan&gt;
                  &lt;SearchIcon/&gt;
              &lt;/SearchIconSpan&gt;
              &lt;SearchInput placeholder = &quot;Search for Roles&quot; 
                                  onFocus={expandedContainer} 
                                  ref={inputRef}
                                  value={searchQuery}
                                  onChange={changeHandler}
                                  /&gt;
              &lt;AnimatePresence&gt;
                  {isExpanded &amp;&amp; (&lt;CloseIconSpan  key=&quot;close-icon&quot; 
                                  inital={{opacity:0, rotate: 0}} 
                                  animate={{opacity:1, rotate: 180}} 
                                  exit={{opacity:0, rotate: 0}} 
                                  transition={{duration: 0.2}}
                                  onClick={collapseContainer}&gt;
                      &lt;CloseIcon/&gt;
                  &lt;/CloseIconSpan&gt;
                   )}
              &lt;/AnimatePresence&gt;
          &lt;/SearchInputContainer&gt;
          {isExpanded &amp;&amp; &lt;LineSeperator/&gt;}
          {isExpanded &amp;&amp; &lt;SearchContent&gt;
  
          {!isLoading &amp;&amp; isEmpty &amp;&amp; !noRoles &amp;&amp; (
          &lt;Typography color=&quot;gray&quot; display=&quot;flex&quot; flex=&quot;0.2&quot; alignSelf=&quot;center&quot; justifySelf=&quot;center&quot;&gt;
                  Start typing to search
          &lt;/Typography&gt;
          )}
          
          {!isLoading &amp;&amp; !isEmpty &amp;&amp; &lt;&gt;
              {jobPostings.map((searchRolePosition) =&gt; (
                  &lt;JobSection
                      title={searchRolePosition.title}
                      //will need to do something like ----
                      //people = {searchRolePosition.title &amp;&amp; searchRolePosition.title.average}
                      // future implementations
                  /&gt;
              ))}    
          &lt;/&gt;}
        
          &lt;/SearchContent&gt;}
      &lt;/SearchBarContainer&gt;
      )
  }
  
  </code></pre>
  <p><strong>As you can see, the main thing is the 'query' this creates a backend request to my titlerole, such as getting the data on Data Analyst. This all works in my frontend right now, but I can't pass that data down to the next component etc</strong></p>
  <p>So i'm looking to use redux.</p>
  <p>I've created the following slice:</p>
  <pre><code>import { createSlice } from &quot;@reduxjs/toolkit&quot;;
  
  const jobSearchSlice = createSlice({
    name: &quot;jobsearch&quot;,
    initialState: {
      currentRole: null,
      isFetching: false,
      error: false,
    },
    reducers: {
      jobsearchStart: (state) =&gt; {
        state.isFetching = true;
      },
      jobsearchSuccess: (state, action) =&gt; {
        state.isFetching = false;
        state.currentRole = action.payload;
      },
      jobsearchFailure: (state) =&gt; {
        state.isFetching = false;
        state.error = true;
      },
    },
  });
  
  export const { jobsearchStart, jobsearchSuccess, jobsearchFailure } = jobSearchSlice.actions;
  export default jobSearchSlice.reducer;
  </code></pre>
  <p>With this, I'm also using the following apiCalls.js file:</p>
  <pre><code>import { jobsearchStart, jobsearchSuccess, jobsearchFailure } from &quot;./jobsearchSlice&quot;;
  import { publicRequest } from &quot;../requestMethods&quot;;
  export const roleSearchQuery = async (dispatch, jobsearch) =&gt; {
    dispatch(jobsearchStart());
    try{
      const res = await publicRequest.get(&quot;`http://localhost:5000/api/role/titlerole?title=${query}`&quot;, jobsearch);
      dispatch(jobsearchSuccess(res.data))
    }catch(err){
      dispatch(jobsearchFailure());
    }
  };
  </code></pre>
  <p>My question is as a Redux noob, how do I implement this query functionality into a redux API request? What's the way to do this properly as I begin to tranisition this to an app which uses standard state management!</p>